Thread: Getting the size of an array of structs

  1. #1
    UK2
    Join Date
    Sep 2003
    Posts
    112

    Getting the size of an array of structs

    Hello,

    I am just wondering as I have seen this in some sample code. And when I was looking at it more carefully I am wondering if two would do the same job in getting the correct size of the array structure.

    Code:
    static const unsigned int NUMBER_OF_PORTS = 2;
    struct PORTS
    {
    	unsigned int portID;
        unsigned int portType;
    
    } port[NUMBER_OF_PORTS];
    My structure has 2 unsigned int (keeping it simple) 4 bytes each. And the array is only 2 elements.

    So my idea of the actual size of the array is this. (2 * 4) = (unsigned int * bytes) = 8.
    The array size is 2. So that gives us 2 * 8 = 16.

    The above structure is 16 bytes total. So with the code below sizeof(port) would be correct size.
    Code:
    std::cout << "portID: " << sizeof(unsigned int) << std::endl; // 4 bytes
    std::cout << "port: " << sizeof(port) << std::endl; //16 bytes
    std::cout << "PORT: " << sizeof(PORT) << std::endl; //8 bytes
    Now, I have sent this in a sample C++ program
    Code:
    memset(port, 0, NUMBER_OF_PORTS * sizeof(PORT)); //Will give 16 bytes
    Now with what I have mentioned above. Would it not be possible to have this instead?
    Code:
    memset(port, 0, sizeof(port));
    I think this is more clear and more efficient as there is no calculation to perform.

    Am I correct with my thinking.

    Thanks,

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Assuming that port is visible as an array (not passed in as a pointer), that should give you the same result, yes.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    It's not more efficient, though, because the calculation is most likely performed at compile time anyway.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Also, as a tip, for arrays, you should use std::tr1::array, if you can.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do you use variable to initialize array size?
    By yougene in forum C Programming
    Replies: 11
    Last Post: 09-04-2007, 02:50 PM
  2. Array size
    By tigrfire in forum C Programming
    Replies: 5
    Last Post: 11-14-2005, 08:45 PM
  3. An exercise in optimization
    By Prelude in forum Contests Board
    Replies: 10
    Last Post: 04-29-2005, 03:06 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Maximum array size?
    By code2big in forum C Programming
    Replies: 2
    Last Post: 05-25-2004, 08:16 AM