Size of fixed size object?

This is a discussion on Size of fixed size object? within the C Programming forums, part of the General Programming Boards category; Dear All, How many bytes following peace of code will take? Code: char *num[10] = { "One", "Two", "Three", "Four", ...

  1. #1
    Registered User
    Join Date
    Apr 2008

    Size of fixed size object?

    Dear All,
    How many bytes following peace of code will take?

    char *num[10] = { "One", "Two", "Three", "Four", 
    "Five", "Six", "Seven", "Eight", "Nine", "Ten" } ;
    Thanks in Advance

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Including a newline at the end, there are 101 characters in that piece of code, so the answer is likely to be 101 or 103.

    You might want to phrase your question more accurately. For example, if you are asking for the size of num in bytes, then the answer would be sizeof(char*) * 10.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    It would not be possible to calculate EXACTLY how many bytes are used by the strings pointed to by the array members, since there is no good way to tell generically what (if any) padding the compiler produces between strings.

    But the formula to calculate the minimum total size is 10 * sizeof(char *) + length of each member + 1. The compiler may, however, pad everything to even 2, 4, 8 or 16 bytes for it's own ideas of optimization and/or processor architecture requirements.

    Further, how much ACTUAL space is used would depend on where this line is placed. If it's a local variable inside a function, the array needs to be initialized every time the function is called [or the block is entered, if it's inside another block within a function]. This means (at least) copying 10 pointers from one place to another, which may be anything from 3 instructions upwards (probably no more than 50 instructions, but certainly 40 is quite plausible for a 29K processor for example, since it needs 2 instructions to load a 32-bit address and one instruction to store the value and one move to the next location).

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Heapsort
    By xENGINEERx in forum C Programming
    Replies: 2
    Last Post: 03-30-2008, 07:17 PM
  2. Error with a vector
    By Tropicalia in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2006, 07:45 PM
  3. Replies: 60
    Last Post: 12-20-2005, 10:36 PM
  4. Linked List Templates and Object Types
    By ventolin in forum C++ Programming
    Replies: 10
    Last Post: 06-16-2004, 12:05 PM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 04:27 AM

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