pointer corruption

N

nair SL

I want to understand whether this following piece of code is giving me
junk result becoz I'm trying to access values out of scope?

int* array_display(){
int array1[4] = {11,22,33,44};
int *qq = array1;
return qq;
}

int main()
{
int *pp;
pp = array_display();

for(i=0; i<4; ++i,++pp) {
int temp = *(pp); //case 1
// int temp = *(pp+1); //case 2 returns 22 first and
subsequent junk
cout<<"pp array ->- "<<temp<<endl;
}
return 0;
}
expected output : 11 22 33 44
but I'm hitting ony the first access point. Rest is giving me junk.
The point is if I can access any values by incrementing pointer (as in
the second case), why I could not access them a second time?

Any help will be appreciated.
Thanks in advance...
 
M

Morya

I want to understand whether this following piece of code is giving me
junk result becoz I'm trying to access values out of scope?

int* array_display(){
        int array1[4] = {11,22,33,44};
        int *qq = array1;
        return qq;

}

int main()
{
        int *pp;
        pp = array_display();

        for(i=0; i<4; ++i,++pp) {
             int temp = *(pp); //case 1
             // int temp = *(pp+1); //case 2 returns 22 first and
subsequent junk
                cout<<"pp array ->- "<<temp<<endl;
        }
        return 0;}

expected output : 11 22 33 44
but I'm hitting ony the first access point. Rest is giving me junk.
The point is if I can access any values by incrementing pointer (as in
the second case), why I could not access them a second time?

Any help will be appreciated.
Thanks in advance...

IMHO, array is local to array_display() which means it runs out of
scope when array_display() returns.
HTH.

~Mo
 
A

Andrey Tarasevich

nair said:
I want to understand whether this following piece of code is giving me
junk result becoz I'm trying to access values out of scope?

int* array_display(){
int array1[4] = {11,22,33,44};
int *qq = array1;
return qq;
}

int main()
{
int *pp;
pp = array_display();

for(i=0; i<4; ++i,++pp) {
int temp = *(pp); //case 1
// int temp = *(pp+1); //case 2 returns 22 first and
subsequent junk
cout<<"pp array ->- "<<temp<<endl;
}
return 0;
}
expected output : 11 22 33 44
but I'm hitting ony the first access point. Rest is giving me junk.
The point is if I can access any values by incrementing pointer (as in
the second case), why I could not access them a second time?


'array1' is a local (automatic) array in 'array_display' function. Once
this function finishes working they array is gone. It no longer exists.
It cannot be accessed regardless of how you try to access it. What you
do in your code after calling 'array_display' produces undefined behavior.
 
M

Morya

I want to understand whether this following piece of code is giving me
junk result becoz I'm trying to access values out of scope?
int* array_display(){
        int array1[4] = {11,22,33,44};
        int *qq = array1;
        return qq;

int main()
{
        int *pp;
        pp = array_display();
        for(i=0; i<4; ++i,++pp) {
             int temp = *(pp); //case 1
             // int temp = *(pp+1); //case 2 returns 22 first and
subsequent junk
                cout<<"pp array ->- "<<temp<<endl;
        }
        return 0;}
expected output : 11 22 33 44
but I'm hitting ony the first access point. Rest is giving me junk.
The point is if I can access any values by incrementing pointer (as in
the second case), why I could not access them a second time?
Any help will be appreciated.
Thanks in advance...

IMHO, array is local to array_display() which means it runs out of
scope when array_display() returns.
HTH.

~Mo

On a second note i see that you are not displaying the array at all in
the array_display(). But you are trying to allocate something. Might
want to consider using a reference to a pointer. Your code might look
something like this:

void allocate_array(int*& qq)
{
qq = new int[4]; // google around for how to initialize.
}

int main()
{
int * array;
allocate_array(array);
for(i=0;i<4;++i) {
cout << array;
}
delete [] array;
}

HTH.
~Mo
 
T

Thomas J. Gritzan

Morya wrote:
[...returning pointer to local array...]
On a second note i see that you are not displaying the array at all in
the array_display(). But you are trying to allocate something. Might
want to consider using a reference to a pointer. Your code might look
something like this:

void allocate_array(int*& qq)
{
qq = new int[4]; // google around for how to initialize.
}

int main()
{
int * array;
allocate_array(array);
for(i=0;i<4;++i) {
cout << array;
}
delete [] array;
}


Please don't use new[]! It's a waiting memory leak. There are much
better tools around.

std::vector<int> filled_array()
{
int array1[4] = {11,22,33,44};
// use begin()/end() templates if you like
std::vector<int> arr(array1, array1 + 4);
return arr;
}

int main()
{
std::vector<int> array = filled_array();
for(size_t i = 0; i<array.size(); ++i)
std::cout << array;
}
 
G

gw7rib

I want to understand whether this following piece of code is giving me
junk result becoz I'm trying to access values out of scope?

int* array_display(){
        int array1[4] = {11,22,33,44};
        int *qq = array1;
        return qq;

}

int main()
{
        int *pp;
        pp = array_display();

        for(i=0; i<4; ++i,++pp) {
             int temp = *(pp); //case 1
             // int temp = *(pp+1); //case 2 returns 22 first and
subsequent junk
                cout<<"pp array ->- "<<temp<<endl;
        }
        return 0;}

I think you're right about the problem, but you are using slightly the
wrong name for it.

As the program stands, array1 disappears when the function
array_display finishes. So when you try to read it in main, it's gone
before you finish.

You can fix this problem by putting the word "static" in front of the
"int array1". This will cause the array to exist all the time the
program is running, and so (if you also add "int i;" to main, and a
suitable header) the program will work.

The property of how long a variable hangs around for is called its
"lifetime". In your program as stated, the lifetime of the array was
while array_display was running, with my changes, the lifetime of the
array is the time the program is running.

The "scope" of a variable is the places in the program where you can
refer to it by name. In your program, and indeed in my modification of
it, the scope of array1 is the routine array_display. You can't refer
to the array by the name "array1", or by the name "qq", in main. But
if the lifetime of the array is still running, you can refer to it
using another variable which has the same value - in this case, using
pp.

Hope that helps.
Paul.
 
N

nair SL

I want to understand whether this following piece of code is giving me
junk result becoz I'm trying to access values out of scope?
int* array_display(){
        int array1[4] = {11,22,33,44};
        int *qq = array1;
        return qq;

int main()
{
        int *pp;
        pp = array_display();
        for(i=0; i<4; ++i,++pp) {
             int temp = *(pp); //case 1
             // int temp = *(pp+1); //case 2 returns 22 first and
subsequent junk
                cout<<"pp array ->- "<<temp<<endl;
        }
        return 0;}

I think you're right about the problem, but you are using slightly the
wrong name for it.

As the program stands, array1 disappears when the function
array_display finishes. So when you try to read it in main, it's gone
before you finish.

You can fix this problem by putting the word "static" in front of the
"int array1". This will cause the array to exist all the time the
program is running, and so (if you also add "int i;" to main, and a
suitable header) the program will work.

The property of how long a variable hangs around for is called its
"lifetime". In your program as stated, the lifetime of the array was
while array_display was running, with my changes, the lifetime of the
array is the time the program is running.

The "scope" of a variable is the places in the program where you can
refer to it by name. In your program, and indeed in my modification of
it, the scope of array1 is the routine array_display. You can't refer
to the array by the name "array1", or by the name "qq", in main. But
if the lifetime of the array is still running, you can refer to it
using another variable which has the same value - in this case, using
pp.

Hope that helps.
Paul.

Thanks for all the Tips.
That helped me a lot.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top