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 33 2725
"Chris Fogelklou" <ch************ *@comhem.se> wrote in message
news:7l******** ************@ne wsc.telia.net.. . 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>
"Chris Fogelklou" <ch************ *@comhem.se> a écrit dans le message de
news:Sn******** ************@ne wsc.telia.net.. . "Chris Fogelklou" <ch************ *@comhem.se> wrote in message
Hi,
news:7l******** ************@ne wsc.telia.net.. . 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
"Régis Troadec" <re**@wanadoo.f r> wrote in message
news:c5******** **@news-reader3.wanadoo .fr... "Chris Fogelklou" <ch************ *@comhem.se> a écrit dans le message de news:Sn******** ************@ne wsc.telia.net.. . "Chris Fogelklou" <ch************ *@comhem.se> wrote in message Hi,
news:7l******** ************@ne wsc.telia.net.. . 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
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.
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.
"Chris Fogelklou" <ch************ *@comhem.se> a écrit dans le message de
news:3H******** ************@ne wsc.telia.net.. .
Yes, sorry for that, i read your post too quickly, "Régis Troadec" <re**@wanadoo.f r> wrote in message news:c5******** **@news-reader3.wanadoo .fr... "Chris Fogelklou" <ch************ *@comhem.se> a écrit dans le message de news:Sn******** ************@ne wsc.telia.net.. . "Chris Fogelklou" <ch************ *@comhem.se> wrote in message Hi,
news:7l******** ************@ne wsc.telia.net.. . > 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
"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
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.
--
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
"Régis Troadec" <re**@wanadoo.f r> wrote in message
news:c5******** **@news-reader4.wanadoo .fr... "Chris Fogelklou" <ch************ *@comhem.se> a écrit dans le message de news:3H******** ************@ne wsc.telia.net.. .
Yes, sorry for that, i read your post too quickly,
"Régis Troadec" <re**@wanadoo.f r> wrote in message news:c5******** **@news-reader3.wanadoo .fr... "Chris Fogelklou" <ch************ *@comhem.se> a écrit dans le message
de news:Sn******** ************@ne wsc.telia.net.. . > "Chris Fogelklou" <ch************ *@comhem.se> wrote in message
Hi,
> news:7l******** ************@ne wsc.telia.net.. . > > 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
Do you know if this was also a standard in C89/C90? If so, it would
strengthen my argument since the recent response was that "GCC-derived
compilers don't fully support C99 yet." Not sure if it's true... Any
thoughts?
Not sure what she meant with arithmetic operations... you don't normally do
arithmetic on pointers, but if you did, I guess you could just cast to an
integral type of the correct size, correct?
The fact is I am very uncomfortable holding addresses in an integral data
type. Usually we let the compiler choose the addressing scheme (depending
on the memory model) and it should (if it is smart) know how big to make
pointers. But if all of our generic pointers are integral and need to be
casted to pointers, it's our own code that must know what the memory model
is.
Thanks in advance!
Chris 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: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |