Hi Guys, it's me again! I have been posing memory-related problems for the past few days because I don't seem to understand what is the problem with my code. I kind of figured it's due to memory issues....so here's my question.
Suppose I have a (first in first out) queue that contain "Items", which is a structure previously defined:
void QUEUEinit(int); /* initiates queue */
static Item *q;
static int N, head, tail;
void QUEUEinit(int maxN)
{ q=malloc((maxN+1)*sizeof(Item));
if(q){
N=maxN+1; head = N; tail = 0;}else{printf("memory allocation error.\n");}
}
So every time in my main program, whenever I call
QUEUEinit(50);
the system sets aside memory, say of amount X, for my queue. Suppose (just for testing purposes) if I do this:
for(i=0; i<1000; i++)
QUEUEinit(50);
does this mean that a total of 1000X amount of memory will be used? If this is the case, very soon (by increasing i to 2000, or more) my system is going to run out of memory and I am going to get a "memory allocation error" message.
What if, instead I had
for(i=0; i<1000; i++)
{QUEUEinit(50); free(q); q=NULL;}
Does this mean that every time QUEUEinit() is called, the memory set aside for it will be freed before the next i, so I will never run out of memory?
Sorry if this is a little confusing, hope it is understandable and any help will be appreciated.