size of string memory

Started by czerny, January 19, 2015, 12:36:50 PM

Previous topic - Next topic

czerny

If I define
char a[] = "Blub";
I can get the memory size of 'a' with the sizeof operator.
But if I call a function 'tst(a)' then inside of 'tst' the sizeof operator gives me the size of the address of 'a'.

If I define
char* b = malloc(somesize);
The 'sizeof(b)' too is the size of the address of 'b'.

'a' is a constant string while 'b' is allocated on the heap.

It should be possible to differentiate between the two string types by
BOOL IsHeapStr(char* x) { return _heap_validate(x); }

But how can I find the memory size they own, 5 for 'a' and somesize for 'b'?

frankie

You can't.
For the allocated value you can use _msize function, and if pointing to a valid allocated memory chunk you will get a value that is not exactly what you have allocated, but generally bigger.
Passing a static array on the called function side you always see a char * which size id 4 or 8 bytes (32 or 64 bits compilation).
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

czerny

Is there something like a heap pointer (or better two heap pointers, a small heap pointer and a large heap pointer)?