Click here to Skip to main content
15,919,434 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hi,

I have issue understanding following. Shouldn't be a big thing though.

If I have
C++
char buff1[100];
int test1 = sizeof buff1;


Then test1 contains 100 (which is what I want).

If I have following code:

C++
char *buff2 = new char[100];
int test2 = sizeof buff2;


then test2 contains 4. Why former case works and later doesn't ??? I want to obtain length of the whole (dynamic) array. Edited: Well actually I think slowly I start to understand why above code returns 4 (because buff2 is simply a pointer and it returns size of the pointer -- but isn't there a way to obtain whole array length for dynamic arrays?)

thanks.
Posted
Updated 27-Oct-12 12:32pm
v2

Quote
isn't there a way to obtain whole array length for dynamic arrays?

Using a dynamic C-array, no. If you need that kind of information, and you need the array to be dynamic in size, you better use the STL class std::vector. It keeps track of it's size and cleans up after itself. You can even dynamically extend the array at runtime.
C++
std::vector<char> buffer; // defines buffer with initial size 0
const char* hello = "Hello";
buffer.reserve(strlen(hello)); // tells buffer to make room for at least 5 chars
for (std::size_t i = 0; i < strlen(hello); ++i) {
   buffer[i] = hello[i];
}
std::size_t buf_size = buffer.size(); // this value will be 5
buffer.push_back('!'); // dynamically resizes buffer and appends '!'
buf_size = buffer.size(); // size is now 6

At the end of this code segment, buffer contains "Hello!" (but note there is no trailing 0 character)

On a sidenote, if your intention is just storing strings, use std::string instead. It has the same functionality as std::vector, but also defines additional string operations.
 
Share this answer
 
Yes agree with pallani. Can't get dynamicaly allocated array size. There is a simple logic to get size.
1. Keep a variable to track of it.
 
Share this answer
 
Comments
CPallini 29-Oct-12 10:22am    
It is 'Pallini'.
Thanks for agreeing with me. :-)
[no name] 29-Oct-12 22:55pm    
sorry sir. Thanks
// but isn't there a way to obtain whole array length for dynamic arrays?

Yes, an array could be wrapped into a class
that is caching the allocated (and used) size(s) :)
 
Share this answer
 
Quote:
Well actually I think slowly I start to understand why above code returns 4 (because buff2 is simply a pointer and it returns size of the pointer
Yes, it is correct.


Quote:
but isn't there a way to obtain whole array length for dynamic arrays?
No, there is no way.
 
Share this answer
 
When you wrote:

C++
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:

C++
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:

C++
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.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900