Thread: Questions regarding arrays and their memory consumption, depicted using sizeof()

  1. #1
    Registered User
    Join Date
    Nov 2014
    Posts
    5

    Questions regarding arrays and their memory consumption, depicted using sizeof()

    My Questions:
    1) Why does the function sizeof() return a higher byte value when evaluating nElementIndex rather than nElementIndex [ARRAY_LENGTH] ?

    2) If you remove the keyword const when declaring the integer variable, the program is unable to compile. Why is this?
    (I understand it would be completely redundant doing so, but still.)
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        const int ARRAY_LENGTH = 10;
        int nElementIndex [ARRAY_LENGTH] = {0};
        
        cout << sizeof(nElementIndex) << endl;
        cout << sizeof(nElementIndex [ARRAY_LENGTH]) << endl;
        
        return 0;
    }
    The only logical sense I can extract is that int consumes 4 bytes of memory, of which, since the number of elements is initialized to 10, the microprocessor stores into 10 addresses. Thus, validating the result of "40" from the program. But that's where things become confusing. I would of thought "40" would of been the result of sizeof(nElementIndex [ARRAY_LENGTH]). ???

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    sizeof(nElementIndex) gives the size of an array. sizeof(nElement[any_integral_value]) gives the size of an element of the array. Since arrays contain multiple elements, sizeof for an array can never be less than the size of an element.

    Array sizes in C++ can only be constants that are known at compile time. Removing the const makes ARRAY_LENGTH a variable, which - by definition - cannot have a value that is known at compile time.


    The size of an int is implementation defined. For your compiler (and chosen compiler settings) it might be consistently a value of 4 - because that's what is meant by "implementation defined". However, there is no requirement that size of an int be 4 - and there are real-world compilers that support different sizes.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Nit:
    Code:
    cout << sizeof(nElementIndex [ARRAY_LENGTH]) << endl;
    Since ARRAY_LENGTH is 10, the range of valid index values to use in accessing the nElementIndex array go from 0 through 9 (ARRAY_LENGTH - 1). Using ARRAY_LENGTH at this point here constitutes an attempt to access beyond the array bounds and is undefined behavior.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by hk_mp5kpdw View Post
    Nit:
    Code:
    cout << sizeof(nElementIndex [ARRAY_LENGTH]) << endl;
    Since ARRAY_LENGTH is 10, the range of valid index values to use in accessing the nElementIndex array go from 0 through 9 (ARRAY_LENGTH - 1). Using ARRAY_LENGTH at this point here constitutes an attempt to access beyond the array bounds and is undefined behavior.
    That's not true. sizeof does not evaluate its operand - it only works out the type of the operand, and then evaluates the size of the type. So sizeof(nElementIndex [ARRAY_LENGTH]) does not attempt to access one past the end of array nElementIndex.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Oops... see, I'm still learning new things even after all these years. Now, if I can only RETAIN said knowledge in my feeble mind.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by hk_mp5kpdw View Post
    Now, if I can only RETAIN said knowledge in my feeble mind.
    Very few people can retain everything.

    The trick to learning is working out what core knowledge you need to retain. From there, it is often a case of knowing where to look (e.g. in a textbook) or how to work out (e.g. derive from the core things you know) other things.

    The catch is that what works for one person doesn't necessarily work for another. And that, sometimes, most people still get things wrong. It's called being human.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. RAM memory consumption
    By onako in forum C++ Programming
    Replies: 3
    Last Post: 08-19-2010, 07:40 AM
  2. sizeof(arrays)
    By Nextstopearth in forum C Programming
    Replies: 5
    Last Post: 02-18-2009, 12:04 AM
  3. calculate memory consumption and cpu usage
    By GermanDev in forum C++ Programming
    Replies: 18
    Last Post: 12-14-2008, 05:32 PM
  4. How to measure memory consumption?
    By nysubmarine in forum C++ Programming
    Replies: 25
    Last Post: 06-27-2006, 03:40 AM
  5. sizeof, calloc and free questions
    By gogo in forum C Programming
    Replies: 3
    Last Post: 10-25-2001, 05:32 AM