Thread: [Dev-C++] whats wrong with this?
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Lloyd <ll...@cd...> - 2007-05-21 08:48:31
|
Hi ,
This is a small program and its output
#include<iostream>
using namespace std;
bool call(int *a)
{
static int x;
++x;
int *temp=new int;
*temp=x;
a=temp;
cout<<"call->"<<x<<","<<*a<<endl;
return true;
}
int main()
{
int *a;
for(int i=0;i<10;++i)
{
call(a);
cout<<"val->"<<*a<<endl;
}
return 0;
}
Outputcall->1,1
val->0
call->2,2
val->0
call->3,3
val->0
......
...
The output which I wanted is
Outputcall->1,1
val->1
call->2,2
val->2
call->3,3
val->3
.....
....
The modification of the pointer in the function is reflected in the
calling function. Would anybody tell me the reason for this mistake?
Thanks and Regards,
Lloyd
______________________________________
Scanned and protected by Email scanner
|
|
From: Lloyd <ll...@cd...> - 2007-05-21 08:55:17
|
Sorry some typing error are occured... please check this mail...
On Mon, 2007-05-21 at 14:25 +0530, Lloyd wrote:
> Hi ,
>
> This is a small program and its output
>
> #include<iostream>
> using namespace std;
>
> bool call(int *a)
> {
> static int x;
> ++x;
> int *temp=new int;
> *temp=x;
> a=temp;
> cout<<"call->"<<x<<","<<*a<<endl;
> return true;
> }
>
> int main()
> {
> int *a;
> for(int i=0;i<10;++i)
> {
> call(a);
> cout<<"val->"<<*a<<endl;
> }
> return 0;
> }
>
> Output
> call->1,1
> val->0
> call->2,2
> val->0
> call->3,3
> val->0
> .......
> ....
>
> The output which I wanted is
>
> Output
> call->1,1
> val->1
> call->2,2
> val->2
> call->3,3
> val->3
> ......
> .....
>
> The modification of the pointer in the function is not reflected in the
> calling function. Would anybody tell me the reason for this mistake?
>
> Thanks and Regards,
> Lloyd
>
>
>
>
> ______________________________________
> Scanned and protected by Email scanner
>
> -------------------------------------------------------------------------
> This SF.net email is sponsored by DB2 Express
> Download DB2 Express C - the FREE version of DB2 express and take
> control of your XML. No limits. Just data. Click to get it now.
> http://sourceforge.net/powerbar/db2/
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
______________________________________
Scanned and protected by Email scanner
|
|
From: Jonathan W. <jon...@gm...> - 2007-05-21 09:14:06
|
Hi,
You are missing a level of indirection:
the 'a' you are setting here (a=temp) is a local variable in the function
'call', so you can access the int that it points to, but not the pointer
that main knows
The a that main knows is always an uninitialized pointer, and you're just
lucky it's not segfaulting :D
to change the pointer main knows, you need a pointer or a reference to that
pointer. ie.:
(by pointer)
bool call(int **a) {
static int x;
++x;
int *temp=new int;
*temp=x;
*a=temp;
cout<<"call->"<<x<<","<<**a<<endl;
return true;
}
and
call(&a);
or
(by reference)
bool call(int* &a) { // no further changes
Also, you have a memory leak: you loose the new ints you create (indeed it's
a small leak, but consider this: I was leaking 192bytes in a loop and in 2
minutes, I ate up all my 2Gb of RAM....)
Regards,
Jonathan
On 5/21/07, Lloyd <ll...@cd...> wrote:
>
> Hi ,
>
> This is a small program and its output
>
> #include<iostream>
> using namespace std;
>
> bool call(int *a)
> {
> static int x;
> ++x;
> int *temp=new int;
> *temp=x;
> a=temp;
> cout<<"call->"<<x<<","<<*a<<endl;
> return true;
> }
>
> int main()
> {
> int *a;
> for(int i=0;i<10;++i)
> {
> call(a);
> cout<<"val->"<<*a<<endl;
> }
> return 0;
> }
>
> Outputcall->1,1
> val->0
> call->2,2
> val->0
> call->3,3
> val->0
> ......
> ...
>
> The output which I wanted is
>
> Outputcall->1,1
> val->1
> call->2,2
> val->2
> call->3,3
> val->3
> .....
> ....
>
> The modification of the pointer in the function is reflected in the
> calling function. Would anybody tell me the reason for this mistake?
>
> Thanks and Regards,
> Lloyd
>
>
>
>
> ______________________________________
> Scanned and protected by Email scanner
>
> -------------------------------------------------------------------------
> This SF.net email is sponsored by DB2 Express
> Download DB2 Express C - the FREE version of DB2 express and take
> control of your XML. No limits. Just data. Click to get it now.
> http://sourceforge.net/powerbar/db2/
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
--
<Morpheus> linux, c'est une question de VI ou de MORE
|
|
From: Lloyd <ll...@cd...> - 2007-05-21 09:48:54
|
Thank you Jonathan for your vary detailed reply. I works now. (I was
confused with the concept of call by reference :( ). I was aware of the
leak, but just to demonstrate the problem I wrote this program.
Thank you very much for your quick reply.
Thanks and Regards,
Lloyd
On Mon, 2007-05-21 at 11:14 +0200, Jonathan Winterflood wrote:
> Hi,
>
> You are missing a level of indirection:
> the 'a' you are setting here (a=temp) is a local variable in the
> function 'call', so you can access the int that it points to, but not
> the pointer that main knows
>
> The a that main knows is always an uninitialized pointer, and you're
> just lucky it's not segfaulting :D
>
> to change the pointer main knows, you need a pointer or a reference to
> that pointer. ie.:
> (by pointer)
> bool call(int **a) {
> static int x;
> ++x;
> int *temp=new int;
> *temp=x;
> *a=temp;
> cout<<"call->"<<x<<","<<**a<<endl;
> return true;
> }
> and
> call(&a);
>
> or
> (by reference)
> bool call(int* &a) { // no further changes
>
> Also, you have a memory leak: you loose the new ints you create
> (indeed it's a small leak, but consider this: I was leaking 192bytes
> in a loop and in 2 minutes, I ate up all my 2Gb of RAM....)
>
> Regards,
> Jonathan
>
> On 5/21/07, Lloyd <ll...@cd...> wrote:
> Hi ,
>
> This is a small program and its output
>
> #include<iostream>
> using namespace std;
>
> bool call(int *a)
> {
> static int x;
> ++x;
> int *temp=new int;
> *temp=x;
> a=temp;
> cout<<"call->"<<x<<","<<*a<<endl;
> return true;
> }
>
> int main()
> {
> int *a;
> for(int i=0;i<10;++i)
> {
> call(a);
> cout<<"val->"<<*a<<endl;
> }
> return 0;
> }
>
> Outputcall->1,1
> val->0
> call->2,2
> val->0
> call->3,3
> val->0
> ......
> ...
>
> The output which I wanted is
>
> Outputcall->1,1
> val->1
> call->2,2
> val->2
> call->3,3
> val->3
> ......
> ....
>
> The modification of the pointer in the function is reflected
> in the
> calling function. Would anybody tell me the reason for this
> mistake?
>
> Thanks and Regards,
> Lloyd
>
>
>
>
> ______________________________________
> Scanned and protected by Email scanner
>
> -------------------------------------------------------------------------
> This SF.net email is sponsored by DB2 Express
> Download DB2 Express C - the FREE version of DB2 express and
> take
> control of your XML. No limits. Just data. Click to get it
> now.
> http://sourceforge.net/powerbar/db2/
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE:
> http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
>
>
> --
> <Morpheus> linux, c'est une question de VI ou de MORE
______________________________________
Scanned and protected by Email scanner
|
|
From: Jonathan W. <jon...@gm...> - 2007-05-21 09:36:58
|
You're welcome,
If you're still confused by references, consider this: they are the same as
pointers, i.e. they reference other data, but C++ hides some of the dangers
of pointers with them: you cannot change the pointer that the reference
uses, plus they simplify notation where you don't need the extra
liberty/functionality.
>I was aware of the leak [...]
I suspected that :)
Regards,
Jonathan
On 5/21/07, Lloyd <ll...@cd...> wrote:
>
> Thank you Jonathan for your vary detailed reply. I works now. (I was
> confused with the concept of call by reference :( ). I was aware of the
> leak, but just to demonstrate the problem I wrote this program.
>
> Thank you very much for your quick reply.
>
> Thanks and Regards,
> Lloyd
>
>
>
> On Mon, 2007-05-21 at 11:14 +0200, Jonathan Winterflood wrote:
> > Hi,
> >
> > You are missing a level of indirection:
> > the 'a' you are setting here (a=temp) is a local variable in the
> > function 'call', so you can access the int that it points to, but not
> > the pointer that main knows
> >
> > The a that main knows is always an uninitialized pointer, and you're
> > just lucky it's not segfaulting :D
> >
> > to change the pointer main knows, you need a pointer or a reference to
> > that pointer. ie.:
> > (by pointer)
> > bool call(int **a) {
> > static int x;
> > ++x;
> > int *temp=new int;
> > *temp=x;
> > *a=temp;
> > cout<<"call->"<<x<<","<<**a<<endl;
> > return true;
> > }
> > and
> > call(&a);
> >
> > or
> > (by reference)
> > bool call(int* &a) { // no further changes
> >
> > Also, you have a memory leak: you loose the new ints you create
> > (indeed it's a small leak, but consider this: I was leaking 192bytes
> > in a loop and in 2 minutes, I ate up all my 2Gb of RAM....)
> >
> > Regards,
> > Jonathan
> >
> > On 5/21/07, Lloyd <ll...@cd...> wrote:
> > Hi ,
> >
> > This is a small program and its output
> >
> > #include<iostream>
> > using namespace std;
> >
> > bool call(int *a)
> > {
> > static int x;
> > ++x;
> > int *temp=new int;
> > *temp=x;
> > a=temp;
> > cout<<"call->"<<x<<","<<*a<<endl;
> > return true;
> > }
> >
> > int main()
> > {
> > int *a;
> > for(int i=0;i<10;++i)
> > {
> > call(a);
> > cout<<"val->"<<*a<<endl;
> > }
> > return 0;
> > }
> >
> > Outputcall->1,1
> > val->0
> > call->2,2
> > val->0
> > call->3,3
> > val->0
> > ......
> > ...
> >
> > The output which I wanted is
> >
> > Outputcall->1,1
> > val->1
> > call->2,2
> > val->2
> > call->3,3
> > val->3
> > ......
> > ....
> >
> > The modification of the pointer in the function is reflected
> > in the
> > calling function. Would anybody tell me the reason for this
> > mistake?
> >
> > Thanks and Regards,
> > Lloyd
> >
> >
> >
> >
> > ______________________________________
> > Scanned and protected by Email scanner
> >
> >
> -------------------------------------------------------------------------
> > This SF.net email is sponsored by DB2 Express
> > Download DB2 Express C - the FREE version of DB2 express and
> > take
> > control of your XML. No limits. Just data. Click to get it
> > now.
> > http://sourceforge.net/powerbar/db2/
> > _______________________________________________
> > Dev-cpp-users mailing list
> > Dev...@li...
> > TO UNSUBSCRIBE:
> > http://www23.brinkster.com/noicys/devcpp/ub.htm
> > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
> >
> >
> >
> > --
> > <Morpheus> linux, c'est une question de VI ou de MORE
>
>
> ______________________________________
> Scanned and protected by Email scanner
>
--
<Morpheus> linux, c'est une question de VI ou de MORE
|
|
From: Per W. <pw...@ia...> - 2007-05-21 09:14:51
|
The a pointer in the function call() is just a copy of the a pointer in
main(). Hence, your allocated pointer will not be transmitted back.
You must modify call() to either take a reference to a pointer, or send i
a pointer to a pointer. This will give call() the address of the pointer a
in main(), so that it knows where to store the pointer value returned from
new.
/pwm
On Mon, 21 May 2007, Lloyd wrote:
> Hi ,
>
> This is a small program and its output
>
> #include<iostream>
> using namespace std;
>
> bool call(int *a)
> {
> static int x;
> ++x;
> int *temp=new int;
> *temp=x;
> a=temp;
> cout<<"call->"<<x<<","<<*a<<endl;
> return true;
> }
>
> int main()
> {
> int *a;
> for(int i=0;i<10;++i)
> {
> call(a);
> cout<<"val->"<<*a<<endl;
> }
> return 0;
> }
>
> Outputcall->1,1
> val->0
> call->2,2
> val->0
> call->3,3
> val->0
> ......
> ...
>
> The output which I wanted is
>
> Outputcall->1,1
> val->1
> call->2,2
> val->2
> call->3,3
> val->3
> .....
> ....
>
> The modification of the pointer in the function is reflected in the
> calling function. Would anybody tell me the reason for this mistake?
>
> Thanks and Regards,
> Lloyd
>
>
>
>
> ______________________________________
> Scanned and protected by Email scanner
>
> -------------------------------------------------------------------------
> This SF.net email is sponsored by DB2 Express
> Download DB2 Express C - the FREE version of DB2 express and take
> control of your XML. No limits. Just data. Click to get it now.
> http://sourceforge.net/powerbar/db2/
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|