Deallocating memory in main function that was malloc'd elsewhere.

This is a discussion on Deallocating memory in main function that was malloc'd elsewhere. within the C Programming forums, part of the General Programming Boards category; The instructions for the program state that "The main program should deallocate the returned array when it's finished using it." ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    98

    Deallocating memory in main function that was malloc'd elsewhere.

    The instructions for the program state that "The main program should deallocate the returned array when it's finished using it."

    Which is slightly ambiguous, the function actually returns a pointer that points to the first element of an array that the function creates.

    My main:

    Code:
    int main (void)
    {
    	int c[] = {2,4,4,6};
    
    	printf("Reverse 2 returns: %i, which is the value of the pointer\n" 
    	"that points to the first element in a new array containing the\n" 
    	"elements of array c in reverse.\n", reverse2(c, 4));
    	printf("The dereferenced value it points to is: %i\n", *reverse2(c, 4));
    	free(temp);
    	system("pause");
    }
    My reverse2 function:

    Code:
    int* reverse2 (const int a[], int size)
    {
    	int i;
    	int j=0;
    	int* temp;
    	temp=(int*)malloc(size*sizeof(int));
    	int* ptr=temp;
    
    	for (i=size-1; i>=0; --i)
    	{
    		temp[j]=a[i];
    		++j;
    	}
    	return ptr;
    }
    That returned an error:
    error C2065: 'temp' : undeclared identifier

    The program itself runs fine-- if I don't free the memory-- returning all the correct values. But if I want to free up the memory used to assign that new array and such it errors.

    So I'm not sure how to de-allocate that memory (besides freeing it inside the reverse2 function but then the ptr is returned in the main function pointing to garbage) in the main function.

    I tried a few different approaches but it just brain damaged the comp

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You need to do something like
    Code:
    int *bob = reverse2(c,4)
    Every time you call reverse2 without storing the pointer somewhere, you immediately "leak" that memory that was allocated in the function.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    98
    Quote Originally Posted by tabstop View Post
    You need to do something like
    Code:
    int *bob = reverse2(c,4)
    Every time you call reverse2 without storing the pointer somewhere, you immediately "leak" that memory that was allocated in the function.
    Hmm, and free(bob) will also end up freeing temp in reverse2?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    There is no such thing as "temp in reverse2". By the time we get here, all knowledge of reverse2 is gone. If you mean "the memory that was allocated and stored into temp in reverse2", then yes.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    98
    Quote Originally Posted by tabstop View Post
    There is no such thing as "temp in reverse2". By the time we get here, all knowledge of reverse2 is gone. If you mean "the memory that was allocated and stored into temp in reverse2", then yes.
    Yes that is what I meant, thanks

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    8
    As long as I see, you defining "tmp" INSIDE reverse2. Remember that (in C and almost all languages) when you define a variable 'foo' inside a subprocess, the scope of that variable is just that subprocess and it's children.

    So, the parent of reverse2 don't see anything named "tmp", because "tmp" is locally defined in reverse2. A correct way to do what you want to do, I think, is this: (as pointed by tabstop)


    Code:
    int main (void)
    {
    	int c[] = {2,4,4,6};
    	int* foo = reverse2(c, 4);
    
    	printf("Reverse 2 returns: %p, which is the value of the pointer\n" 
    	"that points to the first element in a new array containing the\n" 
    	"elements of array c in reverse.\n", foo);
    	printf("The dereferenced value it points to is: %i\n", *foo);
    	free(foo);
    	system("pause");
    }
    Last edited by ninboy; 10-15-2008 at 05:02 PM.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,033
    Quote Originally Posted by ninboy View Post
    Code:
    	printf("Reverse 2 returns: %i, which is the value of the pointer\n" 
    	"that points to the first element in a new array containing the\n" 
    	"elements of array c in reverse.\n", foo);
    }
    Use %p for pointers. %i will result in errors if sizeof(void *) != sizeof(int). Also the behavior is undefined regardless.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Malloc and free works with memory addresses. Malloc allocates a range and returns the address, which is stored in temp.
    You return this value which can then be stored inside another variable. You then call free on that variable (pointer), which contains the original address that malloc returned.
    That's how it works.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Replies: 18
    Last Post: 12-31-2005, 12:56 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21