0

Write a function to find exact size of dynamically created * variable ?

Guys it is working but for static allocation only...

int alp=0;
printf("%d",(char*)(&alp+1)-(char*)(&alp));

it will return 4 correct size, which is size of int at 32 bit machine but not working with dynamically allocated pointer variable.

char *c=(char *)malloc(12*sizeof(char));

How to find size of *c which is actually 12 here ??

please Help me to write a function to find dynamically allocated memory.

srhaider
  • 17
  • 4
  • 1
    You can't, you'll need to keep track of it yourself (in a separate variable). – Kninnug Oct 13 '15 at 17:25
  • By the way, [In C you should not cast the result of `malloc`](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/605858#605858), or any other function returning `void *`. – Some programmer dude Oct 13 '15 at 17:26
  • Possible duplicate of [determine size of dynamically allocated memory in c](http://stackoverflow.com/questions/1281686/determine-size-of-dynamically-allocated-memory-in-c) – Fred Larson Oct 13 '15 at 17:28
  • The size is the size you allocated. But because you used a hard-coded `12` you have made life difficult for yourself. Your question is not comparing like with like. If you did the same exercise using the `char*` pointer as you did with `int*` you would get corresponding result. – Weather Vane Oct 13 '15 at 17:32

2 Answers2

3

The short and only answer is that you can't. You simply have to keep track of it yourself.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • helo sir, I have read somewhere last block point to the first block. When request is made free list is scanned until big enough block is found. And if the block is too big , it is split , and proper amount is returned to user. Is and way to found it. – srhaider Oct 13 '15 at 17:45
  • @srhaider *Some* memory allocators may work that way, but there is no standardized memory allocator system, and the only way to take a peek at the internal data used by the memory allocator is to go out of bounds of the memory returned to your application, and that's technically *undefined behavior*. The ***only*** standard way to get the size of a dynamic memory allocation is to keep track of it yourself. – Some programmer dude Oct 13 '15 at 17:52
  • There's a gnu extension to do just that: `malloc_usable_size()`. – EOF Oct 13 '15 at 17:56
2

There's no way to know programmatically how many bytes were allocated in a call to malloc(). You need to keep track of that size separately and pass that size around where needed.

For example:

void myfunc(char *c, int size)
{
    int i;
    for (i=0;i<size;i++) {
        printf("c[%d]=%c\n",size,c[size]);
    }
}

int main()
{
    int len=10;
    char *c = malloc(len);
    strcpy(c,"hello");
    myfunc(c,len);
    free(c);
}
dbush
  • 205,898
  • 23
  • 218
  • 273
  • Do you pass type of data ant size of it in sizeof operator too. – srhaider Oct 13 '15 at 17:38
  • @srhaider The `sizeof` operator takes a single operand which is either a type name in parenthesis or a variable or literal. It doesn't take the size, since that's what it's returning to you. – dbush Oct 13 '15 at 17:42
  • @srhaider `sizeof(c)` in this instance will evaluate to the size of a `char *`, since that is the type of `c`. It will most likely evaluate to either 4 or 8, depending on how big a pointer is on your system. – dbush Oct 13 '15 at 17:44