Posted October 20, 200816 yr 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: 004EF8BCArray mailbox index 1 contains the value: 20 @ memory location: 004EF8BCArray mailbox index 2 contains the value: 30 @ memory location: 004EF8BCArray mailbox index 3 contains the value: 40 @ memory location: 004EF8BCArray mailbox index 4 contains the value: 50 @ memory location: 004EF8BC thanks Teddy can you move me to Programming sorry XD Edited October 20, 200816 yr by D1N
October 20, 200816 yr use of this source for more detail's :http://www.team-x.ru/guru-exe/Sources/Crac...emplate-FFF.rarBye
October 20, 200816 yr 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 October 20, 200816 yr by What
October 22, 200816 yr Author 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 October 23, 200816 yr by D1N
Create an account or sign in to comment