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
Nov 14 '05
33 2726
Chris Fogelklou <ch************ *@comhem.se> 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 (Zo**********@d aimlerchrysler. com)
"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
"Chris Fogelklou" <ch************ *@comhem.se> wrote in message
news:o3******** ************@ne wsc.telia.net.. . "Thomas Stegen" <ts*****@cis.st rath.ac.uk> wrote in message news:c5******** ****@ID-228872.news.uni-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
Sorry... meant "then this is also BAD practice."
"CBFalconer " <cb********@yah oo.com> wrote in message
news:40******** *******@yahoo.c om... Chris Fogelklou wrote: "Chris Fogelklou" <ch************ *@comhem.se> 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.
Don't worry... I'm not getting antsy (lots of smileys in my email to her).
But she's the expert and I'm just the user. Good guess that it is an
embedded system; that it is!
It's easier to ask here and get exact references than to say "but I
think..." (I made the "but I think" mistake on this NG last week :)
Next time I'll put the question in the post as well.
Cheers, Chris
In <c5************ @ID-228872.news.uni-berlin.de> Thomas Stegen <ts*****@cis.st rath.ac.uk> writes: 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.
Nope, it doesn't. It says:
1 A pointer to void may be converted to or from a pointer to any
incomplete or object type. A pointer to any incomplete or
object type may be converted to a pointer to void and back again;
the result shall compare equal to the original pointer.
But there is no mention that the conversion is automatic. Compare with
5 An integer may be converted to any pointer type. Except as
previously specified, the result is implementation-defined,
might not be correctly aligned, might not point to an entity of
the referenced type, and might be a trap representation.
6 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.
7 A pointer to an object or incomplete type may be converted to
a pointer to a different object or incomplete type.
Do you mean that these conversions are automatic, too?
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
In <40************ ***@yahoo.com> CBFalconer <cb********@yah oo.com> writes: Chris Fogelklou wrote: "Chris Fogelklou" <ch************ *@comhem.se> 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.
It sounds more like someone confusing the C and C++ rules.
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
In <c5**********@n ews-reader4.wanadoo .fr> "Régis Troadec" <re**@wanadoo.f r> writes: "Chris Fogelklou" <ch************ *@comhem.se> a écrit dans le message de news:3H******* *************@n ewsc.telia.net. ..
You're WRONG!
void* is implicitly converted to the destination pointer type (C99 §6.3.2.3 and §7.20.3.1).
You're WRONG!
There is NOTHING in C99 §6.3.2.3 or §7.20.3.1 supporting your assertion.
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
"Dan Pop" <Da*****@cern.c h> wrote in message
news:c5******** **@sunnews.cern .ch... In <c5************ @ID-228872.news.uni-berlin.de> Thomas Stegen
<ts*****@cis.st rath.ac.uk> writes: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.
Nope, it doesn't. It says:
1 A pointer to void may be converted to or from a pointer to any incomplete or object type. A pointer to any incomplete or object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.
But there is no mention that the conversion is automatic. Compare with
5 An integer may be converted to any pointer type. Except as previously specified, the result is implementation-defined, might not be correctly aligned, might not point to an entity of the referenced type, and might be a trap representation.
6 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.
7 A pointer to an object or incomplete type may be converted to a pointer to a different object or incomplete type.
Do you mean that these conversions are automatic, too?
Looks to me like if 1.) is, then 7.) is as well. Both 5.) and 6.) claim
that the result is implementation defined and the behaviour might be
undefined.
Dan Pop wrote: In <c5************ @ID-228872.news.uni-berlin.de> Thomas Stegen <ts*****@cis.st rath.ac.uk> writes:
Nope, it doesn't. It says:
I gave the other relevant sections in a different post.
--
Thomas.
Chris Fogelklou wrote: Looks to me like if 1.) is, then 7.) is as well. Both 5.) and 6.) claim that the result is implementation defined and the behaviour might be undefined.
The right section to look at would be 6.5.4 and 6.5.16.1.
--
Thomas.
"Dan Pop" <Da*****@cern.c h> a écrit dans le message de
news:c5******** **@sunnews.cern .ch... In <c5**********@n ews-reader4.wanadoo .fr> "Régis Troadec"
<re**@wanadoo.f r> writes:
"Chris Fogelklou" <ch************ *@comhem.se> a écrit dans le message de news:3H******* *************@n ewsc.telia.net. ..
You're WRONG! void* is implicitly converted to the destination pointer type (C99
§6.3.2.3and §7.20.3.1).
You're WRONG!
There is NOTHING in C99 §6.3.2.3 or §7.20.3.1 supporting your assertion.
Let's correct it : void * may be converted to a pointer to any incomplete or
object type. As well, void* may be converted from a pointer to any
incomplete or object type. This is what I read in C99 §6.3.2.3.
You were probably hurt by seeing "void* IS ... converted".
Regis Dan -- Dan Pop DESY Zeuthen, RZ group Email: Da*****@ifh.de This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: bradleyc |
last post by:
I have seen this is mainly linked lists ... something like
struct NodeType
{
int current
NodeType *next
}
So you are obviously able to use the struct within itself?
|
by: Angus Comber |
last post by:
Hello
I have received a lot of help on my little project here. Many thanks.
I have a struct with a string and a long member. I have worked out how to
qsort the struct on both members. I can do a bsearch on the long member
(nKey) but I am struggling to do a search using the string member.
The code I am running appears below. It doesn't crash or anything. It is
just that when I do the last bsearch using "192.168.1.3" I SHOULD find...
|
by: Fernan Bolando |
last post by:
What is the best way of passing a structure to a function currently I do
by it passing a pointer to the structe.
sample code below
int main()
{
struct sample_struct a_struct, *point_struct;
point_struct = &a_struct;
|
by: Michael B Allen |
last post by:
Consider the following structures:
struct bar {
int i;
float f;
unsigned char tail;
};
struct foo {
struct bar b1;
unsigned char _b1tail;
|
by: Jón Sveinsson |
last post by:
Hello everone
I have code from c which I need to convert to c#, the c
code is following, as you can see there is a struct
inside struct
typedef struct
{
some variables....
| |
by: Okko Willeboordse |
last post by:
typedef struct _RDATA {
struct RDATA *pRDATA;
} RDATA, *PRDATA;
How to compile with a C++ compiler?
|
by: Ben |
last post by:
Hello,
Is it possible to define a struct that has a type of itself in it?
So is it possible and if so how to do what I'm trying to do here?:
typedef struct mystruct {
int a;
mystruct *b;
} mystruct;
|
by: kritikapoor |
last post by:
Hi,
i have a structure like this:
struct SC
{
const int address ;
const int pad ;
|
by: ice88 |
last post by:
i have problem in define struct within struct
struct student_detail
{
int id;
string name;
};
struct subject_detail
|
by: metalheadstorm |
last post by:
There isn't much explaining to this, this is what I have:
public struct PACKET_HEADER
{
public string computerIp;
public string computerName;
public string computerCustomName;
};
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |