Only pointers returned by calls to malloc()
, realloc()
or calloc()
can be passed to free()
(dynamically allocated memory on the heap). From section 7.20.3.2 The free function of C99 standard:
The free function causes the space pointed to by ptr to be deallocated, that is, made available for further allocation. If ptr is a null pointer, no action occurs. Otherwise, if the argument does not match a pointer earlier returned by the calloc, malloc, or realloc function, or if the space has been deallocated by a call to free or realloc, the behavior is undefined.
In the posted code, str
is not dynamically allocated but is allocated on the stack and is automatically released when it goes out of scope and does not need to be free()
d.
Related Posts:
- How big can a 64 bit unsigned integer be?
- warning: implicit declaration of function
- What is *(uint32_t*)?
- Why should we typedef a struct so often in C?
- What is a string of hexadecimal digits?
- What is the difference between ++i and i++?
- Stack smashing detected
- Stack smashing detected
- How do I determine the size of my array in C?
- Why am I getting “void value not ignored as it ought to be”?
- Pointer Arithmetic
- Openssl : error “self signed certificate in certificate chain”
- What is the difference between C and embedded C?
- How do I create an array of strings in C?
- Difference between malloc and calloc?
- How do function pointers in C work?
- segmentation fault : 11
- What are .a and .so files?
- So what does “return 0” actually mean?
- “Expected expression before ‘ { ‘ token”
- How to generate a random int in C?
- C pointers and arrays: [Warning] assignment makes pointer from integer without a cast
- warning: assignment makes integer from pointer without a cast
- How to print the array?
- How to solve the error: assignment to expression with array type
- segmentation fault : 11
- Why use bzero over memset?
- What does “1e” mean?
- how to use uint64_t in C [duplicate]
- How do I solve the following errors: “Undefined reference to WinMain”, “[Error] Id returned 1 exit status”?
- Warning comparison between pointer and integer
- How do I properly compare strings in C?
- Connect: Socket operation on non-socket
- How to correctly use the extern keyword in C
- How do we check if a pointer is NULL pointer?
- how to use wait in C
- what is the meaning of == sign?
- How do I calculate MB/s & MiB/s?
- How to create my own header file in c++?
- ARM Assembler – How do I use CMP, BLT and BGT?
- In C programming, what is `undefined reference`error, when compiling?
- Invalid read of size 8 – Valgrind + C
- warning: incompatible implicit declaration of built-in function ‘xyz’
- strcpy vs strdup
- double free or corruption (fasttop)
- Warning comparison between pointer and integer in C language
- error: expected declaration or statement at end of input in c
- Why do I get clang: error: linker command failed with exit code 1?
- Difference between scanf() and fgets()
- Return char[]/string from a function [duplicate]
- How to convert integer to char in C?
- Is there a printf converter to print in binary format?
- Program received signal SIGPIPE, Broken pipe
- Difference between char* and const char*?
- How to read from stdin with fgets()?
- Output single character in C
- Does connect() block for TCP socket?
- Converting a C program to MIPS
- Why am I getting “array initializer must be an initializer list or string literal”?
- what is the unsigned datatype?
- C: linker command failed with exit code 1
- #31 expression must have integral type
- How do you pass a function as a parameter in C?
- What’s the equivalent of new/delete of C++ in C?
- sizeof float (3.0) vs (3.0f)
- How to trigger SIGUSR1 and SIGUSR2?
- What does “%.*s” mean in printf?
- Display value found at given address gdb
- Does C have a “foreach” loop construct?
- What is a bus error? Is it different from a segmentation fault?
- Array definition – Expression must have a constant value
- Constant pointer vs Pointer to constant
- What is the cause of flexible array member not at end of struct error?
- Variable warning set but not used
- Reversing a string in C
- When a number is written as 0x00… what does the x mean
- Expected declaration specifier error in function
- How can I get argv[] as int?
- Why I do get “Cannot find bound of current function” when I overwrite the ret address of a vulnerable program?
- valgrind – Address —- is 0 bytes after a block of size 8 alloc’d
- Writing binary number system in C code
- Convert char array to string use C
- warning: passing argument ’from incompatible pointer type [enabled by default]’
- Return a `struct` from a function in C
- How does one represent the empty char?
- Removing trailing newline character from fgets() input
- “Multiple definition”, “first defined here” errors
- how use EOF stdin in C
- Execution of printf() and Segmentation Fault
- How does the strtok function in C work? [duplicate]
- Allocating char array using malloc
- Implementation of strtok() function
- switch case: error: case label does not reduce to an integer constant
- warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
- warning: return makes pointer from integer without a cast but returns integer as desired
- Reaching EOF with fgets
- The difference between char * and char[] [duplicate]
- How to clear input buffer in C?
- Compiler warning – suggest parentheses around assignment used as truth value
- error: `itoa` was not declared in this scope