Thread: free() Memory Question

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    38

    free() Memory Question

    Hello to all.

    Is this program licking memory?


    Code:
    int main()
    {
          while(i<100)
          {
                ...
                printf("Example &#37;s\n",function());
                ...
                i++;
           }
    
          return 0;
    }
    
    char* function ()
    {
           char* data;
           ...
           data = "memory";
           ....
           return data;
    }

    Thanks!
    Last edited by guillermoh; 02-06-2008 at 10:40 AM.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,337
    No.

    If it were to actually compile and run, it would only be using the program stack.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    isn't 100,000 out of bounds for an integer? thought it was 16,000, unless i'm the dinosaur;->

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,337
    Quote Originally Posted by rogster001 View Post
    isn't 100,000 out of bounds for an integer? thought it was 16,000, unless i'm the dinosaur;->
    For a 2 byte integer, yes. For a 4 byte integer, no.
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The code is flawed.
    1) It never calls the function, instead it prints the actual function address which is not a string. You may very well get junk or a crash.
    2) The function "function" returns a pointer to local data. I'm not sure if it might just return a proper string, but I wouldn't bet on it.
    3) It should be const char*, not char*.
    4) i is never defined.
    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.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,337
    Quote Originally Posted by Elysia View Post
    The code is flawed.
    1) It never calls the function, instead it prints the actual function address which is not a string. You may very well get junk or a crash.
    2) The function "function" returns a pointer to local data. I'm not sure if it might just return a proper string, but I wouldn't bet on it.
    3) It should be const char*, not char*.
    4) i is never defined.
    You forgot one.
    Mainframe assembler programmer by trade. C coder when I can.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by guillermoh View Post
    Hello to all.
    Is this program licking memory?
    RAM is a terrible thing to taste.

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    38
    2) The function "function" returns a pointer to local data. I'm not sure if it might just return a proper string, but I wouldn't bet on it.
    Do I need to malloc()?

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you need to call function like
    function()
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    Registered User
    Join Date
    Sep 2007
    Posts
    38
    you need to call function like
    function()
    Sorry. typing mistake

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by guillermoh View Post
    Do I need to malloc()?
    Most likely, yes. Then you need to free.
    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.

  12. #12
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Elysia View Post
    Most likely, yes. Then you need to free.
    Why if he needs only to return some const string?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  13. #13
    Registered User
    Join Date
    Sep 2007
    Posts
    38
    Does this solve my problems?

    Code:
    int main()
    {
          gchar *string;
          while(i<100)
          {
                ...
                string = function();
                printf("Example &#37;s\n",string);
                free (string);
                ...
                i++;
           }
    
          return 0;
    }
    
    char* function ()
    {
           char* data = "";
           ...
           data = concatenate(data,"memory");
           ....
           return data;
    }
    
    char * concatenate (char* str1, char* str2)
    {
    	char *result;
    	if ( str2 != NULL )
    	{
    		result = malloc(strlen(str1) + strlen(str2) + 1);
    		if ( result != NULL )
    		{
    			strcpy(result, str1);
    			strcat(result, str2);
    		}
    	}
    	return result;
    }

  14. #14
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by guillermoh View Post
    Does this solve my problems?
    Depends on what is your problem

    Currently this code is more complicated without a gain
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  15. #15
    Registered User
    Join Date
    Sep 2007
    Posts
    38
    I'm trying to solve this:

    1) It never calls the function, instead it prints the actual function address which is not a string. You may very well get junk or a crash.
    2) The function "function" returns a pointer to local data. I'm not sure if it might just return a proper string, but I wouldn't bet on it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about memory
    By simo_mon in forum C Programming
    Replies: 7
    Last Post: 03-20-2009, 08:02 AM
  2. Problems with shared memory shmdt() shmctl()
    By Jcarroll in forum C Programming
    Replies: 1
    Last Post: 03-17-2009, 10:48 PM
  3. Memory question
    By John_L in forum Tech Board
    Replies: 8
    Last Post: 06-02-2008, 10:06 PM
  4. OS out of memory if malloc without free?
    By hkuser2001 in forum C Programming
    Replies: 7
    Last Post: 04-24-2006, 07:23 AM
  5. Question regarding constructors and memory.....
    By INFERNO2K in forum C++ Programming
    Replies: 6
    Last Post: 05-25-2005, 11:30 AM