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