Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > C/C++ guidelines

Reply
Thread Tools

C/C++ guidelines

 
 
Joachim Schmitz
Guest
Posts: n/a
 
      09-16-2007
"Joachim Schmitz" <(E-Mail Removed)> schrieb im Newsbeitrag
news:fcitgn$h1f$(E-Mail Removed)...
> "Peter J. Holzer" <(E-Mail Removed)> schrieb im Newsbeitrag
> news:(E-Mail Removed)...
>> On 2007-09-16 01:36, Karl Heinze <nomail@invalid> wrote:
>>> On Sat, 15 Sep 2007 18:14:30 -0700, "Chris Thomasson"
>>><(E-Mail Removed)> wrote:
>>>> http://c-faq.com/malloc/mallocnocast.html
>>>>
>>> Thanx. Though I don't buy in that "argument".
>>>
>>> Consider:
>>>
>>> #include <stdio.h>
>>>
>>> int main(void)
>>> {
>>> char *p = (char *) malloc(10);
>>>
>>> return 0;
>>> }
>>>
>>> At least lcc-win32 DOES warn you: "Missing prototype for 'malloc'.
>>>
>>> Same with Pelles C: "Missing prototype for 'malloc'".
>>>
>>> Same with gcc: "implicit declaration of function 'malloc'".

>>
>> Once upon a time most compilers didn't warn about missing prototypes but
>> they did warn about conversions from int to a pointer. In these days not
>> casting malloc was the most reliable way to catch a missing declaration
>> of malloc.

> Because they are required to do diagnose the conversionm and free to do do
> the same with the missing prototype

oops, spelling/typing problems, sorry...
Because they are required to diagnose the conversion and free to diagnose
the missing prototype


 
Reply With Quote
 
 
 
 
Joachim Schmitz
Guest
Posts: n/a
 
      09-16-2007
"Harald van D?k" <(E-Mail Removed)> schrieb im Newsbeitrag
news:fcj2k0$830$(E-Mail Removed)1.ov.home.nl...
> On Sun, 16 Sep 2007 11:34:38 +0200, Joachim Schmitz wrote:
>> a diagnostic is not required by the standard for an implicit declared
>> function

>
> It is not in C90, but it is in C99.

Chapter and Verse?

However, not using the case it still the safe bet, given the not so wide
choice of C99 compilers


 
Reply With Quote
 
 
 
 
Karl Heinze
Guest
Posts: n/a
 
      09-16-2007
On Sat, 15 Sep 2007 22:45:00 -0400, CBFalconer <(E-Mail Removed)>
wrote:

>>>
>>> [...] in C you should not cast the result of malloc, because
>>> doing that might introduce subtle errors [...]
>>>

>> For example?
>>

> Failure to #include <stdlib.h> going undetected.
>

Huh?

So it's _not_ the usage of the cast (as such) which "might introduce
subtle errors" but forgetting to include <stdlib.h>.

On the other hand most modern C compilers (as it seems) would catch
that lapse (and produce a warning). (At least this is true for the C
compilers _I_ use: lcc-win32, Pelles C, gcc.)


K. H.

--

E-mail: info<at>simple-line<Punkt>de
 
Reply With Quote
 
Wade Ward
Guest
Posts: n/a
 
      09-16-2007


"Keith Thompson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Robbie Marshall <(E-Mail Removed)> writes:
>> I'm about to embark on a new project and for maximum portability we've
>> been asked to code it in the common subset of C and C++.
>>
>> Can anyone suggest any good documents that might help us? Lists of
>> gotchas to avoid, that sort of thing?

>
> Consider programming in C90 and making sure not to accidentally use a
> C++ compiler. You can enforce this with:
>
> #ifdef __cplusplus
> #error "Use a C compiler, not a C++ compiler"
> #endif

Keith's right here.

Mr. Thompson has called me a troll, a curious appellation for a large,
jellyfilled donut whose first german language book was Gödel, Escher, Bach.
Was ist die Abbildung an Bloop, Floop, und Gloop?
--
Wade Ward
http://www.velocityreviews.com/forums/(E-Mail Removed)
"A sequence of zeroes and ones;" Indiana


 
Reply With Quote
 
=?iso-2022-kr?q?=1B=24=29CHarald_van_D=0E=29=26=0Fk?=
Guest
Posts: n/a
 
      09-16-2007
On Sun, 16 Sep 2007 11:48:29 +0200, Joachim Schmitz wrote:
> "Harald van D?k" <(E-Mail Removed)> schrieb im Newsbeitrag
> news:fcj2k0$830$(E-Mail Removed)1.ov.home.nl...
>> On Sun, 16 Sep 2007 11:34:38 +0200, Joachim Schmitz wrote:
>>> a diagnostic is not required by the standard for an implicit declared
>>> function

>>
>> It is not in C90, but it is in C99.

> Chapter and Verse?


C99 has no implicit function declarations.

6.5.1p2:
"An identifier is a primary expression, provided it has been declared as
designating an object (in which case it is an lvalue) or a function (in
which case it is a function designator). 76)"

Footnote 76:
"Thus, an undeclared identifier is a violation of the syntax."

A diagnostic is required (in both C90 and C99) for all syntax errors.

