在结构中将指针分配给变量 [英] Assigning a pointer in a struct to a variable

查看:98
本文介绍了在结构中将指针分配给变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该程序应创建一个动态内存向量.我很确定我正确使用了malloc.我真正的问题是指针的某些语法,尤其是结构内部的指针.

This program is supposed to create a dynamic memory vector. I'm pretty sure I'm using malloc correctly. My real problem is some syntax with pointers, particularly a pointer inside a struct.

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

I'm trying to access the address of an int pointer inside a struct so I can assign it to another pointer

我给定的结构是:

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

我要开始使用的功能是:

and the function I'm trying to get to work is:

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

这应该采用项目指针的地址减去列表中项目的数量,然后将所需项目的索引添加到p的地址.然后我返回p地址处的内容.

This is supposed to take the address of the items pointer subtract the number of items in the list and add the index of the desired item to the address of p. Then I return what is at the address of p.

我非常强烈地认为第(2)行不是我需要的语法.

I have a pretty strong feeling that line (2) is not the syntax I need.

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

Depending on what I've tried so far my program either crashes when getVector is called or it outputs (my best guess) some memory locations.

以下是添加向量的代码:

Here's the code that adds a vector:

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 ...

I don't feel like my problem is in here, but if it is I have some suspicion at lines A & B...

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

Any insight would be much appreciated, Thanks!

推荐答案

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

Your dealing with pointers is wrong in at least these places:

  • 带有注释将项目添加到向量"的代码是非常错误的:不是添加项目,而是使用任意int覆盖指针.
  • The code with the comment "add the item to the vector" is very wrong: instead of adding an item, it overrides the pointer with an arbitrary int.

v->items =(int *) i;

应该是

*(v->items) = i;

  • 您的指针算法不正确:减去大小并添加索引将使您在分配的区域的开始之前获得一个指针,这是不正确的.

    • Your pointer arithmetic is incorrect: subtracting the size and adding an index will get you a pointer prior to the beginning of the allocated area, which is not correct.

      您正在将malloc的结果分配给类型为指向矢量的指针"的局部变量v.该分配在调用方中无效,因为指针是通过值传递的.如果要重新确定addVector中的向量,则应将VectorT **pv作为第一个参数.该代码片段看起来并不正确:看来您应该分配v->items=malloc(2 * v->capacity * sizeof(int))而不是v=malloc(...)

      You are assigning the results of malloc to a local variable v of type "pointer to vector". This assignment has no effect in the caller, because pointers are passed by value. If you wanted to re-assing the vector in the addVector, you should have taken VectorT **pv as the first parameter. This code fragment does not look right at all: it appears that you should be assigning v->items=malloc(2 * v->capacity * sizeof(int)) instead of v=malloc(...)

      执行malloc时不会释放旧的向量,从而导致内存泄漏.

      You do not free the old vector when you do a malloc, causing a memory leak.

      这篇关于在结构中将指针分配给变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