Pelles C forum

Pelles C => General discussions => Topic started by: czerny on January 19, 2015, 12:36:50 PM

Title: size of string memory
Post by: czerny on January 19, 2015, 12:36:50 PM
If I define
Code: [Select]
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
Code: [Select]
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
Code: [Select]
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'?
Title: Re: size of string memory
Post by: frankie on January 19, 2015, 01:18:04 PM
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).
Title: Re: size of string memory
Post by: czerny on January 19, 2015, 06:49:57 PM
Is there something like a heap pointer (or better two heap pointers, a small heap pointer and a large heap pointer)?