473,804 Members | 3,745 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

New book - 'C of Peril'

Hello All,

I've been compiling a series of pages about the bad functions and
mistakes that occur in C into a book, subsequently, I now have a moderate
little book of about 32 pages available for download as a PDF file at

http://www.pldaniels.com/c-of-peril

It's currently still being worked on and I'm always seeking out new
examples of C being used poorly or broken standard functions.

Regards.

--
PLDaniels - Software - Xamime
Unix systems Internet Development A.B.N. 19 500 721 806
PGP Public Key at http://www.pldaniels.com/gpg-keys.pld
Nov 14 '05
56 2608

"CBFalconer " <cb********@yah oo.com> wrote
> #define foo(x) do { bar(x); baz(x); } while(0)

Anyone know if there's a way to do this without relying on
do/while/if/for (etc)?

As far as reading the original macro definition is concerned, this
is a well known method, and should not cause any concern
whatsoever.

The fault lies with the language, rather than the original programmer.

Some construct such as #define foo(x,y){ code(x); morecode(y); } should be
used to define multi-line macros. Whoever designed the original preprocessor
forgot to include any support, and so we are stuck with the do while
workaround.

As for being a well-known method, it is a little quirk that experienced C
programmers will eventually pick up, but might puzzle programmers who only
use C rarely.
Nov 14 '05 #51
"Malcolm" <ma*****@55bank .freeserve.co.u k> writes:
"CBFalconer " <cb********@yah oo.com> wrote
>>>> #define foo(x) do { bar(x); baz(x); } while(0)
>>>
>>> Anyone know if there's a way to do this without relying on
>>> do/while/if/for (etc)?
>> As far as reading the original macro definition is concerned, this
is a well known method, and should not cause any concern
whatsoever.

The fault lies with the language, rather than the original programmer.

Some construct such as #define foo(x,y){ code(x); morecode(y); } should be
used to define multi-line macros. Whoever designed the original preprocessor
forgot to include any support, and so we are stuck with the do while
workaround.


The preprocessor expands macro invocations to arbitrary sequences of
tokens; it has no concept of higher-level constructs such as
statements and blocks. This makes it both powerful and dangerous.

It's not the preprocessor's fault that

#define foo(x,y){ code(x); morecode(y); }

doesn't always work. It's a consequence of the grammar of the
language itself, specifically the fact that a block cannot always be
substituted for a statement.
As for being a well-known method, it is a little quirk that experienced C
programmers will eventually pick up, but might puzzle programmers who only
use C rarely.


The "do { ... } while(0)" idiom is obscure at first glance, but *is*
well-known, and any C programmer should be able to understand it.
It's even in the FAQ (10.4). Programmers who only use C rarely are
likel to have bigger problems than understanding a do-while loop in a
macro definition.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #52
On Tue, 7 Dec 2004,Arthur J. O'Dwyer wrote:
list Arthur as a "Contributo r" and credit him ;-)


Bah. You asked for corrections, and I supplied some. No worries.


Exactly. I do believe it would be only fair that you are duly noted in
the credits since you did provide quite an number of corrections.

--
PLDaniels - Software - Xamime
Unix systems Internet Development A.B.N. 19 500 721 806
PGP Public Key at http://www.pldaniels.com/gpg-keys.pld
Nov 14 '05 #53
On Mon, 06 Dec 2004 21:51:03 +0000, CBFalconer wrote:
jacob navia wrote:
CBFalconer wrote:
jacob navia wrote:

The only thing that changes is

#define malloc(a) GC_malloc(a)
#define free(a)

There are no language modifications whatsoever.

Those redefinitions are specifically forbidden by the standard,
and for good reasons, which in turn should be obvious to any
implementor.


That is in the case you want to replace all your calls to
malloc with the gc (a sensible solution)

You are not forced to do that, since you can always explicitely
call GC_malloc. You should never call free however, because if
you call free with a GC_malloced object chaos will ensue.

Those errors can be very difficult to catch, hence the proposed
defines.


As I said, the proposed defines themselves are illegal.


Only as part of program code. Just arrange for this to be considered part
of the implementation and that problem goes away.

Lawrence

Nov 14 '05 #54
In <Pi************ *************** ********@unix40 .andrew.cmu.edu > "Arthur J. O'Dwyer" <aj*@nospam.and rew.cmu.edu> writes:

On Tue, 7 Dec 2004, Dan Pop wrote:

ro****@pc18.mat h.umbc.edu (Rouben Rostamian) writes:
#define foo(x) { bar(x); baz(x); }

#define foo(x) do { bar(x); baz(x); } while(0)


