dynamic char array

This is a discussion on dynamic char array within the C++ Programming forums, part of the General Programming Boards category; Code: char file[11]; That uses 24 bytes. I am not happy about that when I will be having it in ...

  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
    University of Waterloo
    Posts
    1,903
    Code:
    char* array=new char[size];
    
    .....
    
    delete [] array;
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    University of Waterloo
    Posts
    1,903
    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, 10: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21