Hi,
I receive a segmentation violation when attempting to malloc an integer.
I’ve never had this happen. Does anyone know under what conditions
a malloc could fail?
Here’s the code snippet:
int *steer_pos;
steer_pos = (int *)malloc(sizeof(int));
Thanks,
Alec
Does this code fail if it’s standing alone? Or in the context of a larger
program? One reason malloc can fail is that you’ve gotten some heap
corruption somewhere in your program (possibly by writing off the start/end
of a buffer, etc). You should read the whitepaper
http://qdn.qnx.com/support/docs/neutrino/prog/hat.html for more information.
cheers,
Kris
“Alec Gorjestani” <alecg@me.umn.edu> wrote in message
news:3C922126.4040008@me.umn.edu…
Hi,
I receive a segmentation violation when attempting to malloc an integer.
I’ve never had this happen. Does anyone know under what conditions
a malloc could fail?
Here’s the code snippet:
int *steer_pos;
steer_pos = (int *)malloc(sizeof(int));
Thanks,
Alec
Alec Gorjestani <alecg@me.umn.edu> wrote in
news:3C922126.4040008@me.umn.edu:
Hi,
I receive a segmentation violation when attempting to malloc an integer.
I’ve never had this happen. Does anyone know under what conditions
a malloc could fail?
Here’s the code snippet:
int *steer_pos;
steer_pos = (int *)malloc(sizeof(int));
Don’t cast malloc’s return (evil), it masks errors when you compile if
you’re not including the correct headers.
Also, how is this code snippet being called, is it just a standard C
program (ie. hello world)?
\
Cheers,
Adam
With a PC, I always felt limited by the software available.
On Unix, I am limited only by my knowledge.
–Peter J. Schoenster <pschon@baste.magibox.net>
“Adam Mallory” <amallory@qnx.com> wrote in message
news:Xns91D69C5CE3744amalloryqnxcom@209.226.137.4…
Alec Gorjestani <> alecg@me.umn.edu> > wrote in
news:> 3C922126.4040008@me.umn.edu> :
int *steer_pos;
steer_pos = (int *)malloc(sizeof(int));
Don’t cast malloc’s return (evil), it masks errors when you compile if
you’re not including the correct headers.
Excuse me! Please explain.
In my book it’s a no-no NOT to type-cast the return of malloc().
–
Bill Caroselli – 1(626) 824-7983
Q-TPS Consulting
QTPS@EarthLink.net
You shouldn’t need to anymore. Malloc and friends all return void pointers
which don’t need to be cast to avoid warnings. I don’t know if casting is
necessarily ‘evil’ per se… I have mixed feelings about it myself -
sometimes the cast serves as a bit of code level ‘documentation’ as to what
is being assigned but sometimes it just adds clutter. Is there some
compelling technical reason NOT to cast? I don’t see how you would be
including a header with a spurious malloc that would cause a warning/error
if the return were uncast - I would think most mallocs would return void *
regardless.
cheers,
Kris
“Bill Caroselli” <qtps@earthlink.net> wrote in message
news:a78s0b$m03$1@inn.qnx.com…
“Adam Mallory” <> amallory@qnx.com> > wrote in message
news:Xns91D69C5CE3744amalloryqnxcom@209.226.137.4…
Alec Gorjestani <> alecg@me.umn.edu> > wrote in
news:> 3C922126.4040008@me.umn.edu> :
int *steer_pos;
steer_pos = (int *)malloc(sizeof(int));
Don’t cast malloc’s return (evil), it masks errors when you compile if
you’re not including the correct headers.
Excuse me! Please explain.
In my book it’s a no-no NOT to type-cast the return of malloc().
–
Bill Caroselli – 1(626) 824-7983
Q-TPS Consulting
QTPS@EarthLink.net
\
“Bill Caroselli” <qtps@earthlink.net> wrote in
news:a78s0b$m03$1@inn.qnx.com:
Don’t cast malloc’s return (evil), it masks errors when you compile if
you’re not including the correct headers.
Excuse me! Please explain.
In my book it’s a no-no NOT to type-cast the return of malloc().
Casting malloc() can mask errors, such as not including the correct header.
If you do not include the correct header for malloc, the compiler defaults
to a return type of int, which is NOT a void*. This is definately a “no-
no” - read comp.lang.c for more information/ANSI C tips.
\
Cheers,
Adam
With a PC, I always felt limited by the software available.
On Unix, I am limited only by my knowledge.
–Peter J. Schoenster <pschon@baste.magibox.net>
“Adam Mallory” <amallory@qnx.com> wrote in message
news:Xns91D76B93B6916amalloryqnxcom@209.226.137.4…
“Bill Caroselli” <> qtps@earthlink.net> > wrote in
news:a78s0b$m03$> 1@inn.qnx.com> :
Don’t cast malloc’s return (evil), it masks errors when you compile if
you’re not including the correct headers.
Excuse me! Please explain.
In my book it’s a no-no NOT to type-cast the return of malloc().
Casting malloc() can mask errors, such as not including the correct
header.
If you do not include the correct header for malloc, the compiler defaults
to a return type of int, which is NOT a void*. This is definately a
“no-
no” - read comp.lang.c for more information/ANSI C tips.
Wouldn’t the compile complain of undefined function call?
–
Cheers,
Adam
QNX Software Systems Ltd.
[ > amallory@qnx.com > ]
With a PC, I always felt limited by the software available.
On Unix, I am limited only by my knowledge.
–Peter J. Schoenster <> pschon@baste.magibox.net
Adam Mallory wrote:
“Bill Caroselli” <> qtps@earthlink.net> > wrote in
news:a78s0b$m03$> 1@inn.qnx.com> :
Don’t cast malloc’s return (evil), it masks errors when you compile if
you’re not including the correct headers.
Excuse me! Please explain.
In my book it’s a no-no NOT to type-cast the return of malloc().
Casting malloc() can mask errors, such as not including the correct header.
If you do not include the correct header for malloc, the compiler defaults
to a return type of int, which is NOT a void*. This is definately a “no-
no” - read comp.lang.c for more information/ANSI C tips.
I just feel I need to make a small note here. If you don’t cast malloc,
your program can’t compile as a c++ program.
Thus casting malloc at least in “example code” where you don’t know if
the user use c or c++ is a good idear :}
Martin Tilsted
Previously, Martin Tilsted wrote in qdn.public.qnxrtp.os:
Adam Mallory wrote:
“Bill Caroselli” <> qtps@earthlink.net> > wrote in
news:a78s0b$m03$> 1@inn.qnx.com> :
Don’t cast malloc’s return (evil), it masks errors when you compile if
you’re not including the correct headers.
Excuse me! Please explain.
In my book it’s a no-no NOT to type-cast the return of malloc().
Casting malloc() can mask errors, such as not including the correct header.
If you do not include the correct header for malloc, the compiler defaults
to a return type of int, which is NOT a void*. This is definately a “no-
no” - read comp.lang.c for more information/ANSI C tips.
I just feel I need to make a small note here. If you don’t cast malloc,
your program can’t compile as a c++ program.
Thus casting malloc at least in “example code” where you don’t know if
the user use c or c++ is a good idear :}
Martin Tilsted
And I (almost) always use C++.
Bill Caroselli <QTPS@earthlink.net> wrote:
And I (almost) always use C++.
Isn’t it a no-no in your book to use malloc() then?
–
Wojtek Lerch QNX Software Systems Ltd.
Good point! In C++ I would use new.
BTW, In C++ you can’t call malloc() without including a header for it.
–
Bill Caroselli – 1(626) 824-7983
Q-TPS Consulting
QTPS@EarthLink.net
“Wojtek Lerch” <wojtek_l@yahoo.ca> wrote in message
news:a7amcp$dk1$1@nntp.qnx.com…
Bill Caroselli <> QTPS@earthlink.net> > wrote:
And I (almost) always use C++.
Isn’t it a no-no in your book to use malloc() then? >
–
Wojtek Lerch QNX Software Systems Ltd.