Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用";char";_C_Arrays_Free_Malloc_Boolean - Fatal编程技术网

使用";char";

使用";char";,c,arrays,free,malloc,boolean,C,Arrays,Free,Malloc,Boolean,我制作了一个对象,它实际上表示存储在一个字符中的8个布尔值的数组。我来这里是为了学习更多关于位运算符的知识,以及如何在C中创建自己的对象。因此,我有两个问题: 我能确定以下代码是否正确吗 总是有用吗 这是一个很好的实现吗 制造一个不会丢失的物体 在C语言中,除非你释放它 你自己 代码: /* * IEFBooleanArray.h * IEFBooleanArray * * Created by ief2 on 8/08/10. * Copyright 2010 ief2. A

我制作了一个对象,它实际上表示存储在一个字符中的8个布尔值的数组。我来这里是为了学习更多关于位运算符的知识,以及如何在C中创建自己的对象。因此,我有两个问题:

  • 我能确定以下代码是否正确吗 总是有用吗
  • 这是一个很好的实现吗 制造一个不会丢失的物体 在C语言中,除非你释放它 你自己
  • 代码:

    /*
     *  IEFBooleanArray.h
     *  IEFBooleanArray
     *
     *  Created by ief2 on 8/08/10.
     *  Copyright 2010 ief2. All rights reserved.
     *
     */
    
    #ifndef IEFBOOLEANARRAY_H
    #define IEFBOOLEANARRAY_H
    
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    
    typedef char * IEFBooleanArrayRef;
    
    void IEFBooleanArrayCreate(IEFBooleanArrayRef *ref);
    void IEFBooleanArrayRelease(IEFBooleanArrayRef ref);
    int IEFBooleanArraySetBitAtIndex(IEFBooleanArrayRef ref, 
                                     unsigned index, 
                                     int flag);
    int IEFBooleanArrayGetBitAtIndex(IEFBooleanArrayRef ref, 
                                     unsigned index);
    
    #endif
    
    /*
    *IEFBooleanArray.h
    *IEFBoolean阵列
    *
    *由ief2于2010年8月8日创建。
    *版权所有2010 ief2。版权所有。
    *
    */
    #ifndef IEFBoolean阵列
    #定义IEFBOOLEANARRAY_H
    #包括
    #包括
    #包括
    typedef char*IEFBooleanArrayRef;
    无效IEFBooleanArrayCreate(IEFBooleanArrayRef*ref);
    无效IEFBooleanArrayRelease(IEFBooleanArrayRef参考);
    int IEFBooleanArraySetBitAtIndex(IEFBooleanArrayRef,
    无符号索引,
    int标志);
    内部IEFBooleanArrayGetBitIndex(IEFBooleanArrayRef,
    无符号索引);
    #恩迪夫
    

    /*
    *IEFBooleanArray.c
    *IEFBoolean阵列
    *
    *由ief2于2010年8月8日创建。
    *版权所有2010 ief2。版权所有。
    *
    */
    #包括“IEFBooleanArray.h”
    无效IEFBooleanArrayCreate(IEFBooleanArrayRef*ref){
    IEFBooleanArrayRef新参考;
    newReference=malloc(sizeof(char));
    memset(newReference,0,sizeof(char));
    *ref=新参考;
    }
    无效IEFBooleanArrayRelease(IEFBooleanArrayRef){
    自由(参考);
    }
    int IEFBooleanArraySetBitIndex(IEFBooleanArrayRef,无符号索引,int标志){
    原始状态;
    如果(指数<0 | |指数>7)
    返回-1;
    如果(标志==0)
    flag=0;
    其他的
    flag=1;
    原始状态=IEFBooleanArrayGetBitAtIndex(参考,索引);
    if(orignalStatus==0&&flag==1)
    *ref=*ref+(int)功率(2,指数);
    else if(orignalStatus==1&&flag==0)
    *ref=*ref-(int)功率(2,索引);
    返回0;
    }
    int IEFBooleanArrayGetBitIndex(IEFBooleanArrayRef参考,无符号索引){
    int结果;
    int值;
    值=(int)pow(2,索引);
    结果=值&*ref;
    如果(结果==0)
    返回0;
    其他的
    返回1;
    }
    
    我更喜欢C语言,但我真的很想学习C语言。有人能要求我多做些“家庭作业”来提高自己吗

    谢谢,, ief2

  • 不要用
    <0
    检查无符号类型,这毫无意义,并且会在某些编译器上引起警告
  • 不要在未指定大小的情况下使用无符号类型(
    unsigned int
    unsigned char
    等)
  • 如果
    flag==0
    为什么要将其设置为
    0
  • 我不喜欢将
    *
    抽象为
    typedef
    ,但无论如何它都没有错
  • 无需调用
    memset()
    将单个字节设置为
    0
  • 使用
    pow
    计算位偏移是疯狂的。请查看
    运算符,并改用它们
  • 将您的
    if
    语句完全插入括号中,或者为将来的调试痛苦做好准备
  • 如果在
    setBitIndex
    函数中使用位运算符
    &
    |
    而不是算术
    +
    -
    ,那么无论如何,您都不需要所有复杂的
    If
    语句
  • 您的
    GetBitAtIndex
    例程没有边界检查
    索引
  • 从这个列表中,#9是唯一一个意味着你的程序不能在所有情况下都工作的,我认为。我没有对它进行详尽的测试——这只是一个初步检查

    对于访问char对象中的位#n,可以使用移位和掩蔽,而不是使用pow()函数:

    设置位#n:



    pow(2,index)
    是产生位掩码的效率较低的方法之一。我可以想象使用Ackermann函数可能会更糟糕,但是
    pow()
    的速度非常慢。你应该使用
    (1似乎没有人提到这一点(我很惊讶),但是…你不能告诉我你在认真地执行
    malloc(sizeof(char))
    ?这是一个非常小的分配。将其作为堆分配的对象是没有意义的。只需将其声明为
    char

    如果您想进行某种程度的封装,可以执行以下操作:
    typedef char IEFBoolArray;
    并使用访问器函数操作
    IEFBoolArray
    。甚至执行
    typedef struct{char value;}IEFBoolArray;
    但是考虑到数据的大小,在堆上一次分配一个数据将是非常愚蠢的。让这种类型的使用者直接内联声明它并使用访问器


    此外……您确定要将其设置为
    char
    ?如果将其升级为更大的值,例如
    int
    ,,则生成的代码可能会稍好一些,此外,Carl Norum还指出:

  • 除非您必须(即,您存储了很多位值),否则不要以这种方式在char中节省空间。它的速度要慢得多,因为您必须执行按位操作等
  • 在大多数体系结构上,mallocing char会浪费内存。在大多数现代体系结构上,一个指针占用的内存是char的4到8倍,此外,您还拥有malloced块的数据
  • 可能静态大小不是最好的方法,因为它不灵活。我看不到使用特殊函数的任何好处
  • 从第三点开始,类似于:

    typedef struct {
        uint64_t size;
        uint64_t *array;
    }bitarray;
    
    bitarray bitarray_new(uint64_t size) {
        bitarray arr;
        arr.size = size;
        arr.array = calloc(size/8);
        return arr;
    }
    
    void bitarray_free(bitarray arr) {
        free(arr.array);
    }
    
    void bitarray_set(bitarray arr, uint64_t index, int bit) {
      assert (index <= arr.size)
      if (bit)
        array[index/8] |= 1 << (index % 8);
      else
        array[index/8] ^= ~(1 << (index % 8));
    }
    
    void bitarray_get(bitarray arr, uint64_t index, int bit) {
      assert (index <= arr.size)
      return array[index/8] & 1 << (index % 8);
    }
    
    typedef结构{
    uint64_t尺寸;
    uint64_t*阵列;
    }位阵列;
    bitarray bitarray_new(uint64_t大小){
    位阵列arr;
    arr.size=尺寸;
    arr.array=calloc(大小/8);
    返回arr;
    }
    无效位数组\u free(位数组arr){
    空闲(arr.array);
    }
    无效位数组集合(位数组arr、uint64索引、int位){
    
    assert(index)您还必须处理数组大小大于13的情况。谢谢您的指点
    a = a | (1 << n);
    
    a = a & (~(1 << n));
    
    return ((a >> n) & 1);
    
    if(result == 0)
            return 0;
        else
            return 1;
    
    newReference = malloc(sizeof(char));
    memset(newReference, 0, sizeof(char));
    
    typedef struct {
        uint64_t size;
        uint64_t *array;
    }bitarray;
    
    bitarray bitarray_new(uint64_t size) {
        bitarray arr;
        arr.size = size;
        arr.array = calloc(size/8);
        return arr;
    }
    
    void bitarray_free(bitarray arr) {
        free(arr.array);
    }
    
    void bitarray_set(bitarray arr, uint64_t index, int bit) {
      assert (index <= arr.size)
      if (bit)
        array[index/8] |= 1 << (index % 8);
      else
        array[index/8] ^= ~(1 << (index % 8));
    }
    
    void bitarray_get(bitarray arr, uint64_t index, int bit) {
      assert (index <= arr.size)
      return array[index/8] & 1 << (index % 8);
    }