> However, not using the case it still the safe bet, given the not so wide
> choice of C99 compilers


Using malloc without a proper declaration would result in a warning
message from my compiler even in C90 mode. If yours doesn't at least have
an option to warn about implicit function declarations, though, then yes,
it would be better for you not to cast malloc's result. For me, it
wouldn't matter.
 
Reply With Quote
 
Karl Heinze
Guest
Posts: n/a
 
      09-16-2007
On Sun, 16 Sep 2007 11:23:37 +0200, "Peter J. Holzer"
<(E-Mail Removed)> wrote:

>
> There is STILL something wrong with casting malloc's return value:
> It is unnecessary and clutters up the code.
>

Actually, I already found that habit (casting malloc's return value)
described as _good coding practice_.


K. H.

--

E-mail: info<at>simple-line<Punkt>de
 
Reply With Quote
 
Karl Heinze
Guest
Posts: n/a
 
      09-16-2007
On Sun, 16 Sep 2007 10:43:05 +0000 (UTC), $)CHarald van D)&k
<(E-Mail Removed)> wrote:

>>>
>>> But (char*) malloc(...) is well-formed C++
>>>

>> And it is execreble dangerous coding in C.
>>

> Nonsense. In the cases where the code is broken with the cast, it's also
> broken without the cast, and the argument about hiding a missing
> inclusion of <stdlib.h> only applies if the programmer uses a compiler
> that won't warn on implicit function declarations. Mine does, so how
> would it be dangerous for me to cast the result of malloc?
>

Completely agree with you. Moreover, if C99 is actually required to
produce a warning in case of an implicit declared function (as you
claimed) this silly argument (in the C-FAQ) is completely disproved.


K. H.

--

E-mail: info<at>simple-line<Punkt>de
 
Reply With Quote
 
Joachim Schmitz
Guest
Posts: n/a
 
      09-16-2007
"Harald van D?k" <(E-Mail Removed)> schrieb im Newsbeitrag
news:fciv02$qn5$(E-Mail Removed)1.ov.home.nl...
> On Sun, 16 Sep 2007 11:48:29 +0200, Joachim Schmitz wrote:
>> "Harald van D?k" <(E-Mail Removed)> schrieb im Newsbeitrag
>> news:fcj2k0$830$(E-Mail Removed)1.ov.home.nl...
>>> On Sun, 16 Sep 2007 11:34:38 +0200, Joachim Schmitz wrote:
>>>> a diagnostic is not required by the standard for an implicit declared
>>>> function
>>>
>>> It is not in C90, but it is in C99.

>> Chapter and Verse?

>
> C99 has no implicit function declarations.
>
> 6.5.1p2:
> "An identifier is a primary expression, provided it has been declared as
> designating an object (in which case it is an lvalue) or a function (in
> which case it is a function designator). 76)"



> Footnote 76:
> "Thus, an undeclared identifier is a violation of the syntax."

OK, thanks. Are footnotes normative?

> A diagnostic is required (in both C90 and C99) for all syntax errors.
>
>> However, not using the case it still the safe bet, given the not so wide
>> choice of C99 compilers

>
> Using malloc without a proper declaration would result in a warning
> message from my compiler even in C90 mode. If yours doesn't at least have
> an option to warn about implicit function declarations, though, then yes,
> it would be better for you not to cast malloc's result. For me, it
> wouldn't matter.

The compilers I use warn too (with appropriate warning level set), but they
are not required too and that's the whole point.


 
Reply With Quote
 
Karl Heinze
Guest
Posts: n/a
 
      09-16-2007
On Sun, 16 Sep 2007 12:10:30 +0200, "Joachim Schmitz"
<(E-Mail Removed)> wrote:

>
> The compilers I use warn too (with appropriate warning level set), but they
> are not required too and that's the whole point.
>

Clearly the "problem" is NOT the usage of a cast (which is required in
C++ !) but forgetting to include <stdlib.h>.


K. H.

--

E-mail: info<at>simple-line<Punkt>de
 
Reply With Quote
 
Chris Hills
Guest
Posts: n/a
 
      09-16-2007
In article <(E-Mail Removed)>, Robbie Marshall
<(E-Mail Removed)> writes
>Hi groups,
>
>I'm about to embark on a new project and for maximum portability we've
>been asked to code it in the common subset of C and C++.
>
>Can anyone suggest any good documents that might help us? Lists of
>gotchas to avoid, that sort of thing?


Firstly what are the compilers and targets?

Due to the lack of serious take up of the current C99 standard and many
compiler having compiler and target extensions (also domain extensions)
it is better to start with the tools than the standard.

You have asked a practical not theoretical question.


--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
/\/\/ (E-Mail Removed) www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/



 
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
Case Gallery rules and Guidelines Silverstrand Case Modding 0 06-20-2005 11:38 PM
Portable Coding Guidelines? Roger VHDL 0 12-17-2004 07:33 PM
Code Guidelines JIT ASP .Net 2 11-02-2004 06:57 AM
Is WPS compatible with Smart client as defined in WISPr guidelines nsmurthy Wireless Networking 0 08-13-2004 10:23 AM
VHDL Coding Guidelines Francisco Camarero VHDL 1 07-08-2003 08:17 PM



Advertisments