When you wrote:
char buff1[100];
you create a buffer of
sizeof(char) * 100
, not just 100. It will be different if you used another type, for example the same code but with long will yield 400, because
sizeof(long)
, which equals to 4,
* 100
elements.
Remember this very well because it's important when it comes to memory allocation. If you were using
malloc
, or Windows allocators like
HeapAlloc
, and wrote:
long *foo = malloc(4);
You're actually allocate some memory that can hold only one element, not four. This is a common mistake.
In the seconds case:
char *buff2 = new char[100];
You create a pointer or, in other words, a "memory address". All addresses in 32bit machines are 32bit in size ,
0x11223344
. If you split it into bytes you will end-up with four bytes:
0x11
,
0x22
,
0x33
and
0x44
.
Quote:
but isn't there a way to obtain whole array length for dynamic arrays?
You can't allocate any memory unless you already know the size.