Thread: creating structures in functions

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    46

    creating structures in functions

    I have tried two ways to create a structure in a function, and then return its starting address to the main function.


    One way is to use malloc within the function and then return the address back to main. (pt1 and testFunction1() below)

    The other way is to simply create the structure within the function and then pass the address back to main. (pt2 and testFunction2() below).


    Both options seem to work fine. However, I have an intuitive feeling that the second option is not correct (partly because the the first method is used in my textbook).
    Can someone explain to me if and why the malloc way is better?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct Test
    {
    	int i;
    };
    
    struct Test *testFunction1();
    struct Test *testFunction2();
    void changeValue(struct Test *); 
    
    int main (void)
    {
    	struct Test *pt1;
    	struct Test *pt2;
    	
    	pt1 = testFunction1();	
    	printf("%d\n", (*pt1).i);
    	
    	pt2 = testFunction2();	
    	printf("%d\n", (*pt2).i);
    	
    	changeValue(pt1);	
    	printf("%d\n", (*pt1).i);
    	
    	changeValue(pt2);	
    	printf("%d\n", (*pt2).i);
    
    	return 0;
    }
    
    struct Test *testFunction1()
    {
    	struct Test *pointer;	
    	pointer = (struct Test *) malloc( sizeof(struct Test) );
    	
    	pointer->i = 1234;	
    	return pointer;			
    }
    
    struct Test *testFunction2()
    {
    	struct Test *pointer;	
    	struct Test structure1;	
    	pointer = &structure1;
    	
    	structure1.i = 9876;	
    	return pointer;
    }
    
    void changeValue (struct Test *pointer)
    {
    	pointer->i = 55555;	
    	return;
    }
    
    /* OUTPUT
    1234
    9876
    55555
    55555
    */
    Last edited by Marksman; 06-01-2005 at 09:53 AM.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >However, I have an intuitive feeling that the second option is not correct
    You are correct, sir. Your intuition is impressive. The second option returns a pointer to a local object, which is a big no-no because the memory in which the local object resides will be released for other purposes. Chances are very good that your data will be garbage, and trying to modify it will cause an access violation.

    By using malloc, you ensure that the memory is not released when it goes out of scope and the pointer you have in main still points to memory that you own.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    46
    Great, thanks for the quick reply.

    So the memory reserved by malloc is only released by using free() or when the program terminates, and is unaffected by scope?

    Thanks.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >So the memory reserved by malloc is only released by using free()
    Yes.

    >or when the program terminates
    Bzzt! Wrong! The standard doesn't guarantee that memory returned by malloc will be implicitly freed when the program terminates. The only guaranteed way to release memory you allocate with malloc is to call free on it. However, that said, most operating systems will release the memory owned by the process when it terminates. But it's still poor practice to rely on it.

    >and is unaffected by scope?
    Correct. As long as you have a pointer to the memory, all is well.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating structures
    By HAssan in forum C Programming
    Replies: 7
    Last Post: 01-26-2009, 06:36 AM
  2. Structures and Functions
    By Extol in forum C++ Programming
    Replies: 4
    Last Post: 05-12-2003, 07:28 PM
  3. Functions and Creating Files
    By AngelEyes in forum C++ Programming
    Replies: 2
    Last Post: 05-14-2002, 06:12 PM
  4. data Structures / Hash functions
    By rickc77 in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 11-11-2001, 03:46 PM
  5. data structures / hash functions
    By rickc77 in forum C Programming
    Replies: 5
    Last Post: 11-11-2001, 01:54 PM