-3

I need to free some memory allocated on my program. Can I use something to clean the memory when I need it ?

#include<stdio.h>
#include<stdlib.h>
#define MAXROW 3
#define MAXCOL 4

int main()
{
    int **p, i, j;
    p = (int **) malloc(MAXROW * sizeof(int*));
    return 0;
}
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • 1
    It's not clear what you are asking. To free the memory just call `free(p)`. But if you are asking whether C has native garbage collection then the answer is no. – kaylum May 19 '15 at 06:06
  • Was a clear question for a new one. Don't fear him mr @AlanAu – gtzinos May 19 '15 at 06:23
  • Use correct types and don't cast the result of malloc: `int* (*p)[MAXROW] = malloc (MAXROW * sizeof(int*));`. – Lundin May 19 '15 at 06:26
  • @Lundin and when calling `sizeof` in `malloc` (and the like) [you should always write it](http://stackoverflow.com/a/17258659/1151654) as `ptr = malloc(sizeof(*ptr) * ...);` instead of `ptr = malloc(sizeof(ptrtype*) * ...);`. – Eregrith May 19 '15 at 06:57
  • @Eregrith Which will only work if you declare the pointer as an array pointer. However, what you propose is rather a subjective matter of coding style, there is no obvious benefit of using either form. I have heard the arguments for both styles numerous times, no need to repeat them. There is a sound rationale for either style. Just document which one you pick in your coding style guide. – Lundin May 19 '15 at 08:29
  • @Lundin What do you mean by *it will only work if you declare the pointer as an array pointer*? – Eregrith May 19 '15 at 08:33
  • @Eregrith Ah, I meant that rather than `ptr = malloc(sizeof(*ptr) * ...);` or something else, you can write `ptr = malloc(sizeof(*ptr));` if you are using the proper pointer type. [See this](http://stackoverflow.com/a/30117625/584518). – Lundin May 19 '15 at 08:37
  • @Lundin I see. But, in your linked answer, is it also correct to write `int (*array2d)[Y] = malloc(sizeof(*array2d) * X);`? – Eregrith May 19 '15 at 08:42
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/78160/discussion-between-eregrith-and-lundin). – Eregrith May 19 '15 at 09:10

2 Answers2

5

Point 1

You cannot free some memory. You have to free all. To elaborate, whatever memory has been allocated by a single call to malloc() or family, will be free-d at a time. You cannot free half (or so) of the allocated memory.

Point 2

  • Please do not cast the return value of malloc() and family in C.
  • You should always write sizeof(*ptr) instead of sizeof(type*).

Point 3

You can use the free() to free the allocated memory.

for example, see the below code and please notice the inline comments.

#include<stdio.h>
#include<stdlib.h>
#define MAXROW 3
#define MAXCOL 4

int main(void)                        //notice the signature of main
{
    int **p = NULL;                      //always initialize local variables
    int i = 0, j = 0;

    p = malloc(MAXROW * sizeof(*p));  // do not cast and use sizeof(*p)
    if (p)                            //continue only if malloc is a success
    {

        //do something
        //do something more

        free(p);                      //-----------> freeing the memory here.
    }
    return 0;
}
Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

It's simple. You can use this code to clean your only used variable.

#include<stdio.h>
#include<stdlib.h>
#define MAXROW 3
#define MAXCOL 4

int main()
{
     int **p, i, j;
     p = (int **) malloc(MAXROW * sizeof(int*)); 
     free(p);
     return 0;
}
gtzinos
  • 1,205
  • 15
  • 27
  • 5
    `But you dont need it now cause your code is small.`...bad advice. – Sourav Ghosh May 19 '15 at 06:06
  • I mean for this program he dont need to free his memory.Its only 2 lines program. I didn't advice him for a bigger one. – gtzinos May 19 '15 at 06:08
  • 5
    No, that's the not the correct approach, IMHO. You __should__ free() what you allocate, regardless of the LoC. – Sourav Ghosh May 19 '15 at 06:12
  • Yes i am sure you are right now but i just thought that was just 2 lines. Thank you very much for your advice i will edit it again. – gtzinos May 19 '15 at 06:15
  • 4
    see, my point is, don't make `free()`ing the dynamically allocated memory a job, make it a habit. will help in long run. – Sourav Ghosh May 19 '15 at 06:16
  • I feel free cause i use java programming mainly and this language have the garbage collector. Cause of this i don't think more to clean my memory. But you gave me a good advice to learn more thinks. – gtzinos May 19 '15 at 06:21