Thread: malloc and realloc

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    28

    malloc and realloc

    hello,
    Code:
    char *str;
    str = (char *) malloc (sizeof (char) * strlen("hello"));
    strcpy(str, "hello");
    str = (char *) malloc (sizeof (char) * strlen("bye bye"));
    strcpy(str, "bye bye");
    g_free(str);
    is the above code memory OK or the continuous allocation of malloc will lead to a memory leak problem (imagine i have a big loop of reading strings)? in other words is the use of malloc correct or do i need to use realloc? as i think the only difference between the two is that malloc is destructive without copying the previous contents while realloc is also destructive but copies the old contents if necessary

    cheers
    Last edited by odysseus.lost; 05-27-2005 at 04:25 AM. Reason: clarification

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc, realloc..wat on earth!
    By zesty in forum C Programming
    Replies: 3
    Last Post: 12-21-2007, 01:42 PM
  2. To malloc or not realloc on struct members
    By gh0st in forum C Programming
    Replies: 4
    Last Post: 11-17-2006, 05:27 AM
  3. malloc, realloc
    By figo2476 in forum C Programming
    Replies: 3
    Last Post: 04-28-2006, 10:11 PM
  4. Linked list versus malloc and realloc.
    By Bajanine in forum C Programming
    Replies: 2
    Last Post: 06-20-2005, 08:08 PM
  5. Need help on malloc and realloc
    By YevGenius in forum C Programming
    Replies: 8
    Last Post: 03-06-2004, 01:55 AM