char
is a character and sizeof(char)
is defined to be 1. (N1570 6.5.3.4 The sizeof and _Alignof operators, paragraph 4)
char*
is a pointer to a character and sizeof(char*)
depends on the environment. It is typically 4 in 32-bit environment and 8 in 64-bit environment.
In typical environment where sizeof(char*) > sizeof(char)
, malloc(sizeof(char*)*len + 1)
will (at least try to) allocate more memory than malloc(sizeof(char)*len + 1)
if len
is small enough not to cause integer overflow.
Related Posts:
- How to properly malloc for array of struct in C
- difference between
and - Use of cudamalloc(). Why the double pointer?
- Incorrect checksum for freed object on malloc
- Why is the sizeof(int) == sizeof(long)?
- Allocating char array using malloc
- How to allocate array of pointers for strings by malloc in C?
- realloc(): invalid next size when reallocating to make space for strcat on char *
- malloc(sizeof(int)) vs malloc(sizeof(int *)) vs (int *)malloc(sizeof(int))
- Difference between malloc and calloc?
- sizeof float (3.0) vs (3.0f)
- Incompatible implicit declaration of built-in function ‘malloc’
- Need more information about Aborted (core dumped)
- What is the cause of flexible array member not at end of struct error?
- How to initialize array to 0 in C?
- Is the sizeof(some pointer) always equal to four?
- Sign extend a nine-bit number in C
- Warning: assignment from incompatible pointer type
- How do malloc() and free() work?
- Cache Simulator in C
- How to run valgrind with basic c example?
- make: Nothing to be done for `all’
- Data argument not used by format strings in C
- execv vs execvp, why just one of them require the exact file’s path?
- expected expression before ‘{‘ token
- Setting std=c99 flag in GCC
- C Vector/ArrayList/LinkedList
- Convert char array to a int number in C
- What is the difference between %f and %lf in C?
- warning: initializer element is not computable at load time
- What is stdin in C language?
- error: ‘for’ loop initial declarations are only allowed in C99 mode [duplicate]
- c – warning: implicit declaration of function ‘printf’
- Flushing buffers in C
- Undefined Reference issues using Semaphores
- What is time(NULL) in C?
- How to compile makefile using MinGW?
- Portable way to check if directory exists [Windows/Linux, C]
- Allocating string with malloc
- How to use EOF to run through a text file in C?
- Expression must be a pointer to a complete object type using simple pointer arithmetic
- How to copy a char array in C?
- Simple way to check if a string contains another string in C?
- What does “request for member ‘*******’ in something not a structure or union” mean?
- Segmentation fault (core dumped) due to fgets – I think
- What does “request for member ‘*******’ in something not a structure or union” mean?
- Unknown ending signal when using debugger gdb
- Valgrind: invalid read of size 4 -> sigsegv, works fine without valgrind and in visual studio
- How to convert integer to char in C?
- Is there a good Valgrind substitute for Windows?
- Difference between “move” and “li” in MIPS assembly language
- How to empty a char array?
- What is the difference between array and enum in C ?
- Undefined reference to main – collect2: ld returned 1 exit status
- How can one see content of stack with GDB?
- What does the term “empty loop” refer to exactly in C and C++?
- Is there a way to have printf() properly print out an array (of floats, say)?
- What integer hash function are good that accepts an integer hash key?
- Proper way to empty a C-String
- malloc(): memory corruption
- How do you allow spaces to be entered using scanf?
- X86 assembly – Handling the IDIV instruction
- Pause screen at program completion in C
- Get size of pointer in C
- Where is the
header file on Linux? Why can’t I find ? - Difference between fgets and fscanf?
- What is the difference between %g and %f in C?
- Can’t understand the working of getint() in C as per K&R
- Structure padding and packing
- Lua – Number to string behaviour
- Reading in double values with scanf in c
- Realloc Invalid Pointer in C
- What tools are there for functional programming in C?
- typedef fixed length array
- “-bash: gcc: command not found” using cygwin when compiling c?
- How to format strings using printf() to get equal length in the output
- In C, what exactly happens when you pass a NULL pointer to strcmp()?
- In C, what exactly happens when you pass a NULL pointer to strcmp()?
- lseek/write suddenly returns -1 with errno = 9 (Bad file descriptor)
- What are the differences between if, else, and else if?
- strtok segmentation fault
- Invalid write of size 1
- How to solve static declaration follows non-static declaration in GCC C code?
- Constructor for structs in C
- C Error: declaration shadows a local variable — Won’t let me repeatedly replace the value of my float variable
- MIPS to C Translation
- Pointer to 2D arrays in C
- Pointer to a string in C?
- gdb: No symbol “i” in current context
- Cross Platform C library for GUI Apps?
- Implicit function declarations in C
- Pre increment vs Post increment in array
- Convert long long to string in C?
- Error: Conversion to non-scalar type requested
- how to stop a loop arduino
- How to normalize a mantissa
- “Nothing to be done for makefile” message
- How to use shared memory with Linux in C
- When is it ok to use a global variable in C?
- What is the difference between signed and unsigned int