Jump to content
  • Advertisement
Sign in to follow this  
Mr_Od86

Help value of pointer seems to change magically!!

This topic is 3419 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, I have no idea why this is happening as my code seems fine and compiles fine on Dev-C++. I have one function that initialises an array and another function that is supposed to read values from this array. I'm trying to accomplish this through using a pointer (first time i'm using pointers) but somewhere it goes wrong.
#include <cstdlib>
#include <iostream>

using namespace std;

int *ptr_pop;   //Pointer to pop array
void initpop()
{
     int pop[5] = {0};
     ptr_pop = &pop[0]; //Pointer pointing to first array element
     
     cout<<"Enter the population of the 5 cities"<<endl;
     cout << &pop[2]<<endl;   //Address of second element
     for(int i = 0; i<5; i++)
         cin>> pop;
}

void displaypop()
{
     cout<< (ptr_pop + 2) <<endl;   //Address of second element
     for (int i = 0; i<5; i++)
     //{
         cout<< *(ptr_pop + i) <<endl;
     //}
}
          
int main(int argc, char *argv[])
{
    initpop();
    displaypop();
    system("PAUSE");
    return EXIT_SUCCESS;
}


This is the output that I get:
Enter the population of the 5 cities
0x22ff38
100
80
60
40
20
0x22ff38
2088810051
54131748
2293592
4199585
4469712
Press any key to continue . . .


The address that the pointer points to is correct but somehow the value changes when it is inside the for loop. Does anyone have any idea what I'm doing wrong??

Share this post


Link to post
Share on other sites
Advertisement
What do beleive happens to the pop array after you leave the function in which it is defined? The array no longer exists so the program is free to use that memory as it sees fit.

Share this post


Link to post
Share on other sites
Haha, duh!! I have been wondering how in the world the values just changed. I made the pop array static and now the problem has disappeared, thanks alot!!

Share this post


Link to post
Share on other sites
Put another way, "pop" in initpop is a local variable on the stack for initpop. When execution leaves initpop, pop goes out of scope and is destroyed. At this point the global ptr_pop is pointing to memory that is no longer valid.

You're actually lucky. Sometimes this bug does not manifest itself immediately -- it's always wrong, but sometimes with this class of bug you will get exactly the results you expect when you test it the first time, and won't be until much later you discover that something isn't quite right.

Quote:

Haha, duh!! I have been wondering how in the world the values just changed. I made the pop array static and now the problem has disappeared, thanks alot!!

This isn't necessarily what you want, either. While it will solve the immediate problem, you may be causing others, depending on what your actually trying to do and if this is dummied up code or just code you're using to explore things.

Share this post


Link to post
Share on other sites
Quote:

Quote:

Haha, duh!! I have been wondering how in the world the values just changed. I made the pop array static and now the problem has disappeared, thanks alot!!

This isn't necessarily what you want, either. While it will solve the immediate problem, you may be causing others, depending on what your actually trying to do and if this is dummied up code or just code you're using to explore things.



Ooh I see, I just wanted to create a global array that will hold the data value of the populations as they will be changing throughout the "game". I want these to be available to all functions, so that for instance the total sum of the populations can be calculated. In the past I have had trouble defining a global array outside of any function, so this is the solution that I'm trying at the moment. If there is a better way to approach this, I would love to know!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!