Thread: Freeing memmory - Automatic?

  1. #1
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    Freeing memmory - Automatic?

    Hello, long time no questions, and the site was down yesterday My question will be simple today, as always haha. Hmm, I have the follow code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define SIZE 10
    
    void *pToData[100] = {NULL}; //pointer to all the data's to be free( )'ed
    
    static void addToList( void *data ) {
    	static int i = 0;
    
    	pToData[i++] = data;
    }
    
    
    static int *createArray( void ) {
    	int *array = malloc(SIZE * sizeof( int ) );
    	int i;
    
    	//check to see if we gain the memmory
    	if( array != NULL ) {
    		for( i=0; i<SIZE; i++ )
    			array[i] = i;
    	}
    
    	else {
    		printf( "Could not create array!\n" );
    		exit( 1 );
    	}
    	addToList(array);
    	return array;
    }
    
    static printArr( int *arr, char *label ) {
    	int i;
    
    	puts( label );
    	for( i=0; i<SIZE; i++ )
    		printf( "%d ", arr[i] );
    }
    
    static void freeAll( void ) {
    	int i;
    
    	for( i=0; pToData[i] != NULL; i++ )
    		free( pToData[i] );
    }
    
    int main( void ) {
    	int *arr1, *arr2;
    
    	arr1 = createArray();
    	arr2 = createArray();
    
    	printArr( arr1, "Array 1: " );
    	printArr( arr2, "\n\nArray 2:" );
    
    	freeAll(); //is this really works? so easy?
    
    	return 0;
    }
    As you can imagine, I'm trying to make a function that will free all the memmory I allocated by using the function createArray(); Is this really free's the memmory? If so, I'll rewrite the malloc function with some help, and then make a part that always if I malloc'ed some memmory it calls addToList() and then at the end of the program I don't need to free() every single thing, I just call freeAll and it free's for me, of course, if this works
    Last edited by Vber; 04-22-2003 at 07:24 PM.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    23
    Yeah that will work

    side note:

    you probably need to change the following line
    Code:
    int *array = malloc(SIZE * sizeof( int ) );
    to
    Code:
    int *array = (int*)malloc(SIZE * sizeof( int ) );
    you're declaring a int *, malloc return a void *...
    Merc

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Mercury_Linx
    Yeah that will work
    Code:
    int *array = (int*)malloc(SIZE * sizeof( int ) );
    you're declaring a int *, malloc return a void *...
    No. You never need to type cast malloc. It is pointless. Search the forums if you're bored, for lengthy debates on why or why not to, but in short, there is no need. (Unless you're using C++, in which case, you're in the wrong forum .)

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    That may look useful at first glance, and not that it's bad code or anything, but just not a good solution to the problem. I would suggest using a simple linked list. Something that would be used like:

    Code:
    int main()
    {
     memory_manager mm;
     mminit(&mm);
     int * i = mmalloc(&mm, 100*sizeof(int));
     //...
     mmfree(&mm); 
    }
    This could be used across many programs without a single static variable - and without some of the other, more glaring limitations, as well. IMO - back to the drawing board...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Thanks for the answers, and Sebastiani, I tought about what you wrote, and I'll make it in your way, I was just checking if this idea would work

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Freeing a char**
    By +Azazel+ in forum C Programming
    Replies: 3
    Last Post: 02-26-2008, 07:27 PM
  2. why automatic variables are assigned with garbage values
    By srinivasg in forum C Programming
    Replies: 1
    Last Post: 11-08-2005, 07:14 AM
  3. automatic variables
    By FOOTOO in forum C Programming
    Replies: 5
    Last Post: 03-08-2005, 06:30 PM
  4. Automatic vs. Static Duration
    By JMB in forum C++ Programming
    Replies: 7
    Last Post: 10-14-2002, 07:16 PM
  5. Automatic Time in C++Builder ??
    By Gugge in forum Windows Programming
    Replies: 0
    Last Post: 06-18-2002, 12:58 PM