Thread: need help with malloc-free

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    17

    need help with malloc-free

    this is my code :
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    #define JUMLAH 1000000
    #define ISI     100
    
    typedef struct _dodol
    {
            char *string;
    } dodol;
    
    void testing(void)
    {
    	dodol **abc = (dodol**)malloc( JUMLAH * sizeof(dodol*) );
    	memset(abc, 0, JUMLAH * sizeof(dodol*));
            int i;
            char a[10];
    
            for(i=0; i<JUMLAH; i++) {
                    abc[i] = (dodol*)malloc(sizeof(dodol));
    		abc[i]->string = strdup("HALLLLLLLLLLOOOOOOOOOOOO");
            }
    
            for(i=0; i<JUMLAH; i++) {
    		free( abc[i]->string );
                    free( abc[i] );
            }
            free(abc);
    }
    
    int main()
    {
    	int i, j;
    	char a[10];
    
    	printf("ready to insert\n");
            scanf("%s",a);
    
    	j=1;
    	for(i=0; i<j; i++)
    		testing();
    
    	
     	printf("done free\n");
            scanf("%s",a);
    
            return 0;
    }
    If i run that code, it will be memory leak.
    But if i change j>1 (testing() running more than 1 time), the memory is cooled down.
    Whats wrong? >.<

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Wow, you're allocating a lot of memory... not that it matters, but consider downsizing the test data.

    * Don't cast malloc, see the FAQ
    * Check if doesn't malloc fail before continuing, it returns NULL if it does.
    * testing() will only run once as it is.
    * What exactly are you trying to do? You could allocate an array of char * pointers... which point to strings which you also allocate.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    17
    Quote Originally Posted by zacs7 View Post
    Wow, you're allocating a lot of memory... not that it matters, but consider downsizing the test data.

    * Don't cast malloc, see the FAQ
    * Check if doesn't malloc fail before continuing, it returns NULL if it does.
    * testing() will only run once as it is.
    * What exactly are you trying to do? You could allocate an array of char * pointers... which point to strings which you also allocate.
    what i'm trying to do is to test why after i run testing() for 1 time, the memory is not going down (i use top to see my memory). It's supposed to be freed.
    But if i run testing() more than 1 time (try to change j>1), the memory is freed like it supposed to be.

    Thx

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by sleith View Post
    If i run that code, it will be memory leak.
    But if i change j>1 (testing() running more than 1 time), the memory is cooled down.
    Whats wrong? >.<
    Nothing is wrong. Just because you free() some memory does NOT mean it is returned to the system. It just makes it available for future allocation by malloc().

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    17
    Quote Originally Posted by brewbuck View Post
    Nothing is wrong. Just because you free() some memory does NOT mean it is returned to the system. It just makes it available for future allocation by malloc().
    Hm..ok2 i undestand now. Thx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory leaks problem in C -- Help please
    By Amely in forum C Programming
    Replies: 14
    Last Post: 05-21-2008, 11:16 AM
  2. Function to check memory left from malloc and free?
    By Lechx in forum C Programming
    Replies: 4
    Last Post: 04-24-2006, 05:45 AM
  3. String malloc & free?
    By The Tracker in forum C Programming
    Replies: 2
    Last Post: 05-22-2002, 10:45 PM
  4. Ask about free funtion using with malloc
    By ooosawaddee3 in forum C++ Programming
    Replies: 1
    Last Post: 05-12-2002, 04:43 PM
  5. Malloc and Free.....
    By heljy in forum C Programming
    Replies: 5
    Last Post: 04-14-2002, 09:17 PM