Using malloc and free wrappers?

It is not necessary to call malloc and free in the same function, but I have some remarks on your code.

Up vote 1 down vote favorite 1 share g+ share fb share tw.

Please suggest if there is a better way of implementing the below. Int main(int argc, char ** argv) { int * arr = {0}; foo(); return 1; } void mymalloc(int ** myarr1 ) { (*myarr1) = (int*) malloc( sizeof(int)*25 ); (*myarr1)3 = 69; } void myfree(int ** myarr2 ) { if (*myarr2) { memset(*myarr2, 0, sizeof(int)*25 ); free(*myarr2); } void foo() { int * arr1 = {0}; mymalloc(&arr1); printf("car3=%d\n",arr13); myfree (&arr1); // Check if memory was freed if (arr1) { printf("ERROR: Memory allocated to arr1 is not freed. "); exit (1); } } Output: ERROR: Memory allocated to arr1 is not freed.

C malloc free link|improve this question edited Feb 4 '11 at 23:37 asked Feb 4 '11 at 20:05StackOverflow336 88% accept rate.

Test. C:20: error: ‘arr’ undeclared (first use in this function). – ulidtko Feb 4 '11 at 20:12 @ulidtko: Thanks.

Corrected now. I just typed in the code to give an idea of what I am trying to do. – StackOverflow Feb 4 '11 at 20:22 1 BTW, unless you are writing secure code, there is no need to clear memory before using free.

– Thomas Matthews Feb 4 '11 at 20:33 Nice user name. – Platinum Azure Feb 4 '11 at 21:57.

The memset in myfree is useless, the pointer is invalidated anyway. Assigning {0} to the pointers is useless, because malloc will overwrite these anyway.

Patrik: Since I will be calling malloc several times in my code, I wanted to keep my main() clean and short, hence separate functions. And my compiler chokes if I don't initialize variables. I updated code with different variable names to be clear about my question.

Now, does the above code free the memory allocated to arr1 in foo? Or since the malloc was done on myarr1 in mymalloc, do I need to free myarr1? – StackOverflow Feb 4 '11 at 20:49 The post has changed but memsetting memory to some marker value BEFORE freeing it can be valuable for chasing down overrun bugs.

– Martin Beckett Feb 4 '11 at 20:51 @Martin: Thanks Martin. That was the intent I had it there initially. I just removed it so folks can focus on my main question, which I mentioned in response to @Patrik's remarks.

– StackOverflow Feb 4 '11 at 20:55 Yes, your code will free arr1. Arr1 and *myarr1 point to the same memory location. – Patrik Feb 4 '11 at 21:02 @Patrik: Thanks for the clarification.

Ignore my above (late) question. Now that you confirmed that the above code works as expected, I need to figure out the real reason for memory not being freed up in my code. – StackOverflow Feb 4 '11 at 21:11.

You can free anything you have created with malloc anywhere in the program. Memory allocated with malloc comes from the "heap" section of memory, and will persist throughout the lifetime of a program unless released with free.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions