0

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円?

Vlad from Moscow
313k27 gold badges204 silver badges358 bronze badges
asked Dec 16, 2024 at 20:45
2
  • 6
    It frees all the allocated memory. Commented Dec 16, 2024 at 20:47
  • 2
    After strcpy the character at long_one[15] is a '0円'. ... BTW *(long_one+9) is 100% equivalent to long_one[9]: I find the latter more readable. Commented Dec 16, 2024 at 20:48

2 Answers 2

4

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 malloc in C. Relevant reading: Should I cast the result of malloc (in C)?
  • sizeof(char) is 1, so you can just pass 256 to malloc. Though you probably want to define a constant to hold that 256 and avoid "magic numbers."
  • You should be checking to see that malloc succeeded 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. The main function implicitly returns 0 if 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;
}
answered Dec 16, 2024 at 20:46
Sign up to request clarification or add additional context in comments.

2 Comments

@zwol I would leave it as malloc(256 * sizoef(*long_one));. Why? If OP changes it to wchar then it will be still ok without remembering what has to be changed
@0___________ Who uses wchar? It's such a bad fit for the Unicode world. I say YAGNI.
2

If 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; 
answered Dec 16, 2024 at 20:58

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.