By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,431 Members | 1,845 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,431 IT Pros & Developers. It's quick & easy.

pointer corruption

P: n/a
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...
Nov 10 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Nov 10, 1:48*pm, nair SL <anoop...@gmail.comwrote:
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
Nov 10 '08 #2

P: n/a
nair SL wrote:
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.

--
Best regards,
Andrey Tarasevich
Nov 10 '08 #3

P: n/a
On Nov 10, 2:02*pm, Morya <mda...@gmail.comwrote:
On Nov 10, 1:48*pm, nair SL <anoop...@gmail.comwrote:
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 22first 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[i];
}
delete [] array;
}

HTH.
~Mo
Nov 10 '08 #4

P: n/a
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[i];
}
delete [] array;
}
Please don't use new[]! It's a waiting memory leak. There are much
better tools around.

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

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

--
Thomas
Nov 10 '08 #5

P: n/a
On 10 Nov, 08:48, nair SL <anoop...@gmail.comwrote:
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.
Nov 10 '08 #6

P: n/a
On Nov 11, 1:03*am, gw7...@aol.com wrote:
On 10 Nov, 08:48, nair SL <anoop...@gmail.comwrote:
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 22first 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.
Nov 12 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.