Thread: Size of fixed size object?

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    83

    Size of fixed size object?

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

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

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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).

    --
    Mats
    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, 11: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, 05:27 AM