1
char *str;
int length = 100;  
str = (char *)malloc(sizeof(char) * (length+1));
*str = '\0';

free(str);  

strcpy strlen not work, i tried that the expression strlen(str); result is 0.
will free work? if free now work, it can be many memory leaks.

nosources
  • 71
  • 4

4 Answers4

2

Strlen returns zero because after the allocation of memory you make the starting value of pointer is '\0'. So strlen is check until the null character is occurs. So the return value is zero.

Karthikeyan.R.S
  • 3,991
  • 1
  • 19
  • 31
  • yeah,got that,now the free will work?how the free founction knows the length of str – nosources Jan 09 '15 at 08:49
  • 1
    Typically the size of a memory block is stored just before the memory block. However, there are other techniques that could be used. – Michael Burr Jan 09 '15 at 08:50
  • Free function will free the value allocated by the calloc or malloc up to the allocated value. Free and strlen is a separate function. free will not consider about the strlen. – Karthikeyan.R.S Jan 09 '15 at 08:50
  • thank you all,where can i find the source code behind free?is it possible to find a simply version? – nosources Jan 09 '15 at 08:56
  • i want to find out where stores the size i malloc, it is during the compile or during the execution.confused.i must stop and think about this – nosources Jan 09 '15 at 09:00
  • During the malloc memory will be allocated from the heap memory. `http://gribblelab.org/CBootcamp/7_Memory_Stack_vs_Heap.html` See this one. – Karthikeyan.R.S Jan 09 '15 at 09:03
  • 1
    @nosources Dynamically allocating memory is performed at runtime. If you want to know how and where the size is stored, you have to find the source code for your particular compiler/platform of the standard C library (if the source code is available) and study it. – nos Jan 09 '15 at 09:03
  • i am becoming to understand this issue. although i am not finishing the reading. the malloc size is not fixed, and malloc location should be determined at runtime. so compiler can't do this. the operate system will hold the malloc size for all pointers created by many applications, and store the size when malloc, i will read the code who manage the heap to understand more. – nosources Jan 09 '15 at 09:36
1

strlen will give you the length of the C string not the size of the buffer.

The length of a C string is determined by the terminating null-character: a C string is as long as the number of characters between the beginning of the string and the terminating null character (without including the terminating null character itself). So 0 is the right answer.

However the malloc() routine will hold the size of the allocation you made so when you free() it, it frees the right amount of space.

It is up to you to track the size of the memory if you need it (see Using sizeof with a dynamically allocated array and Is it possible to find the Memory Allocated to the Pointer, without searching for the malloc statement).

With gcc / glibc you have the malloc_usable_size function (not included in POSIX.x / C90 / C99 / C11):

#include <malloc.h>

size_t malloc_usable_size (void *ptr);

that returns the number of usable bytes in the block pointed to by ptr.

Community
  • 1
  • 1
manlio
  • 18,345
  • 14
  • 76
  • 126
1

The length of a string (as calculated by strlen()) has nothing to do with the size of the memory allocation. free() doesn't determine the number of bytes to return to the heap based on strlen(). The bookkeeping for how large a block of memory will be freed is handled behind the scenes.

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
0

free() requires a void * and similarly void * is returned as from malloc() family functions. so it will free the amount of memory which is allocated irrespective to the place where you place the \0 delimeter.

strlen() does not work because it counts the number of characters upto \0 delimeter. as this is your zeroth index so there is no character before this delimeter as a result 0 is returned.

theadnangondal
  • 1,546
  • 3
  • 14
  • 28