Jump to content
View in the app

A better way to browse. Learn more.

Tuts 4 You

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

C++ Arrays

Featured Replies

Posted

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

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

  • 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 by D1N

int mbox[5] = {0};

would start the array empty if that helps :)

Create an account or sign in to comment

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.