Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct));

Reply
Thread Tools

struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct));

 
 
Chris Fogelklou
Guest
Posts: n/a
 
      04-15-2004
What is wrong with the above?

Don't worry, I already know (learned my lesson last week.) It is for the
benefit of our resident compiler guru who seems to think you need the cast.
I thought it too, up until I started posting here!

Thanks,

Chris

 
Reply With Quote
 
 
 
 
Chris Fogelklou
Guest
Posts: n/a
 
      04-15-2004
"Chris Fogelklou" <(E-Mail Removed)> wrote in message
news:7lxfc.89758$(E-Mail Removed)...
> What is wrong with the above?
>
> Don't worry, I already know (learned my lesson last week.) It is for the
> benefit of our resident compiler guru who seems to think you need the

cast.
> I thought it too, up until I started posting here!
>
> Thanks,
>
> Chris
>

Even better, here is the email itself. Please help!

<start email>
First, Casts from void * are necessary both in C and C++. This is from the
same reason you told me: otherwise, if the compiler wouldn't
know what is the size of the object which is pointed at, it might get
confused with arithmetic operations. You can ask him where did he learned
this rule from. tell me if you want me to correspond directly with him.

Second, a possible reason to use integral types for addresses rather than
void *, is that in order to perform bitwise operations on an
address, you have to use operators of an integral type.
<end email>

 
Reply With Quote
 
 
 
 
Régis Troadec
Guest
Posts: n/a
 
      04-15-2004

"Chris Fogelklou" <(E-Mail Removed)> a écrit dans le message de
news:Snxfc.89759$(E-Mail Removed)...
> "Chris Fogelklou" <(E-Mail Removed)> wrote in message


Hi,

> news:7lxfc.89758$(E-Mail Removed)...
> > What is wrong with the above?
> >
> > Don't worry, I already know (learned my lesson last week.) It is for

the
> > benefit of our resident compiler guru who seems to think you need the

> cast.
> > I thought it too, up until I started posting here!
> >
> > Thanks,
> >
> > Chris
> >

> Even better, here is the email itself. Please help!
>
> <start email>
> First, Casts from void * are necessary both in C and C++. This is from the


Casts from void* are necessary in C++, not in C.

> same reason you told me: otherwise, if the compiler wouldn't
> know what is the size of the object which is pointed at, it might get
> confused with arithmetic operations. You can ask him where did he learned
> this rule from. tell me if you want me to correspond directly with him.
>
> Second, a possible reason to use integral types for addresses rather than
> void *, is that in order to perform bitwise operations on an
> address, you have to use operators of an integral type.
> <end email>
>


Regis


 
Reply With Quote
 
Chris Fogelklou
Guest
Posts: n/a
 
      04-15-2004

"Régis Troadec" <(E-Mail Removed)> wrote in message
news:c5m8hk$hc5$(E-Mail Removed)...
>
> "Chris Fogelklou" <(E-Mail Removed)> a écrit dans le message de
> news:Snxfc.89759$(E-Mail Removed)...
> > "Chris Fogelklou" <(E-Mail Removed)> wrote in message

>
> Hi,
>
> > news:7lxfc.89758$(E-Mail Removed)...
> > > What is wrong with the above?
> > >
> > > Don't worry, I already know (learned my lesson last week.) It is for

> the
> > > benefit of our resident compiler guru who seems to think you need the

> > cast.
> > > I thought it too, up until I started posting here!
> > >
> > > Thanks,
> > >
> > > Chris
> > >

> > Even better, here is the email itself. Please help!
> >
> > <start email>
> > First, Casts from void * are necessary both in C and C++. This is from

the
>
> Casts from void* are necessary in C++, not in C.


I am aware... I'm fishing for more info! Perhaps if I reposted again and
said you were wrong I'll get more...

You're WRONG!

>
> > same reason you told me: otherwise, if the compiler wouldn't
> > know what is the size of the object which is pointed at, it might get
> > confused with arithmetic operations. You can ask him where did he

learned
> > this rule from. tell me if you want me to correspond directly with him.
> >
> > Second, a possible reason to use integral types for addresses rather

than
> > void *, is that in order to perform bitwise operations on an
> > address, you have to use operators of an integral type.
> > <end email>
> >

>
> Regis
>
>


 
Reply With Quote
 
Thomas Stegen
Guest
Posts: n/a
 
      04-15-2004
Chris Fogelklou wrote:

