RE: [Dev-C++] Correct use of pointers
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Hal K. <ha...@ha...> - 2003-03-14 20:50:04
|
small correction - the first line should read
int a,b,c,*p =3D &c;
otherwise your are still initializing p to an unknown value (if the compi=
ler
lets you get away with it)
Hal Kellerman
ha...@ha...
-----Original Message-----
From: dev...@li...
[mailto:dev...@li...]On Behalf Of Per
Westermark
Sent: Friday, March 14, 2003 12:19 PM
To: jo mateix
Cc: dev...@li...
Subject: Re: [Dev-C++] Correct use of pointers
The problem in this example is that the pointer is not initialized, i.e.
it doesn't point to anything. An assignment to *p would then send the dat=
a
just about anywhere, either corrupting the program, corrupting the OS
(some OS doesn't protect their own memory), or generate an exception if
outside of accessible memory for a machine with a memory management unit.
Any normally competent compiler should detect this form of pointer abuse,
and report the use of an uninitialized variable.
Correct use must be either:
int a,b,c,*p =3D c;
*p =3D a+b; // Assign to c
or
int a,b,*p;
p =3D new int;
*p =3D a+b;
delete p;
/Per W
On Fri, 14 Mar 2003, jo mateix wrote:
> Hello.
> I don't see any problem with your example (except for the non-standard
"void
> main"). Tell me if I'm wrong:
> A pointer in the stack is the same thing as an automatic variable in th=
e
> stack: it is released when you go out of scope, so no problem.
Furthermore:
> if you are concerned about eficiency, you shouldn't use the free store
> unless you need it (for example when using dinamically allocated object=
s).
> If you all don't aggree, please let me know. Thanks.
>
>
>
> >From: Andr=E9 Mac=E1rio Barros <and...@su...>
> >To: "Dev-Cpp-Users" <dev...@li...>
> >Subject: [Dev-C++] Correct use of pointers
> >Date: Thu, 13 Mar 2003 21:46:02 -0300
> >
> >Gentlemen,
> >
> > Even in the Ritchie=B4s books sometimes I fount .C codes using "wi=
ld
> >pointers" or something
> >like:
> >****************
> >void main()
> >{
> > int *p, a, b;
> >
> > a=3D2;
> > b=3D3;
> > *p =3D a + b;
> >
> > ...
> >}
> >****************
> >But... in large programs we can have problems.
> >
> > Would I be corret, allways, if I use insted a code like:
> >*********************
> >#include <stdlib.h> /* or alloc.h */
> >
> >void main()
> >{
> > int *p, a, b;
> >
> > p=3Dmalloc(1*sizeof(int));
> > a=3D2;
> > b=3D3;
> > *p =3D a + b;
> > ...
> > free(p);
> >}
> >******************
> >1. It=B4s not necessary, but is it tecnically correct?
> >2. Is there any point, where the previous memory allocation is mandato=
ry?
> >
> >Thanks
> >Andre
> >
> >
> >
> >
> >-------------------------------------------------------
> >This SF.net email is sponsored by:Crypto Challenge is now open!
> >Get cracking and register here for some mind boggling fun and
> >the chance of winning an Apple iPod:
> >http://ads.sourceforge.net/cgi-bin/redirect.pl?thaw0031en
> >_______________________________________________
> >Dev-cpp-users mailing list
> >Dev...@li...
> >TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> >https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
>
> _________________________________________________________________
> The new MSN 8: smart spam protection and 2 months FREE*
> http://join.msn.com/?page=3Dfeatures/junkmail
>
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by:Crypto Challenge is now open!
> Get cracking and register here for some mind boggling fun and
> the chance of winning an Apple iPod:
> http://ads.sourceforge.net/cgi-bin/redirect.pl?thaw0031en
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
-------------------------------------------------------
This SF.net email is sponsored by:Crypto Challenge is now open!
Get cracking and register here for some mind boggling fun and
the chance of winning an Apple iPod:
http://ads.sourceforge.net/cgi-bin/redirect.pl?thaw0031en
_______________________________________________
Dev-cpp-users mailing list
Dev...@li...
TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|