Jump to content
Larry Ullman's Book Forums

How Many Bytes For An Unsigned Long?


Recommended Posts

Hello everyone:

 

I am confused about the output from script 6.3.

 

I read it that the integer has a length of 4 bytes, character = 1, float = 4.  But the unsigned long gives a value indicating a length of 7.  At least to me.  Making in a 'non unsigned; long doesn't change things.

 

Making up an array of unsigned longs and printing out their addresses like in script 6.5 gives lengths of 4 bytes, not 7.  Does anyone understand this?  Thank you.

 

 

 

MJZ

Link to comment
Share on other sites

It seems to me you mix up the terms size and length. Length will give you the number of characters of the object.

 

Regarding signed vs unsigned, you need to think of numbers as a closed circle, not a straight line. An unsigned version of the type will therefor allow you to utilize larger numbers on the scale, while a signed version allocated negative numbers instead.

 

If you have ever touched PHP, you will see a constant called MAX_INT. That is the largest possible number allowed for the datatype Integer. If a larger number is assigned, you'll get something called Integer Overflow, and the number will instead be saved as a float. That kind of conversions between types are common. Reading more about basic data types will give you a lot. :)

Link to comment
Share on other sites

No problem. I'm not really familar with C+, so I can't share to much besides general programming knowledge.

 

The reason why I talked about size and length wasn't to be pedantic with you, but to tell you that length() will yield wrong results. (Considering what you want) I noticed at least String has a capacity() member that should return the size, but I'm not familiar enough with the manual to find if that's true for the other types. If you don't use the member length(), ignore this.

 

After some searching, I found a Stack Overflow post related to your information seeking. It seems the size of the types are dependent on both compiler, architecture and OS, as an Int can range from 4 to 8 bytes. I would take a strong guess that the same is true for long.

 

Also, I tried to explain why a sign and unsigned version of long won't differentiate in size. This is pretty common in language design, so I would guess C++ implements numbers the same way as Java or C#. Read my explanation one more time about that.

 

Did that answer anything? I'm sure Larry will get around to this one eventually.

Link to comment
Share on other sites

I'm not sure that I have anything else to add as Antonio's answer pretty much covers it, along with the Stack Overflow post. 

 

As a metaphor for the signed/unsigned, think of it like a bucket that you can put both red and blue marbles in. That'd be signed, with positive and negative numbers. The bucket can only fit X number of marbles. If you make it unsigned, you can only put the one marble type in, but can still only put the same X number of marbles. It doesn't change the size of the bucket, however.

Link to comment
Share on other sites

// address.cpp - Script 6.2 modified

#include <iostream>

int main() {

    // Create some variables.
    unsigned int a = 2;
    float b = 78.53;
    char c = 'D';
    long d = 1904856026;
    int e = 9002356;
    long f=3;

std::cout <<"a = "<<a<<std::endl;
std::cout <<"b = "<<b<<std::endl;
    // Print the addresses.
    std::cout << "The address of a is "
    << (unsigned long) &a << "\n";
    std::cout << "The address of b is "
    << (unsigned long) &b << "\n";
    std::cout << "The address of c is "
    << (unsigned long) &c << "\n";
    std::cout << "The address of d is "
    << (unsigned long) &d << "\n";
    std::cout << "The address of e is "
    << (unsigned long) &e << "\n";
    std::cout << "The address of f is "
    << (unsigned long) &f << "\n";

            const unsigned long I=3;
            long arr = {100.987,200.876,300.765};
            long *A_ptr= arr;
            for (int i =0; i<I; ++i){
                std::cout<<reinterpret_cast<long>(A_ptr)<<std::endl;
                A_ptr++;
            }

    std::cout << "Press Enter or Return to continue.\n";
    std::cin.get();

    return 0;

} // End of the main() function.

 

This gives the following output:

 

a=2

d=1904856026

The address of a is 229536

The address of b is 229532

The address of c is 229531

The address of d is 229524

The address of e is 229520

The address of a is 229516

 

address for an element of (long) array is 2293504

address for an element of (long) array is 2293508

address for an element of (long) array is 2293512

 

Press Enter or Continue to continue.

 

 

This output tells me that the long constant 'D' has a length of 7, but when a long loop was built, its addresses are 4 bytes apart!

The unsigned aspect of the problem doesn't come in here.

 

 

 

 

 

 

 

 

 

 

Link to comment
Share on other sites

Don't confuse addresses allocation with size. Some bits of the total size is used for meta data and similar things like pointers, etc. The size is an indication of the total amount of data you can save to the type, not how much size is used in total. You can compare this to HTTP headers, or something else you are familiar with.

 

One reasons why the array might allocate closer using less bytes, is that it can possibly ignore saving lots of redundant meta data. I don't really know how C++ functions from a lower level programming standpoint, but such optimizations are normally done behind the scenes.

 

To summarize, a single long and an array of longs still has the same capacity (4 bytes) but an array optimizes meta data in some way, allowing for closer memory addresses.

Link to comment
Share on other sites

  • 1 year later...
  • 5 months later...
 Share

×
×
  • Create New...