Jump to content
Tuts 4 You

C++ Arrays


D1N

Recommended Posts

Posted (edited)

First, thanks to everyone for taking the time to answer and offer your explanations. I've been working with arrays in C++ recently.

I've commented out the good code so you guys can compile it and produce these odd values and locations.

When I use

int mbox[5] = {10,20,30,40,50};  // normal init @10,20,30,40,50 -> mbox @0,1,2,3,4

things look great! but when I don't initialize anything the program produces random values and my question is two fold why and where do these random values come from?

// main.cpp 
// This is a simple example of using arrays#include <iostream>
#include <conio.h>using namespace std;int main()
{
// int mbox[5] = {10,20,30,40,50}; // normal init @10,20,30,40,50 -> mbox @0,1,2,3,4
// int mbox[5] = {}; // init 0 @nothing
int mbox[5]; // <--- Strange effect
for(int i = 0; i<5; i++)
{
cout << "Array mailbox index " << i << " " << "contains the value: "
<< mbox[i] << " @ memory location: " << mbox <<endl;
} getch();
return 0;
}

The result is:

Array mailbox index 0 contains the value: -858993460 @ memory location: 003FFC64Array mailbox index 1 contains the value: -858993460 @ memory location: 003FFC64Array mailbox index 2 contains the value: -858993460 @ memory location: 003FFC64Array mailbox index 3 contains the value: -858993460 @ memory location: 003FFC64Array mailbox index 4 contains the value: -858993460 @ memory location: 003FFC64

The normal result:

Array mailbox index 0 contains the value: 10 @ memory location: 004EF8BC
Array mailbox index 1 contains the value: 20 @ memory location: 004EF8BC
Array mailbox index 2 contains the value: 30 @ memory location: 004EF8BC
Array mailbox index 3 contains the value: 40 @ memory location: 004EF8BC
Array mailbox index 4 contains the value: 50 @ memory location: 004EF8BC

thanks :)

Teddy can you move me to Programming sorry XD

Edited by D1N
Posted (edited)

You have to 0 the memory because all it is doing is allocating space on the stack for local variables when you type this.

int mbox[5] is like saying add esp, 24. Also the result address @ 003FFC64 looks like stack, assuming image base 400000, and the "normal result" @ 004EF8BC looks like a static reference. A static ref would default 0, or whatever you want, its a constant that is always there, unless changed.

Edited by What
Posted (edited)

figured out the answer! ;-) when and if the array is not initialized it takes the place of whatever was in memory prior. So the values are actual data just not the data we are trying to initialize in our index of arrays. Thanks what for your information makes sense.

Edited by D1N
Posted

int mbox[5] = {0};

would start the array empty if that helps :)

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...