-
dynamic char array
That uses 24 bytes. I am not happy about that when I will be having it in a struct array 20 * 80.
How do I create a dynamic array that starts of at a minimal size, and then allows me to allocate memory for the length of the filename specified?
-
Code:
char* array=new char[size];
.....
delete [] array;
http://faq.cprogramming.com/cgi-bin/...&id=1043284351
-
> That uses 24 bytes.
Huh?
It looks like it uses 11 from here...
> How do I create a dynamic array that starts of at a minimal size
Which is likely to take up more space, since dynamic memory has overheads which statically allocated memory does not (for example, space for the pointer).
Not to mention all the admin overhead of the heap manager.
In debug builds, the compiler usually inserts padding "DMZ's" around arrays in particular to help trap your array overrun code.
Even in release builds, memory will be allocated so as to preserve performance enhancing memory alignment restrictions.
I think you need to stop worrying so much about where every single byte is going, and focus more on the actual problems to be solved.
-
There's no way on earth I'm doing 20 * 80 of char file[11]; no ........ing way. Reason is that the array exists for the potential to add a filename...there may be only one tile on the board which links to another board. It's cheaper to waste 4 bytes * 80 * 20 than 12 bytes.
-
Use std::strings. They handle memory allocation and deallocation for you. Plus, I think they are really quite flexible.
If std::strings aren't appropriate, you can use a std::vector to act as a dynamic array of sorts for you.
-
Dude, what are you worrying about? That's only 17.1875kb of memory, hardly a scratch on the surface of most people's RAM nowadays. You should be more worried about memory when you start bumping up to the megabytes being used.