Thread: Why malloc?

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    45

    Why malloc?

    Code:
    struct STRING
    {
      int size;
      char *theString;
    };
    
    typedef struct STRING String;
    
    
    String * newString( char const * const init )
    {
      String *newString = NULL;
      
      newString = (String *)malloc( sizeof( String ) );
      
      if ( NULL != newString )
      {
        newString->theString = (char *)malloc( strlen( init ) + 1 );
        
        if ( NULL != newString->theString )
        {
          strcpy( newString->theString, init );
          newString->size = strlen( newString->theString );
        }
        
        // not enough memory, clean up
        else
        {
          free( newString );
          // set ptr to NULL so we don't illegally reference it
          newString = NULL;
        }
      }
      
      // don't use a struct declaration...
      // struct assignment doesn't do deep copying
      return newString;
    }
    In this code why do you use malloc here (line 18):
    newString->theString = (char *)malloc( strlen( init ) + 1 );
    instead of just directly copying the initial string
    strcpy(newString -> theString, init);

    I was told that when you malloc you put the object onto the heap so that don't disappear after the function ends.
    So if you didn't malloc but instead copied directly what would happen?
    I tried this and the terminal said: Segmentation fault (core dumped)
    I don't get what happened.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    I see a couple of problems.

    First, you cast the result of malloc, and you shouldn't.

    Second, the variable you use for your new string inside the newString function is called newString. Naming a local variable the same as its containing function is arguably wrong, and at a minimum, it's confusing to anyone reading it.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    Jan 2016
    Posts
    45
    This is sample code written by my teacher.
    He said you have to cast the return of malloc because malloc returns a void pointer.
    You're probably right, but maybe we just haven't got to that point.
    I'm just learning malloc and want to know why I get a segmentation fault if I don't use malloc on line 18.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by seal308 View Post
    I'm just learning malloc and want to know why I get a segmentation fault if I don't use malloc on line 18.
    The memory you allocate on line 12 is for an instance of the STRING struct. This gives you enough space for (1) an integer, and (2) a character pointer.

    But that character pointer does not, by default, point to accessible memory. So you then need to allocate memory for that pointer. Hence line 18.

    When you free memory, you need to do so in reverse order - first free the char pointer (within the struct), then the struct pointer.

    Quote Originally Posted by seal308 View Post
    This is sample code written by my teacher.
    He said you have to cast the return of malloc because malloc returns a void pointer.
    You're probably right, but maybe we just haven't got to that point.
    Your teacher is giving you bad advice in this respect - read the link Elkvis posted.

  5. #5
    Registered User
    Join Date
    Jan 2016
    Posts
    45
    Ok thx.
    So then for any pointer in a struct I need to allocate memory for?

    So if my struct contained a int* pointer.
    Would the int it's pointing at need to be allocated as well?
    so malloc(sizeOf(int))

    Also I'll look into the casting issue and ask my teacher about it.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by seal308 View Post
    Ok thx.
    So then for any pointer in a struct I need to allocate memory for?

    So if my struct contained a int* pointer.
    Would the int it's pointing at need to be allocated as well?
    so malloc(sizeOf(int))
    Yes, unless it's intended to point to something that already exists.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  2. Replies: 7
    Last Post: 10-01-2008, 07:45 PM
  3. Malloc and MPI
    By moddinati in forum C Programming
    Replies: 17
    Last Post: 03-07-2008, 07:55 PM
  4. malloc() under DOS
    By knutso in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-18-2003, 07:06 AM
  5. malloc()
    By ammar in forum C++ Programming
    Replies: 3
    Last Post: 10-22-2002, 03:58 PM

Tags for this Thread