# Thread: Help with pointers, structures, and malloc

1. ## Help with pointers, structures, and malloc

Hi,

This may be a really dumb question, but I am having trouble with it. I have an assignment that reads like this

"write a function void AllocVector(struct vector v, int n) that dynamically allocates a block of memory of size n*sizeof(float) for the data member of the vector argument. This function must be invoked once for each vector variable in your program.
Symmetrically, write a function void FreeVector(struct vector v) that deallocates the memory block pointed to by the data member of the vector argument. This function must be invoked for each vector allocated with AllocVector, before the program finishes executing (to avoid memory leaks). " Then I have to write functions such as sumVector etc.... I'm not to concerned with the functions, it's invoking that I'm concerned with.

So this is what I have so far
Code:
```Struct Vector
{
int size;            //global
float * data;
}

Void Alloc Vector (struct Vector *VP int n)
{
VP -> size = N;
VP -> data malloc (N *sizeof float);
}

Void FreeVector (struct Vector *VP)
{
free (VP-> data);

}

int SumVector (struct vector V)
{
int sum = 0;
int i;
for (i=0 i <v.size; i++)
{
sum + = v.data[i];
}

return sum;
}```
So my question is first.....is my code right...then if it is how and where do I call the functions to allocate and free memory? Also this code was somewhat given to us in class, not all of it, but the rest I was able to put together...so if someone could help me understand this a little better I would appreciative. 2. Very close but not correct...
Function names cannot have spaces in them.
In your allocvector function you need to allocate the struct before you write to it.

In your free function you correctly deallocate the struct's data, but not the struct itself.

Since your struct and it's data are all pointers in your sumvector function you will have to pass in a pointer to a vector struct and work with pointer notation when adding things up. 3. Hi,

Thanks...I believe, (and I am very much a newbie) the assignment calls to allocate and free the data element, not necessarily the struct itself, so that's what I was trying to do. With the SumVector should it be like this?

Code:
```int SumVector (struct vector * V)
{
int sum = 0;
int i;
for (i=0 i <v.size; i++)
{
sum + = v.data[i];
}

return sum;
}``` 4. More like
Code:
```int SumVector (struct vector * V)
{
int sum = 0;
int i;
for (i=0; i < v->size; i++)  // you forgot a semicolon; and note the '->'
{
sum += v->data[i]; // I wouldn't put a space between + and =. Also again '->'
}

return sum;
}``` 5. Hi,

Thanks a lot for the help.....now...this may be a real dumb question...how do I call the allocmemory (I know the space in the initial code needs to be removed) and the free memory function? Or are these functions I would just use in the Main? So I would apply all the functions to one variable? If that's the case if someone could provide a little bit of code of how this would look, I would be greatly appreciative. Any help at all is greatly appreciated. 6. You forgot the assignment operator. It should look like:
Code:
`VP -> data = malloc(n * sizeof float);`
The FreeVector function looks alright. 7. Ok cool thanks a lot. How would I apply these functions in the Main....if someone can give me a little code on I might use this I mean not a whole main function just how these functions would be applied. Any help is greatly appreciated. 8. Code:
```struct Vector v1; // uninitialized
const int N = 100; // or whatever
AllocVector(&v1, N);
// v1 should be initialized (but maybe not, so you should check)
if (v1->data != NULL) {
v1->data = 3.14;
}
// do other stuff
int sum = SumVect(&v1);
// blah blah
FreeVect(&v1);``` 9. Hi thanks....would someone if they have the time explain to me exactly what is going on in the function he wrote. I understand a lot of it...but if someone could expand I would be greatly appreciative. 10. Code:
```struct Vector v1; // uninitialized
const int N = 100; // or whatever
AllocVector(&v1, N);
// v1 should be initialized (but maybe not, so you should check)
if (v1->data != NULL) {
v1->data = 3.14;
}
// do other stuff
int sum = SumVect(&v1);
// blah blah
FreeVect(&v1);```
Since your functions (AllocVector for instance) take a pointer to a Vector, you have to create some space that the pointer can point at. In this case, its a local variable v1. You must pass the address of v1 to AllocVector so that it can change the data members of v1.

Remember that in C everything is pass by value. You can't modify the arguments of a function and expect to see any changes after the function returns. That's why you pass a _pointer_ to v1 inside of just a struct Vector.

Now, inside AllocVector, you are actually setting v1's data and length members. If malloc() cannot allocate enough memory, it returns NULL and v1.data will be NULL. Thus, you should check for this.

Then you fill the array v1.data with up to N floats (probably in a loop). Then call SumVect() to return the total. Don't forget to free() v1.data by calling FreeVect()

That's basically it.

Note that the design of Vector is horrible. It adds nothing beyond using a regular array. 11. Hi thanks a lot for taking the time.....man you guys are really smart! Ok so let me make sure I have this....AllocVector passes in the address of V1 and whatever value N is, and frees up the appropriate amount of space. Then I start to populate N's values like you said
v1->data = 3.14;
v1->data = 3.15;
and so on....obviously I could use a loop to set the values to whatever. Then I call my Sum Vector function passing in the address of V1 to some int, and then make sure I free up V1? I think that's it? One question..and again might be quite dumb....how would i initialize the srtuct variable? Would it just be
Code:
```Struct V1

{v.size = 8;

v.data = 7;
}```

or something like this? 12. Initialize:
Code:
```struct Vector V1 = {
{ 8, 7 }, // one value for each element in the struct
{ a, b }, // and so on for each struct element in the array
};``` Popular pages Recent additions 