Thread: RE: [Dev-C++] Correct use of pointers
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Somanathan K. <som...@wi...> - 2003-03-14 06:36:30
|
I couldn't understand how the first one will work(without allocating =
memory
for p). This should crash soon or later.
Somanathan
-----Original Message-----
From: Andr=E9 Mac=E1rio Barros [mailto:and...@su...]=20
Sent: Friday, March 14, 2003 6:16 AM
To: Dev-Cpp-Users
Subject: [Dev-C++] Correct use of pointers
Gentlemen,
Even in the Ritchie=B4s books sometimes I fount .C codes using "wild
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 =
mandatory?
Thanks
Andre
-------------------------------------------------------
This SF.net email is sponsored by:Crypto Challenge is now open!=20
Get cracking and register here for some mind boggling fun and=20
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
|
|
From: jo m. <non...@ho...> - 2003-03-14 19:58:07
|
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 the
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 objects).
If you all don't aggree, please let me know. Thanks.
>From: André Macário 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´s books sometimes I fount .C codes using "wild
>pointers" or something
>like:
>****************
>void main()
>{
> int *p, a, b;
>
> a=2;
> b=3;
> *p = 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=malloc(1*sizeof(int));
> a=2;
> b=3;
> *p = a + b;
> ...
> free(p);
>}
>******************
>1. It´s not necessary, but is it tecnically correct?
>2. Is there any point, where the previous memory allocation is mandatory?
>
>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=features/junkmail
|
|
From: Per W. <pw...@ia...> - 2003-03-14 20:20:03
|
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 data
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 "v=
oid
> main"). Tell me if I'm wrong:
> A pointer in the stack is the same thing as an automatic variable in the
> stack: it is released when you go out of scope, so no problem. Furthermor=
e:
> if you are concerned about eficiency, you shouldn't use the free store
> unless you need it (for example when using dinamically allocated objects)=
=2E
> 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 "wild
> >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 mandatory=
?
> >
> >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
>
|
|
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
|
|
From: Per W. <pw...@ia...> - 2003-03-14 20:54:24
|
No, if the compiler doesn't complain about my clumsiness, I would quickly
change to a better one :-)
/Per W
On Fri, 14 Mar 2003, Hal Kellerman wrote:
> 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
>
>
|
|
From: <rol...@mi...> - 2003-03-14 20:59:03
|
> > Any normally competent compiler should detect this form of pointer abuse, > and report the use of an uninitialized variable. > Dev-C++ / g++ does not report, but Visual Studio C++ does. "warning C4700: local variable 'p' used without having been initialized" Is there any compiler setting to make DevC++ report also like Microsoft does -- Roland Strålberg microshade mailto:ro...@st... http://stralberg.kalix.net |
|
From: Ioannis V. <iv...@em...> - 2003-03-15 23:31:02
|
-----------------------Original Message-----------------------
>From: Andr=E9 Mac=E1rio Barros [mailto:and...@su...]
>To: Dev-Cpp-Users [mailto:dev...@li...]
>Date: 14/3/2003 3:31:00 =F0=EC
>Subject: [Dev-C++] Correct use of pointers
>
>Gentlemen,
>
> Even in the Ritchie=B4s books sometimes I fount .C codes using "wild
>pointers" or something
>like:
>****************
>void main()
=20
int main()
>{
> int *p, a, b;
>
> a=3D2;
> b=3D3;
> *p =3D a + b;
>
> ...
>}
>****************
The above is a mistake. I do not recall something like that in K&R2. Are
you sure it is not written something like this?:
int p[10], a, b;
a=3D2;
b=3D3;
*p=3Da+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?
It is correct and required. malloc() could be used like this:
p=3Dmalloc(sizeof(int));
or
p=3Dmalloc(sizeof(*p));
>2. Is there any point, where the previous memory allocation is mandatory=
?
=20
ALWAYS.
Ioannis Vranos=20
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL: http://run.to/noicys=20
|
|
From: Ioannis V. <iv...@in...> - 2003-03-20 14:09:26
|
-----------------------Original Message-----------------------
>From: Andr=E9 Mac=E1rio Barros [mailto:and...@su...]
>To: Dev-Cpp-Users [mailto:dev...@li...]
>Date: 14/3/2003 3:31:00 =F0=EC
>Subject: [Dev-C++] Correct use of pointers
>
>Gentlemen,
>
> Even in the Ritchie=B4s books sometimes I fount .C codes using "wild
>pointers" or something
>like:
>****************
>void main()
=20
int main()
>{
> int *p, a, b;
>
> a=3D2;
> b=3D3;
> *p =3D a + b;
>
> ...
>}
>****************
The above is a mistake. I do not recall something like that in K&R2. Are
you sure it is not written something like this?:
int p[10], a, b;
a=3D2;
b=3D3;
*p=3Da+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?
It is correct and required. malloc() could be used like this:
p=3Dmalloc(sizeof(int));
or
p=3Dmalloc(sizeof(*p));
>2. Is there any point, where the previous memory allocation is mandatory=
?
=20
ALWAYS.
Ioannis Vranos=20
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL: http://run.to/noicys=20
|