Quick question on malloc and strings

This is a discussion on Quick question on malloc and strings within the C Programming forums, part of the General Programming Boards category; What is strlcpy()??? Don't you mean strncpy() ?...

  1. #16
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    What is strlcpy()??? Don't you mean strncpy()?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  2. #17
    Registered User officedog's Avatar
    Join Date
    Oct 2008
    Posts
    77
    This has been very helpful, thanks to everyone.

    Summary:
    a) Don't put cast when calling malloc unless its in C++
    b) Don't free pointer within function
    c) No need to assign pointer to NULL if the pointer is assigned straight away

    Elysia - thanks for the extra explanation. I assume then that because I pass the address of the struct into the function, this is essentially acting as you suggest - a buffer/place to store the char*. I have taken the last 2 lines out as well (free(word) and word = NULL).

    Very last question (I'll have to delete the word "quick" in the title now). My last function is one that will replace the word previously stored. Here's what I wrote - the only difference between this and the initialisation function is in blue, where I free the memory (or at least I think I free the memory) associated with the previous word.

    Code:
    void setWord(Word *wordStruct, const char *newWord)
    {
        free(wordStruct->word);
        
         int isAllocated;
        char* word;
        int length;
        
        length = strlen(newWord) +1;
        word = malloc(length); //sizeof(char) = 1 byte
        if (word != NULL) {
            isAllocated = 1;
            strlcpy(word, newWord, length); //strlcpy adds '\0'
            
            wordStruct->word = word;
            wordStruct->length = length;
            wordStruct->isAllocated = isAllocated;
        }
        else {
            isAllocated = 0;
        }
    }

  3. #18
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Or maybe they mean't strlcpy()

  4. #19
    Registered User officedog's Avatar
    Join Date
    Oct 2008
    Posts
    77
    cpjust - here's a link http://en.wikipedia.org/wiki/Strlcpy

    I read that this would be a 'safer' function than strcpy. Apparently it adds a '\0' to the end of the string as a bonus.

  5. #20
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Quote Originally Posted by officedog View Post
    cpjust - here's a link http://en.wikipedia.org/wiki/Strlcpy

    I read that this would be a 'safer' function than strcpy. Apparently it adds a '\0' to the end of the string as a bonus.
    Quite. It is a very good function to use.

    Quote Originally Posted by officedog View Post
    b) Don't free pointer within function
    Careful. Depends.
    If it is only going to be used inside the function, then you must free it.
    But if you free it inside the function, its memory will be unaccessible outside and any data it contains will be destroyed. So therefore, if you plan to keep the data inside some allocated memory after the function's end, you must not free it.

    Elysia - thanks for the extra explanation. I assume then that because I pass the address of the struct into the function, this is essentially acting as you suggest - a buffer/place to store the char*. I have taken the last 2 lines out as well (free(word) and word = NULL).
    Indeed it is.
    But I meant you to be careful with pointers you allocate. You need to take into account where the data should be used and thusly, whose responsibility it is to free the data later.

    Very last question (I'll have to delete the word "quick" in the title now). My last function is one that will replace the word previously stored. Here's what I wrote - the only difference between this and the initialisation function is in blue, where I free the memory (or at least I think I free the memory) associated with the previous word.

    Code:
    void setWord(Word *wordStruct, const char *newWord)
    {
        free(wordStruct->word);
        
         int isAllocated;
        char* word;
        int length;
        
        length = strlen(newWord) +1;
        word = malloc(length); //sizeof(char) = 1 byte
        if (word != NULL) {
            isAllocated = 1;
            strlcpy(word, newWord, length); //strlcpy adds '\0'
            
            wordStruct->word = word;
            wordStruct->length = length;
            wordStruct->isAllocated = isAllocated;
        }
        else {
            isAllocated = 0;
        }
    }
    Indeed, if you replace the word, you have to get rid of the old memory if you need to allocate more space.
    However, you should add a check to see if there is sufficient space already in there, and if not, allocate new space and free the old.
    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. #21
    Registered User officedog's Avatar
    Join Date
    Oct 2008
    Posts
    77
    Thanks for the last notes Elysia - I took on your suggestion about checking the memory before reallocating. All seems to work fine.

    And finally a big thanks for all the help with this today.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick Question: 2D Array with char **
    By Phoenix_Rebirth in forum C Programming
    Replies: 4
    Last Post: 01-29-2009, 06:33 AM
  2. malloc question
    By adrive in forum C Programming
    Replies: 11
    Last Post: 07-24-2008, 09:27 AM
  3. Question on malloc() and reading strings
    By rockysfr in forum C Programming
    Replies: 2
    Last Post: 09-04-2005, 06:37 PM
  4. how to use malloc for array of strings
    By SoFarAway in forum C Programming
    Replies: 3
    Last Post: 02-18-2005, 03:19 PM
  5. malloc() strings VS array strings
    By Kleid-0 in forum C Programming
    Replies: 5
    Last Post: 01-10-2005, 09:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21