> Even better, here is the email itself. Please help!
>
> <start email>
> First, Casts from void * are necessary both in C and C++. This is from the
> same reason you told me: otherwise, if the compiler wouldn't
> know what is the size of the object which is pointed at, it might get
> confused with arithmetic operations. You can ask him where did he learned
> this rule from. tell me if you want me to correspond directly with him.


Yes, please tell her to come here. On the other hand, don't, tell
her to read the archives. This conversation is getting old

the ISO/IEC 9899:1999 standard section 6.3.2.3 has what you want. That
is C99 btw. Don't know the clause in C89.

>
> Second, a possible reason to use integral types for addresses rather than
> void *, is that in order to perform bitwise operations on an
> address, you have to use operators of an integral type.


This much is true. But it is in implementation defined behaviour land,
most compilers behave very nicely when it comes to this though.

--
Thomas.


 
Reply With Quote
 
Thomas Stegen
Guest
Posts: n/a
 
      04-15-2004
Chris Fogelklou wrote:

> What is wrong with the above?
>
> Don't worry, I already know (learned my lesson last week.) It is for the
> benefit of our resident compiler guru who seems to think you need the cast.
> I thought it too, up until I started posting here!
>


struct my_struct *p = (struct my_struct *)malloc(sizeof(struct
my_struct));

There is nothing wrong with the above. It is considered a better
idiom to use

struct my_struct *p = malloc(sizeof *p);

The argument that the compiler does not know the type of the object
is bogus since all the information is already there. The compiler knows
the type of p, it knows the type of the value returned by malloc,
nothing is missing.

In my previous reply I mentioned 6.3.2.3, I should also have mentioned
clause 6.5.4 and 6.5.16.1 which is where the "no cast required" part is.

--
Thomas.

 
Reply With Quote
 
Régis Troadec
Guest
Posts: n/a
 
      04-15-2004

"Chris Fogelklou" <(E-Mail Removed)> a écrit dans le message de
news:3Hxfc.89762$(E-Mail Removed)...

Yes, sorry for that, i read your post too quickly,

> "Régis Troadec" <(E-Mail Removed)> wrote in message
> news:c5m8hk$hc5$(E-Mail Removed)...
> >
> > "Chris Fogelklou" <(E-Mail Removed)> a écrit dans le message de
> > news:Snxfc.89759$(E-Mail Removed)...
> > > "Chris Fogelklou" <(E-Mail Removed)> wrote in message

> >
> > Hi,
> >
> > > news:7lxfc.89758$(E-Mail Removed)...
> > > > What is wrong with the above?
> > > >
> > > > Don't worry, I already know (learned my lesson last week.) It is

for
> > the
> > > > benefit of our resident compiler guru who seems to think you need

the
> > > cast.
> > > > I thought it too, up until I started posting here!
> > > >
> > > > Thanks,
> > > >
> > > > Chris
> > > >
> > > Even better, here is the email itself. Please help!
> > >
> > > <start email>
> > > First, Casts from void * are necessary both in C and C++. This is from

> the
> >
> > Casts from void* are necessary in C++, not in C.

>
> I am aware... I'm fishing for more info! Perhaps if I reposted again and
> said you were wrong I'll get more...
>
> You're WRONG!


void* is implicitly converted to the destination pointer type (C99 §6.3.2.3
and §7.20.3.1).

> >
> > > same reason you told me: otherwise, if the compiler wouldn't
> > > know what is the size of the object which is pointed at, it might get
> > > confused with arithmetic operations.


sizeof ?

>You can ask him where did he
> learned
> > > this rule from. tell me if you want me to correspond directly with

him.
> > >
> > > Second, a possible reason to use integral types for addresses rather

> than
> > > void *, is that in order to perform bitwise operations on an
> > > address, you have to use operators of an integral type.


It's also said in the standard (C99), part 6 of §6.3.2.3 that :
"Any pointer type may be converted to an integer type. Except as previously
specified, the result is implementation-defined. If the result cannot be
represented in the integer type, the behavior is undefined. The result need
not be in the range of values of any integer type."

Is he/she sure that he/she can always hold his adressses with an integral
type ? It implies sizeof(the famous integral type) >= sizeof(void*).

There is a way in C99 to hold pointers and adresses with integers thanks to
the types intptr_t and uintptr_t. (C99 7.18.1.4), but I'm not sure it's
possible in C90..

Regis

