Thread: dynamic memory allocation and returning pointers

  1. #1
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157

    dynamic memory allocation and returning pointers

    Bare with me here. I am still lost on doing 'word' problems -- understanding syntax of code and what the problem asks for is not my forte - YET.

    I need to do the following: write a function that uses dyn mem allocation to create a copy of a string. The call of such function would be : p = strdup(str);

    Function is to :
    (1) allocate space for a string of the same length as str
    (2) copy the contents of str into the new string
    (3) return a pointer to it
    (4) return a null pointer if the memory allocation fails

    Is any of the following OK?
    Whatever is wrong or unnecessary, can you step me through the logic of it, so I can correct my
    error(s)...and thus learn this correctly.

    Hey and what should I set up in the main() to actually check this code for myself???

    Code:
    char *strdup(const char *str)
    {
    
         char *result;
         
         result = malloc(strlen(str)+1);
         if (result == NULL)  {
            printf("Error: malloc failed ");
            return NULL;
         }
    
         strcpy (result, str);
         return result;
    
    }
    Sue B.

    dazed and confused


  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int main( void )
    {
       char *s = strdup( "This is a test." );
       printf("%s", s );
       return 0;
    }
    Quzah.

  3. #3
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157

    compilation error

    hey Quzah:

    getting a compilation error. How do I fix??

    error:

    4 /accounts/student2/gcc copystring.c
    copystring.c: In function `strdup':
    copystring.c:21: warning: assignment makes pointer from integer without a cast


    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    char *strdup(const char *str);
    
    
    main()
    {
    
       char *s = strdup( "This is a test." );
       printf("%s", s );
       return 0;
    }
    
    char *strdup(const char *str)
    {
    
         char *result;
    
         result = malloc(strlen(str)+1);            <--- line 21
         if (result == NULL)  {
            printf("Error: malloc failed ");
            return NULL;
         }
    
         strcpy (result, str);
         return result;
    
    }
    Sue B.

    dazed and confused


  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You need to include stdlib.h to include the prototype for malloc
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Dammit, Salem. You beat me to it. I was replying to this when I saw it update in another window.

    Quzah.

  6. #6
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157
    i got it to work when I added

    (char *) before malloc

    I didn't add the stdlib either

    ???
    Sue B.

    dazed and confused


  7. #7
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157
    ok added stdlib.h

    and took out (char *) before malloc

    it works that way too
    Sue B.

    dazed and confused


  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The reason it gives the error, without you including the correct header is this:

    Any time you use a function that hasn't been declared or prototyped, the compiler assumes it returns an integer. Thus, when you tried to assign it's return value to a character pointer, it gave you an error saying, "Hey, you can't just convert an integer into a char* without casting!"

    So, if you add a cast, it doesn't give that error, but it still isn't correct, because you haven't told it how malloc works (by adding its header). It ends up working because of the way it includes the libraries when it does the linking.

    Anyway, that's the reason you got the message.

    Quzah.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. returning pointers from a function
    By curlious in forum C++ Programming
    Replies: 2
    Last Post: 12-28-2003, 11:37 PM