NO

Author Topic: size of string memory  (Read 2613 times)

czerny

  • Guest
size of string memory
« 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'?

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2096
Re: size of string memory
« Reply #1 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).
It is better to be hated for what you are than to be loved for what you are not. - Andre Gide

czerny

  • Guest
Re: size of string memory
« Reply #2 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)?