Thread: Memory allocation problem

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    5

    Memory allocation problem

    Hi all,

    I have a little problem with memory allocation.

    At first, just have a look in my code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    //Free memory
    void end(int *a)
    {
    	if(a != NULL)
    	{
    		free(a);
    		a = NULL;
    	}
    	
    }
    
    //Fill random number into array
    void fill(int *a, int n)
    {
    	int i;	
    	
    	for ( i  = 0; i < n; i++)
    	{
    		a[i] = rand()%19 -9;
    	}
    }
    
    //Display array
    void display(int *a,int n)
    {
    	int i;
    	for (i = 0; i < n; i++)
    	{
    		printf("%d\t", a[i]);
    	}
    }
    
    
    void main()
    {
    
    	int *a = NULL;
    	int n = 10;//Element size
    
    	if ((a = calloc(n, sizeof(int))) == NULL) 
    	{
    		printf("Memory can not allocate");
    		exit(-1);
    	}
    	fill(a,n);
    	display(a,n);
    	end(a);
    }
    You can see that it runs perfectly.
    But if I make a little changes in fill function, my program will be crashed.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    //Free memory
    void end(int *a)
    {
    	if(a != NULL)
    	{
    		free(a);
    		a = NULL;
    	}
    	
    }
    
    //Fill random number into array
    void fill(int *a, int n)
    {
    	int i;	
    
    	if ((a = calloc(n, sizeof(int))) == NULL) 
    	{
    		printf("Memory can not allocate");
    		exit(-1);
    	}
    	
    	for ( i  = 0; i < n; i++)
    	{
    		a[i] = rand()%19 -9;
    	}
    }
    
    //Display array
    void display(int *a,int n)
    {
    	int i;
    	for (i = 0; i < n; i++)
    	{
    		printf("%d\t", a[i]);
    	}
    }
    
    
    void main()
    {
    
    	int *a = NULL;
    	int n = 10;//Element size
    
    
    	fill(a,n);
    	display(a,n);
    	end(a);
    }
    My questions is:
    1. Why is my program crashed ?
    2. I see that many programs which use the global variables to store pointer, so they can write initial function and de-allocated function separately. If I do not want to use global variables, how can I initiate and free memory by passing the pointer ?

    Thank you.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I believe Quzah's post has answered your questions, here:

    Dynamic string input

    If not, post back.

    And Welcome to the forum, Ariko!

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    5
    Quote Originally Posted by Adak View Post
    I believe Quzah's post has answered your questions, here:

    Dynamic string input

    If not, post back.

    And Welcome to the forum, Ariko!
    Thanks for your reply.

    I solved my problems based on Quzah's post.

    Code:
    void init(int **a, int n)
    {
    	if ((*a = calloc(n, sizeof(int))) == NULL) 
    	{
    		printf("Memory can not allocate");
    		exit(-1);
    	}
    }
    Code:
    void main()
    {
    
    	int *a = NULL;
    	int n = 10;//Element size
    
    	init(&a, n);
    	fill(a,n);
    	display(a,n);
    	end(a);
    }

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Do not ever use void main: SourceForge.net: Void main - cpwiki
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    5
    Quote Originally Posted by Elysia View Post
    Do not ever use void main: SourceForge.net: Void main - cpwiki
    Thanks. I will avoid it.

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    You end() function will not set its argument to NULL.

    Code:
    void end(int *a)
    {
    	if(a != NULL)
    	{
    		free(a);
    		a = NULL;         // not change in caller! 
    	}
    	
    }

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Bayint Naung
    You end() function will not set its argument to NULL.
    Indeed. You might as well dispense with it, and just call free(a); in the main function. Checking for a null pointer is unnecessary here.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jul 2010
    Posts
    5
    Quote Originally Posted by Bayint Naung View Post
    You end() function will not set its argument to NULL.
    Thanks for your reply.
    I forget to check this function.
    I think this is better.
    Code:
    void end(int **a)
    {
    	if(*a != NULL)
    	{
    		free(*a);
    		*a = NULL;   
    	}
    	
    }

    Quote Originally Posted by laserlight View Post
    Indeed. You might as well dispense with it, and just call free(a); in the main function. Checking for a null pointer is unnecessary here.
    Thanks for your suggestion. In some complicated programs, I realize that they write the initiate and free function separately, but they use global pointer. I can not use global variables in my program, I must included an unallocated output buffer in the prototype of my function. Then, malloc and free in another functions.
    Last edited by ariko; 07-24-2010 at 08:13 AM.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    In complicated programs, they most certainly would not use global variables. You are doing it the correct way.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Thanks for your suggestion. In some complicated programs, I realize that they write the initiate and free function separately, but they use global pointer. I can not use global variables in my program, I must included an unallocated output buffer in the prototype of my function. Then, malloc and free in another functions.
    Laserlight did not mention anything about global variables. Your end() function is an abstraction of free, that does nothing other than free one thing. Just use free!

  11. #11
    Registered User
    Join Date
    Jul 2010
    Posts
    5
    Quote Originally Posted by whiteflags View Post
    Laserlight did not mention anything about global variables. Your end() function is an abstraction of free, that does nothing other than free one thing. Just use free!
    Laserlight suggest that:
    just call free(a); in the main function
    I don't want to do this because I have to separate malloc/calloc and free into different function.

    But Laserlight is right about
    Checking for a null pointer is unnecessary here.
    This is my new code:

    Code:
    int end(int **a)
    {
    	free(*a);
    	*a = NULL;
    }
    
    int main()
    {
    
    	int *a = NULL;
    	int n = 10;//Element size
    
    	init(&a, n);
    ...
    
    	end(&a);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with custom dynamic memory allocation routines
    By BLauritson in forum C++ Programming
    Replies: 12
    Last Post: 03-11-2010, 07:26 AM
  2. Problem with memory allocation
    By soulwarrior89 in forum C Programming
    Replies: 23
    Last Post: 07-14-2009, 03:17 PM
  3. Problem with linked list and shared memory
    By Sirfabius in forum C Programming
    Replies: 10
    Last Post: 11-10-2008, 04:45 PM
  4. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  5. Memory Problem - I think...
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 10-24-2001, 12:14 PM