> > > <end email>
> > >

> >
> > Regis
> >
> >

>



 
Reply With Quote
 
Chris Fogelklou
Guest
Posts: n/a
 
      04-15-2004
"Thomas Stegen" <(E-Mail Removed)> wrote in message
news:c5m9qn$3c0d0$(E-Mail Removed)-berlin.de...
> Chris Fogelklou wrote:
>
> > What is wrong with the above?
> >
> > Don't worry, I already know (learned my lesson last week.) It is for

the
> > benefit of our resident compiler guru who seems to think you need the

cast.
> > I thought it too, up until I started posting here!
> >

>
> struct my_struct *p = (struct my_struct *)malloc(sizeof(struct
> my_struct));
>
> There is nothing wrong with the above. It is considered a better
> idiom to use
>
> struct my_struct *p = malloc(sizeof *p);
>
> The argument that the compiler does not know the type of the object
> is bogus since all the information is already there. The compiler knows
> the type of p, it knows the type of the value returned by malloc,
> nothing is missing.
>
> In my previous reply I mentioned 6.3.2.3, I should also have mentioned
> clause 6.5.4 and 6.5.16.1 which is where the "no cast required" part is.
>
> --
> Thomas.
>


Hi Thomas,

Thanks! OK, then what about this:

int my_func(void *pinst)
{
my_struct *pthis = (my_struct *)pinst; //Cast
pthis->print("Lets see what's wrong with this!");
return 0;
}

If it is OK to never cast to/from void * as was made abundantly clear last
week, then this is also OK. However, pinst could be a run-time defineable
value.

Thanks!

Chris

 
Reply With Quote
 
Zoran Cutura
Guest
Posts: n/a
 
      04-15-2004
Chris Fogelklou <(E-Mail Removed)> wrote:
> int my_func(void *pinst)
> {
> my_struct *pthis = (my_struct *)pinst; //Cast
> pthis->print("Lets see what's wrong with this!");
> return 0;
> }
>
> If it is OK to never cast to/from void * as was made abundantly clear last
> week, then this is also OK. However, pinst could be a run-time defineable
> value.



It is absolutly ok to do this (presuming a correct definition of
my_struct) with or without a cast.

void * can be converted to from other object pointer types and the other
way round without casts in any circumstance. (function pointers are
different here, but that is another story).

And note that function parameters are always evaluated at runtime.

When my_func is called, the arguments to that call are evaluated and
copied into the parameters of the to be called function.

--
Z ((E-Mail Removed))
"LISP is worth learning for the profound enlightenment experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days." -- Eric S. Raymond
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      04-15-2004
Chris Fogelklou wrote:
> "Chris Fogelklou" <(E-Mail Removed)> wrote in message
>
> > What is wrong with the above?
> >
> > Don't worry, I already know (learned my lesson last week.) It
> > is for the benefit of our resident compiler guru who seems to
> > think you need the cast. I thought it too, up until I started
> > posting here!
> >

> Even better, here is the email itself. Please help!
>
> <start email>
> First, Casts from void * are necessary both in C and C++. This is
> from the same reason you told me: otherwise, if the compiler
> wouldn't know what is the size of the object which is pointed at,
> it might get confused with arithmetic operations. You can ask him
> where did he learned this rule from. tell me if you want me to
> correspond directly with him.
>
> Second, a possible reason to use integral types for addresses
> rather than void *, is that in order to perform bitwise operations
> on an address, you have to use operators of an integral type.
> <end email>


Don't get too antsy with him/her. This sounds like some system
programming operations on some embedded system, where the rules
are often different. The cast of the malloc remains unnecessary
(and don't put the question in the subject only).

Do learn to organize your questions into a single consistent
message, rather than a bit here and a bit there.

--
fix (vb.): 1. to paper over, obscure, hide from public view; 2.
to work around, in a way that produces unintended consequences
that are worse than the original problem. Usage: "Windows ME
fixes many of the shortcomings of Windows 98 SE". - Hutchison


 
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
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
Typedef A references struct B which references struct A which... DanielEKFA C++ 8 05-16-2005 10:26 AM
struct in struct Gunnar G C++ 14 06-02-2004 06:43 PM
implementing a templated struct within a templated struct RA Scheltema C++ 3 01-06-2004 11:25 AM
To coerce or not...? struct sockaddr vs struct sockaddr_in James Harris C Programming 4 10-09-2003 10:06 PM



Advertisments