Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Unexpected pointer action

Reply
Thread Tools

Unexpected pointer action

 
 
jvh
Guest
Posts: n/a
 
      12-30-2011
A friend told me that he had read somewhere that giving pointers to an
average programmer is like giving a gun to a 6 year old. Since I got
the wrong answer for the problem below I must be an average
programmer.

I got this problem from cprogramming.com. Does it display 10 or 12 and
why?

void afunction(int *x)
{
x = new int;
*x = 12;
}

void main()
{
int v = 10;
afunction(&v);
cout << "\nv:" << v;

getchar();
}


Thanks,
jvh
 
Reply With Quote
 
 
 
 
LR
Guest
Posts: n/a
 
      12-30-2011
jvh wrote:
> void afunction(int *x)
> {
> x = new int;
> *x = 12;
> }
>
> void main()


int main()

> {
> int v = 10;
> afunction(&v);
> cout << "\nv:" << v;


std::cout << "\nv:" << v;
>
> getchar();
> }


It displays 10. C++ passes the address of v in main to afunction by
value. The parameter, x, in afunction is the value of the pointer, and
changing it doesn't change v in main.


I think it'll become clearer if you try adding some output to see the
changing values, maybe like so:

void a(int *x) {
std::cout << "a,x " << x << " a,*x " << *x << std::endl;
x = new int(12); // leaks memory
std::cout << "a,x " << x << " a,*x " << *x << std::endl;
}

void testa() {
int v = 10;
std::cout << "testa,&v " << &v << " testa,v " << v << std::endl;
a(&v);
std::cout << "testa,&v " << &v << " testa,v " << v << std::endl;
}

int main() {
testa();
}

 
Reply With Quote
 
 
 
 
jvh
Guest
Posts: n/a
 
      12-30-2011

>
> It displays 10. *C++ passes the address of *v in main to afunction by
> value. *The parameter, x, in afunction is the value of the pointer, and
> changing it doesn't change v in main.
>
> I think it'll become clearer if you try adding some output to see the
> changing values, maybe like so:
>
> void a(int *x) {
> * * std::cout << "a,x " << x << " a,*x " << *x << std::endl;
> * * x = new int(12); // leaks memory
> * * std::cout << "a,x " << x << " a,*x " << *x << std::endl;
>
> }
>
> void testa() {
> * * int v = 10;
> * * std::cout << "testa,&v " << &v *<< " testa,v *" << v << std::endl;
> * * a(&v);
> * * std::cout << "testa,&v " << &v *<< " testa,v *" << v << std::endl;
>
> }
>
> int main() {
> * * testa();
>
>


Thanks for the post, I should know better. If x had not been
reassigned then it would display 12. So I think it would be correct to
say that V was passed by reference (by its address) but it's address
was passed by value.

I had put it many display statements and they were telling me what you
told me but not as concisely and I did not see it.

Thanks again,

jvh (AKA an average programmer)
 
Reply With Quote
 
MikeWhy
Guest
Posts: n/a
 
      12-30-2011
jvh wrote:
>> It displays 10. C++ passes the address of v in main to afunction by
>> value. The parameter, x, in afunction is the value of the pointer,
>> and changing it doesn't change v in main.
>>
>> I think it'll become clearer if you try adding some output to see the
>> changing values, maybe like so:
>>
>> void a(int *x) {
>> std::cout << "a,x " << x << " a,*x " << *x << std::endl;
>> x = new int(12); // leaks memory
>> std::cout << "a,x " << x << " a,*x " << *x << std::endl;
>>
>> }
>>
>> void testa() {
>> int v = 10;
>> std::cout << "testa,&v " << &v << " testa,v " << v << std::endl;
>> a(&v);
>> std::cout << "testa,&v " << &v << " testa,v " << v << std::endl;
>>
>> }
>>
>> int main() {
>> testa();
>>
>>

>
> Thanks for the post, I should know better. If x had not been
> reassigned then it would display 12. So I think it would be correct to
> say that V was passed by reference (by its address) but it's address
> was passed by value.


That's still not right. The following is closer to equivalent:

void a(int * p)
{
int * x = new int;
*x = 12;
}

You probably intended the following instead:

void a(int ** p)
{
*p = new int;
**p = 12;
}

int main()
{
int * p;
a(&p);
...
}


 
Reply With Quote
 
gwowen
Guest
Posts: n/a
 
      01-04-2012
On Dec 30 2011, 7:31*pm, jvh <(E-Mail Removed)> wrote:
> A friend told me that he had read somewhere that giving pointers to an
> average programmer is like giving a gun to a 6 year old. Since I got
> the wrong answer for the problem below I must be an average
> programmer.
>
> I got this problem from cprogramming.com. Does it display 10 or 12 and
> why?
>
> void afunction(int *x)
> {
> * * * * x = new int;
> * * * * *x = 12;
>
> }
>
> void main()
> {
> * * * * int v = 10;
> * * * * afunction(&v);
> * * * * cout << "\nv:" << v;
>
> * * * * getchar();
>
> }


In addition to what's been said, its sometimes illuminating to
"inline" the function yourself, so you can see what's going on.

void main()
{
int v = 10;
// afunction(&v);
{
int *x = &v; // x points to v
x = new int; // x now points someplace else
*x = 12; // set the value stored "someplace else"
}
cout << "\nv:" << v;
getchar();
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Struts: 1 action, 1 Form, several views handled by 1 only action. Any idea for better design? John Java 0 06-26-2007 11:22 PM
Can we call an Action from another Action in struts??? vyshu Java 1 04-27-2007 09:19 AM
Struts mapping action to action??? runescience Java 3 02-07-2006 04:07 PM
Struts Forward to an Action from an to Action and URLs rjweytens Java 6 06-25-2004 01:49 PM
Struts Static Action Form vs Dyanamic Action Form Joe Bloggs Java 1 08-03-2003 02:30 AM



Advertisments