Assigning a pointer in a struct to a variable

2019-06-07 07:11发布

问题:

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.

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

My given struct is:

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;
}

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.

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

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)<-----
        }
}

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!

回答1:

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

  • 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;

should be

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

  • 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(...)

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



回答2:

You want the address of i, therefore:

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

Also, when calculating the size you'll want:

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

UPDATE:

You can also pass a pointer to i into getVector and just save that in v->items

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


回答3:

I see you're allocating memory for VectorT when you should be allocating memory for 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]
}