【问题标题】:copying void pointer to another void pointer?将 void 指针复制到另一个 void 指针?
【发布时间】:2019-09-27 07:25:58
【问题描述】:

我正在尝试将一个 void 指针复制到另一个指针数组的不同索引,对于字符来说一切正常,但对于整数和双精度却出现问题

这是我的结构:

typedef struct vector_struct {
 size_t e_sz;
 char e_type;

 #define V_INT 1 
 #define V_DOUBLE 2
 #define V_CHAR 3

 unsigned no_e;
 unsigned cur_cap;
 void* e_array;

}* vector_type;
 //for typecasting
  #define vector_int_at(v,i) (*((int*)v_at(v,i)))
  #define vector_double_at(v,i) (*((double*)v_at(v,i)))
  #define vector_char_at(v,i) (*((char*)v_at(v,i)))

  void* v_at(vector_type v, iterator i) {
    if ( v == NULL )
     fail("v_at() Error: no such vector");
    if ( i == iterator_end )
     i = v->no_e -1;
    if ( i < 0 || i >= v->no_e )
     fail("v_at() Error: no such element in this vector");
    return (v->e_array+i); //return element
   }

我想将值复制到 void* e_array 中,它可以是整数、字符或双精度值。

void v_push_back(vector_type v, void* new_val){
 if ( v->no_e >= v->cur_cap ) {
    /*** need to expand the array ***/
    v->cur_cap += (v->cur_cap)? v->cur_cap : 2;

    v->e_array = check_a(realloc(v->e_array, sizeof(void*) * v->cur_cap));
    //allocate memory

   }
/*** copy new_val into the array at end position (v->no_e) ***/
   printf("%d  %d %p\n",v->e_sz,v->no_e,v->e_array+(v->no_e *v->e_sz));
    memcpy(
          v->e_array +(v->no_e)*v->e_sz,
          new_val,
          v->e_sz
          );

 (v->no_e)++;
}

我这样调用这个函数:

c1 = 'o'; v_push_back(vc1,(void*)&c1); //for character
for ( i1 = 0 ; i1 < 10 ; i1++ ){ //for integer
    v_push_back(vi1,(void*)&i1);
}

//I call these like
  for ( i1 = 0 ; i1 < vector_size(vi1) ; i1++ )
   printf(" %ld",vector_int_at(vi1,i1));
  printf("\n");

但似乎事情不适用于整数,但适用于字符但对于整数垃圾值被复制如下所示: enter image description here

请帮帮我。谢谢

这里是完整的项目: https://s3.amazonaws.com/assignment.pointers.vectorc.implementation/C-VectorImplementation1.zip

【问题讨论】:

  • 阅读的时候投e_array吗?如果是怎么办?
  • @FaizanShah:不要将代码放入 cmets。编辑问题以包含minimal reproducible example
  • 请提供minimal reproducible example(强调完成)。可以复制/粘贴和编译的东西。
  • 链接的 ZIP 文件中的代码与问题中的代码不匹配。在 ZIP 文件中,v_push_back 包含 if(v-&gt;e_type==V_INT){…。在问题中,它没有。显然,人们永远不应该期望通过询问代码 Y 来得到代码 X 不起作用的正确答案。

标签: c pointers malloc heap-memory void-pointers


【解决方案1】:

我看到的问题很少,

  1. 为了获得当前向量类型的第 i 个位置,您需要将 iv-&gt;e_sz 相乘。

    void* v_at(vector_type v, iterator i)
    {
        .....
        return ((char*)v->e_array+i*v->e_sz); //return element    
    }
    
  2. 你正在分配sizeof(void *)你想要的是

     v->e_array = check_a(realloc(v->e_array, v->e_sz * v->cur_cap));
    
  3. 到目前为止,您将地址存储为int,但您需要存储其值。

    void v_push_back(vector_type v, void* new_val){
         if(v->e_type==V_INT){
            //  (v->e_array+(v->no_e))=new_val;
            ((int*)v->e_array)[v->no_e]=*(int*)new_val;
                                        ^------storing its content
         }
    }
    

    没有任何类型检查,简单的memcpy 应该可以工作。

    void v_push_back(vector_type v, void* new_val){
        ....
        memcpy(
              v->e_array +(v->no_e)*v->e_sz,
              new_val,
              v->e_sz
              );
           //   *(char*)(v->e_array+v->no_e)=*(char*)new_val;
    
         (v->no_e)++;
     }
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-30
    • 2013-12-18
    • 2021-07-22
    • 2014-05-13
    • 2020-12-18
    • 2020-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多