【问题标题】:Assigning a pointer in a struct to a variable将结构中的指针分配给变量
【发布时间】:2013-06-13 01:50:09
【问题描述】:

这个程序应该创建一个动态内存向量。我很确定我正确使用了 malloc。我真正的问题是一些带有指针的语法,特别是结构内的指针。

我正在尝试访问结构内的 int 指针的地址,以便将其分配给另一个指针

我给定的结构是:

typedef struct{
int *items;
int capacity;
int size;
}VectorT;

我要开始工作的功能是:

int getVector(VectorT *v, int index){
    int *p;
    p = v->items;//(2)
    p -= v->size;
    p += index;
    return *p;
}

这应该是项目指针的地址减去列表中的项目数,并将所需项目的索引添加到 p 的地址。然后我返回 p 的地址。

我有一种强烈的感觉,第 (2) 行不是我需要的语法。

根据我到目前为止的尝试,我的程序要么在调用 getVector 时崩溃,要么输出(我的最佳猜测)一些内存位置。

这是添加向量的代码:

void addVector(VectorT *v, int i){
        if(v->size >= v->capacity){
            //allocate twice as much as old vector and set old pointer to new address
            v = (VectorT *) malloc(2 * v->capacity * sizeof(VectorT));
            if(v == NULL){
                fprintf(stderr, "Memory allocation failed!\n");//error catch
            }
            else{
                v->capacity *= 2;//double the reported capacity variable
                v->size++;//add one to the reported size variable
                v->items =(int *) i;//add the item to the vector (A)<-----
            }   
        }
        else{
            v->size++;//add one to the reported size variable
            v->items =(int *) i;//add the item to the vector (B)<-----
        }
}

我不觉得我的问题出在这里,但如果是这样的话,我对 A 线和 B 线有些怀疑......

任何见解将不胜感激,谢谢!

【问题讨论】:

  • 不要强制转换malloc的返回值!
  • 是 v 应该包含多个 VectorT 还是 VectorT.items 应该包含多个整数?
  • VectorT.items 包含多个整数

标签: c pointers vector struct pointer-address


【解决方案1】:

至少在这些地方你对指针的处理是错误的:

  • 带有注释“将项目添加到向量”的代码非常错误:它没有添加项目,而是使用任意int 覆盖指针。

v-&gt;items =(int *) i;

应该是

*(v->items) = i;
  • 您的指针算法不正确:减去大小并添加索引将使您在分配区域的开始之前获得一个指针,这是不正确的。

  • 您将malloc 的结果分配给“指向向量的指针”类型的局部变量v。这个赋值对调用者没有影响,因为指针是按值传递的。如果您想重新评估addVector 中的向量,您应该将VectorT **pv 作为第一个参数。此代码片段看起来根本不对:看来您应该分配 v-&gt;items=malloc(2 * v-&gt;capacity * sizeof(int)) 而不是 v=malloc(...)

  • malloc 没有释放旧向量,导致内存泄漏。

【讨论】:

  • 谢谢,*(v->items) = i 看起来不错。我应该如何正确减去大小并添加索引?
  • @user1496866 通常,指针items 指向开始,而不是指向分配给向量的内存间隔的结尾。您不需要减去大小,只需添加索引。请参阅对第三点的编辑:看起来您正在为错误的事物分配内存。
  • 我需要让指针指向列表中的最后一个元素。 v->items++ 会增加项目指针吗?
  • @user1496866 不要忘记将v-&gt;items 指向的内容复制到新分配的区域,并释放旧指针。 Here 是一个不错的主题教程。
【解决方案2】:

你想要 i 的地址,因此:

v->items =&i;//add the item to the vector (A)<-----

另外,在计算你想要的尺寸时:

p -= (v->size*sizeof(int));

更新:

您也可以将指向 i 的指针传递给 getVector 并将其保存在 v-&gt;items

 int getVector(VectorT *v, int *index)
 //...
 v->items = i;

【讨论】:

  • 我是否存储在函数退出后内容不会被清除的内存位置?我的理解是,如果我把地址放到 v->items 中,函数结束后 &i 处的变量可能就不是 i 了。不过我可能是错的。
  • 你是对的。在这种情况下,您需要将 i 作为指针传递给 addVector,然后只需执行 v-&gt;items = i
  • 我希望我能,我的教授,提供函数定义。我真的没有看到另一种方法来做到这一点。我会继续尝试 &i 和 (int *) i 其中一种方法可能会奏效。但是,我认为除了该部分之外我还有一些问题。即使使用 sizeof 仍然会得到一些时髦的输出..
  • 啊,这是作业!好的,看起来int *items 应该是一个整数数组,而您分配错误。您需要将值复制到数组的末尾,而不是仅仅更改 *items,因为那是数组的起始指针
【解决方案3】:

我看到您正在为 VectorT 分配内存,而您应该为 VectorT.items 分配内存

void addVector(VectorT *v, int i){
        if(v->size >= v->capacity){
            //allocate twice as much as old vector and set old pointer to new address
            v->items
            int* tmp = malloc(2 * v->capacity * sizeof(int));
            if(tmp == NULL){
                fprintf(stderr, "Memory allocation failed!\n");//error catch
            }
            else{
                int j;
                for (j = 0; j < v->size; j++){
                    tmp[j] = v->items[j];
                }
                free(v->items);
                v->items = tmp;
                v->capacity *= 2;//double the reported capacity variable
                v->items[v->size] = i;//add the item to the vector (A)<-----
                v->size++;//add one to the reported size variable
            }   
        }
        else{
            v->items[v->size] = i;//add the item to the vector (B)<-----
            v->size++;//add one to the reported size variable
        }
}

int getVector(VectorT *v, int index){
    return v->items[index]
}

【讨论】:

猜你喜欢
  • 2013-01-21
  • 1970-01-01
  • 2011-06-18
  • 2018-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-26
  • 1970-01-01
相关资源
最近更新 更多