Thread: dynamic char array

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    26

    dynamic char array

    Code:
    char file[11];
    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?

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Code:
    char* array=new char[size];
    
    .....
    
    delete [] array;
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    26
    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.

  5. #5
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136
    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.

  6. #6
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  2. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  3. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 11:55 AM
  4. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM