What is strlcpy()??? Don't you mean strncpy()?
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
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; } }
Or maybe they mean't strlcpy()
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.
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.
Indeed it is.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).
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.
Indeed, if you replace the word, you have to get rid of the old memory if you need to allocate more space.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; } }
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.
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.