473,795 Members | 2,766 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
In article <GS************ ********@newsc. telia.net>
Chris Fogelklou <ch************ *@comhem.se> writes:
Is there really that much wrong with making code compatible with both C and
C++? If we leave the cast in there, the code could be copied and pasted
into a C++ library verbatim. ...


There is nothing inherently wrong with it, but I see it as a waste of
effort in most cases.

There are quite a few subtle differences between C and C++, so that
one can write programs that are syntactically valid in both languages,
compile in both languages, yet mean something different in each
language. The problem is similar to getting Americans and Brits
to understand each other: when an American committee "tables" a
subject it is put aside to stop discussion, while the British
committee "tables" the subject to bring it up and begin discussion.

(I posted an example earlier today in which "sizeof type" changes
when the code is compiled in the alternate language. Look for
"struct scopetrick".)

More typically, good C code often fails to compile at all as C++
code (because -- among other things -- it omits casts that C++
requires); and of course good C++ code generally uses "new" instead
of malloc, or -- even better -- uses container classes.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #31
Chris Torek wrote:
Chris Fogelklou <ch************ *@comhem.se> writes:
Is there really that much wrong with making code compatible with
both C and C++? If we leave the cast in there, the code could
be copied and pasted into a C++ library verbatim. ...


There is nothing inherently wrong with it, but I see it as a
waste of effort in most cases.

There are quite a few subtle differences between C and C++, so
that one can write programs that are syntactically valid in both
languages, compile in both languages, yet mean something
different in each language. The problem is similar to getting
Americans and Brits to understand each other: when an American

....

Back when I was young and in my prime there was this amiable, but
poverty stricken Yank who met this foxy Brit chick. Nature took
its course, but then she told him: "I am getting divorced, with a
big settlement. Meet me after the divorce, but meanwhile I have
to stay pristine." And she handed him an envelope and told him to
open it after the day was over.

Well, he followed instructions, and opened it and read: "Until
7/6, noon, in Trafalger Square.". Since it was January, he went
off and did other things, such as roping calves etc., and returned
to London on 7/6. He never found her again.

She meant every word of it, and she was there on 7/6, but never
found him again.

Your mission, should you care to accept it, is to explain this
paradox. Alternate timelines or universes will not be accepted.

--
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
Nov 14 '05 #32
On Fri, 16 Apr 2004 04:29:22 GMT, CBFalconer <cb********@yah oo.com>
wrote:
Chris Torek wrote:
Chris Fogelklou <ch************ *@comhem.se> writes:
Is there really that much wrong with making code compatible with
both C and C++? If we leave the cast in there, the code could
be copied and pasted into a C++ library verbatim. ...


There is nothing inherently wrong with it, but I see it as a
waste of effort in most cases.

There are quite a few subtle differences between C and C++, so
that one can write programs that are syntactically valid in both
languages, compile in both languages, yet mean something
different in each language. The problem is similar to getting
Americans and Brits to understand each other: when an American

...

Back when I was young and in my prime there was this amiable, but
poverty stricken Yank who met this foxy Brit chick. Nature took
its course, but then she told him: "I am getting divorced, with a
big settlement. Meet me after the divorce, but meanwhile I have
to stay pristine." And she handed him an envelope and told him to
open it after the day was over.

Well, he followed instructions, and opened it and read: "Until
7/6, noon, in Trafalger Square.". Since it was January, he went
off and did other things, such as roping calves etc., and returned
to London on 7/6. He never found her again.

She meant every word of it, and she was there on 7/6, but never
found him again.

Your mission, should you care to accept it, is to explain this
paradox. Alternate timelines or universes will not be accepted.


Too easy. June 7th != July 6th. However, your language point is
evident.

--
Sev
Nov 14 '05 #33
In article <9g************ *************** *****@4ax.com>,
Severian <se******@chlam ydia-is-not-a-flower.com> wrote:
On Fri, 16 Apr 2004 04:29:22 GMT, CBFalconer <cb********@yah oo.com>
wrote:
Chris Torek wrote:
Chris Fogelklou <ch************ *@comhem.se> writes:

Is there really that much wrong with making code compatible with
both C and C++? If we leave the cast in there, the code could
be copied and pasted into a C++ library verbatim. ...

There is nothing inherently wrong with it, but I see it as a
waste of effort in most cases.

There are quite a few subtle differences between C and C++, so
that one can write programs that are syntactically valid in both
languages, compile in both languages, yet mean something
different in each language. The problem is similar to getting
Americans and Brits to understand each other: when an American

...

Back when I was young and in my prime there was this amiable, but
poverty stricken Yank who met this foxy Brit chick. Nature took
its course, but then she told him: "I am getting divorced, with a
big settlement. Meet me after the divorce, but meanwhile I have
to stay pristine." And she handed him an envelope and told him to
open it after the day was over.

Well, he followed instructions, and opened it and read: "Until
7/6, noon, in Trafalger Square.". Since it was January, he went
off and did other things, such as roping calves etc., and returned
to London on 7/6. He never found her again.

She meant every word of it, and she was there on 7/6, but never
found him again.

Your mission, should you care to accept it, is to explain this
paradox. Alternate timelines or universes will not be accepted.


Too easy. June 7th != July 6th. However, your language point is
evident.


Many years ago, a C++ framework that I investigated for its usefulness
changed a constructor from

Point::Point (int width, int height);

to

Point::Point (int vertical, int horizontal);

I decided not to use that framework...
Nov 14 '05 #34

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3193
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?
4
5604
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...
6
15234
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;
3
2453
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;
1
8855
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....
1
401
by: Okko Willeboordse | last post by:
typedef struct _RDATA { struct RDATA *pRDATA; } RDATA, *PRDATA; How to compile with a C++ compiler?
10
1508
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;
3
1820
by: kritikapoor | last post by:
Hi, i have a structure like this: struct SC { const int address ; const int pad ;
2
2756
by: ice88 | last post by:
i have problem in define struct within struct struct student_detail { int id; string name; }; struct subject_detail
1
1428
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; };
0
9519
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,...
0
10436
Oralloy
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...
0
10213
jinu1996
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...
1
10163
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,...
1
7538
isladogs
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...
0
6780
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();...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3722
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.