0

I'm trying to find out where the fundamental data types are stored when a variable is declared and what the binary representation is for the fundamental data types. Below is some code that gets data from beyond the array. The output pertaining to the last line is different depending on how I declare the array YoMammy[]; signed or unsigned. I understand that this is normal but I don't know where the fundamental data type is stored. Is this information stored with the pointers? I understand that an array is just another way to reference pointers.

int main()
{
    unsigned int YoMammy[2]={3,7};

    cout<<YoMammy[0];
    cout<<endl;
    cout<<YoMammy[1];
    cout<<endl;
    cout<<YoMammy[2];

    return 0;
}
David
  • 15,894
  • 22
  • 55
  • 66
Tyrone
  • 77
  • 1
  • 10
  • Either way, your code exhibits undefined behavior and can legally produce any output whatsoever. – Igor Tandetnik Oct 21 '13 at 02:06
  • What do you mean where its stored? In memory.. – vidit Oct 21 '13 at 02:06
  • 1
    Realize that the same bit pattern can be interpreted differently. A byte with all bits set to 1 (that is, 0xFF) could represent a value of -1 if interpreted as a signed char, and as 255 if interpreted as unsigned. This is what you observe: there's some garbage value on the stack above your array, and you are asking `cout` to print it as a singed int in one case, and as unsigned int in another. – Igor Tandetnik Oct 21 '13 at 02:08
  • I understand this. I want to know where the stuff that tells the computer how to interpret the set of bits representing a variable is stored. This has to be stored somewhere otherwise I wouldn't get a different result when declaring the array with different fundamental data types. – Tyrone Oct 21 '13 at 02:26

4 Answers4

0

Accessing elements beyond the array's range leads to undefined behavior. You may get some value as YoMammy[2], you may get a segmentation fault, or something weird happens.

I understand that an array is just another way to reference pointers.

Ah, no, no, no. In many situations, an array name is converted to a pointer to its first element. But arrays are not pointers.

Also, you can chain the calls to std::cout, it makes your life easier:

std::cout << YoMammy[0] << std::endl << YoMammy[1] << std::endl << YoMammy[2];
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
0

An array in the logical sense is the the storage in the physical. That means it is one location with with several blocks. int would = i=[9] is one slot to the name. Whereas an array has several slots in the same location array=[][][][][][][][]

dcaswell
  • 3,137
  • 2
  • 26
  • 25
user2865335
  • 27
  • 1
  • 4
0

There is no metadata stored that describes simple datatypes in C++. It is merely a co-incidence that you are observing some relationship between the signed and unsigned integers.

This is what undefined behaviour is. It might exhibit a pattern, but it is not defined. Don't try to define behaviour that has no definition. =)

paddy
  • 60,864
  • 6
  • 61
  • 103
0

Q: Where the variables are stored?

You may print the address and see for yourself where the variables are located.

std::cout<<"Location of YoMammy[0]: "<<&YoMammy[0]<<std::endl;
std::cout<<"Location of YoMammy[1]: "<<&YoMammy[1]<<std::endl;

Since the ones you declared are local to main() function. They are stored at the stack segment.

Q: The output pertaining to the last line is different depending on how I declare the array YoMammy[]; signed or unsigned

Array indexing begins from 0 till n-1. n is the size of the array.

(YoMammy[2]) Accessing out of the bounds is Undefined Behaviour. It can lead to Segmentation Fault, Stack Smashing or just print some garbage value ( which you can't expect to be the same, everytime you run the program) , etc.,

Q: I understand that an array is just another way to reference pointers.

No. Arrays are not pointers. Though array name decays into pointer to the first element of the array. There are exceptions to it. Read this

Community
  • 1
  • 1
smRaj
  • 1,246
  • 1
  • 9
  • 13