Why bother?

#define foo(x) (bar(x), baz(x))


What if 'bar' and 'baz' are statement-like macros in their own right?


Unless the description of the problem *explicitly* mentions this
possibility, there is no point in bothering to work around it.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #55
Lawrence Kirby wrote:
On Mon, 06 Dec 2004 21:51:03 +0000, CBFalconer wrote:
jacob navia wrote:
CBFalconer wrote:
jacob navia wrote:

> The only thing that changes is
>
> #define malloc(a) GC_malloc(a)
> #define free(a)
>
> There are no language modifications whatsoever.

Those redefinitions are specifically forbidden by the standard,
and for good reasons, which in turn should be obvious to any
implementor.

That is in the case you want to replace all your calls to
malloc with the gc (a sensible solution)

You are not forced to do that, since you can always explicitely
call GC_malloc. You should never call free however, because if
you call free with a GC_malloced object chaos will ensue.

Those errors can be very difficult to catch, hence the proposed
defines.


As I said, the proposed defines themselves are illegal.


Only as part of program code. Just arrange for this to be
considered part of the implementation and that problem goes away.


True, but so does standards conformance. See the "obfuscated
pointer" problem mentioned elsethread. The result is just another
bastard language.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #56
On Wed, 08 Dec 2004 15:26:36 +0000, CBFalconer wrote:

....
Only as part of program code. Just arrange for this to be
considered part of the implementation and that problem goes away.


True, but so does standards conformance. See the "obfuscated
pointer" problem mentioned elsethread. The result is just another
bastard language.


Indeed, which is why I stressed it with "that" problem. Clearly there are
other problems which do not go away.

Lawrence

Nov 14 '05 #57

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

Similar topics

9
2430
by: anonymous | last post by:
Hi CLCers, I want to know your opinion about the book: Expert C programming-Deep C secrets by Peter Van Der Linden. Thanks in advance. Sha
12
2125
by: Guido Mureddu | last post by:
Hello, I'm a student in electronic engineering. I do know you've seen and answered this sort of topic/request countless times, but I haven't found past threads as helpful as I had hoped, and even though I have read them all and many reviews, I prefer to ask directly to people who know the subject better than anyone else. First of all, I'm not new to programming, and I have already had an introductory course on C. I have an...
16
8514
by: Robert Zurer | last post by:
Can anyone suggest the best book or part of a book on this subject. I'm looking for an in-depth treatment with examples in C# TIA Robert Zurer robert@zurer.com
8
2199
by: Dgates | last post by:
Has anyone typed up an index for the O'Reilly book "C# and VB.NET Conversion?" I'm just learning C#, and often using this little book to see which VB.NET terms translate directly to some term in C#. However, it's a real hassle that the book has no index, just a table of contents. For example, as early as page 8, the book teaches that C#'s "using" statement is the equivalent of VB.NET's "imports" statement. However, that concept...
11
1934
by: www.douglassdavis.com | last post by:
I'm looking for advice here, and I would really appreciate it if you could help. Is there a VB 2005 book that you like and would recommend (and why)? Would you consider it good for beginners to programming, intermediate, or advanced level?
263
9415
by: Malcolm McLean | last post by:
The webpages for my new book are now up and running. The book, Basic Algorithms, describes many of the fundamental algorithms used in practical programming, with a bias towards graphics. It includes mathematical routines from the basics up, including floating point arithmetic, compression techniques, including the GIF and JPEG file formats, hashing, red black trees, 3D and 3D graphics, colour spaces, machine learning with neural...
6
1992
by: Hello | last post by:
Hello every body Please can any body tells me a good book that can teach me "visual basic 2005" (as beginner). Thank you all =========================================
1
3594
by: jrw133 | last post by:
i got this program the other day and ive just started it and i am getting some errors that i cant figure out. requirements: 1)create a clas called Book. a Book has three data members: m_title, m_id and m_flag to tell whether the book is in or checked out. 2)Write a constructor for Book. write a constructor that takes 3 parameters: Book(char * title, int id, bool flag) and initializes the 3 data members accordingly. in addition print out a...
76
4100
by: lorlarz | last post by:
Crockford's JavaScript, The Good Parts (a book review). This shall perhaps be the world's shortest book review (for one of the world's shortests books). I like Douglas Crockford (because I am a crabby old man too; plus he _is_ smart and good).. But, how can he write a book on the good parts of JavaScript and not mention functions that address CSS & DOM? Weird. It's like
0
9584
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
10583
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...
1
10323
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,...
0
10082
tracyyun
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...
0
6854
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
5525
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...
1
4301
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
2
3822
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2995
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.