The following code allocates 256 bytes for a character array and then replaces the space with 0円 (similar to what strtok and strsep do).
#include <stdlib.h>
#include <string.h>
int main() {
char *long_one = (char *)malloc(sizeof(char) * 256);
strcpy(long_one, "Nutrition Facts");
*(long_one+9) = '0円';
free(long_one);
return 1;
}
Does the free in line 8 free all 256 bytes, or only those up to and including the 0円?
2 Answers 2
free doesn't treat memory as a null-terminated string like the string handling functions necessarily do, so yes, it would free all memory allocated by malloc.
This makes sense given that malloc is used for many purposes other than dynamically allocated char arrays for the purposes of building strings.
Additional notes
- Don't cast
mallocin C. Relevant reading: Should I cast the result of malloc (in C)? sizeof(char)is1, so you can just pass256tomalloc. Though you probably want to define a constant to hold that256and avoid "magic numbers."- You should be checking to see that
mallocsucceeded before using this memory. *(long_one+9) = '0円';is equivalently and more readably written as:long_one[9] = '0円';- There's no point in dynamically allocating in this scenario vs. automatic allocation. The memory size is not prohibitive for automatic storage duration, nor is there a concern about lifetimes for memory automatically allocated in
main. You might better just:char long_one[256]; return 1;suggests the program exited with an error. There are three options available to you:return 0;;return EXIT_SUCCESS;or simply don't explicitly return anything. Themainfunction implicitly returns0if there's no explicit return value.
Given some of these suggestions your code might look like:
#include <stdlib.h>
#include <string.h>
int main(void) {
char *long_one = malloc(256);
if (!long_one) {
fprintf(stderr, "Dynamic memory allocation failed.\n");
return EXIT_FAILURE;
}
strcpy(long_one, "Nutrition Facts");
long_one[9] = '0円';
free(long_one);
return EXIT_SUCCESS;
}
2 Comments
malloc(256 * sizoef(*long_one));. Why? If OP changes it to wchar then it will be still ok without remembering what has to be changedIf free would not deallocate all allocated memory by malloc then what to do with for example calloc that initializes allocated memory with zeroes?:)
Moreover as malloc does not initialize allocated memory then the allocated memory can contain any garbage including zeroes. And again what to do in such a case?:)
The size of allocated memory is stored internally and used when the allocated memory is freed.
Pay attention to that as @pmg wrote in his comment this statement
strcpy(long_one, "Nutrition Facts");
already stores in the allocated memory the terminating zero character '0円' of the string literal "Nutrition Facts".
A problem can occur if you will pass a pointer to the function free() that does not have the address of the allocated memory.
By the way you can use function realloc to enlarge or to truncate allocated memory.
For example after this statement
*(long_one+9) = '0円';
you could write
char *tmp = realloc( long_one, 10 );
if ( tmp != NULL ) long_one = tmp;
Comments
Explore related questions
See similar questions with these tags.
strcpythe character atlong_one[15]is a'0円'. ... BTW*(long_one+9)is 100% equivalent tolong_one[9]: I find the latter more readable.