473,836 Members | 1,487 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C99 Question

Hi,

Which section of C99 says that return value
of malloc(3) should not be casted?

Thanks.

--
Vijay Kumar R Zanvar
My Home Page - http://www.geocities.com/vijoeyz/
Nov 14 '05
110 4535
"P.J. Plauger" wrote:
"CBFalconer " <cb********@yah oo.com> wrote in message
> Mr Zanvar should note that ERT, better known in these parts as
> Trollsdale, is usually wrong and gives (as in this case) bad
> advice.

Whatever his record, I happen to agree with him on this matter.


However you have, I believe, a different motive. You want your
shrouded source to be compilable by unwashed idiots on any
compiler without complaint or handholding.


Believe what you want.


I shall :-) Prepare to be quoted by Trollsdale as authorative
confirmation for anything. He also revises quotes.

--
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 #51
"E. Robert Tisdale" <E.************ **@jpl.nasa.gov > writes:
Vijay Kumar R Zanvar wrote:
Which section of C99 says that
the return value of malloc should not be casted?
Good question! The ANSI/ISO C 99 standard does *not* say that
the return value of malloc should not be casted.

I *always* cast malloc's return value

char* p = (char*)malloc(n *sizeof(char));

so that my C++ compiler won't complain.


The best way to keep your C++ compiler from complaining is to avoid
feeding it C code.
There is *no* advantage to omitting the cast.
It is purely a style issue
but some C programmers have become fond of this style
and invented various *lame* arguments
to justify their personal aesthetics.


Nonsense, as usual.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
(Note new e-mail address)
Nov 14 '05 #52
"P.J. Plauger" <pj*@dinkumware .com> writes:
"Martin Dickopp" <ex************ ****@zero-based.org> wrote in message
news:bs******** *****@news.t-online.com...
I consider people who compile C code with a C++ compiler to be quite at
the newbie end of the spectrum.


You make me feel young again.


I think you've answered this before, but I don't remember the details,
I'm too lazy to do a search, and it probably couldn't hurt to repeat
it here.

Why do you want to compile C code with a C++ compiler?

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
(Note new e-mail address)
Nov 14 '05 #53
"Keith Thompson" <ks***@mib.or g> wrote in message
news:ln******** ****@nuthaus.mi b.org...
"P.J. Plauger" <pj*@dinkumware .com> writes:
"Martin Dickopp" <ex************ ****@zero-based.org> wrote in message
news:bs******** *****@news.t-online.com...
I consider people who compile C code with a C++ compiler to be quite at the newbie end of the spectrum.
You make me feel young again.


I think you've answered this before, but I don't remember the details,
I'm too lazy to do a search, and it probably couldn't hurt to repeat
it here.


I'm almost too lazy to answer, but since it's New Year's Eve and
I'm having trouble starting another project...
Why do you want to compile C code with a C++ compiler?


Actually, I want to compile our C code with *all* significant C and C++
compilers.

For one thing, the C++ Standard is intentionally silent (thanks to me)
on whether the C library has "C" or "C++" linkage. We have some OEM
customers who want the C names mangled, to provide link-time argument
type checking, and we have many others who want C names to look like,
well, C names.

For another thing, C++ is indeed in some ways a "better C", as
Stroustrup has been asserting for the past decade and a half. We
find bugs in hoary C code quite often when compiling it as C. So
it's just part of our shop discipline to make sure all our C code
compiles as C++ too.

I agree that you have to write more casts in C++ than in C, and
I agree that casts can sometimes disguise other problems. But it's
been my experience over the past ten years that casts tend to hide
stupid bugs, which are relatively easy to find, while compiling as
C++ tends to find subtler bugs, which are not. The tradeoff has
been well worth it for us.

HTH,

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Nov 14 '05 #54
begin followup to Keith Thompson:
Why do you want to compile C code with a C++ compiler?


The obvious recursive answer: To catch the problem of missing

#include <stdlib.h>

even if the return value of every malloc is casted...

Seriously, the subset of C that also compiles as C++ is a nice,
simple language. A and C++ compiler will emit a clear error where
naked C will just warn about seemingly obscure stuff.

$ cat a.c
int main() {
int print_a_double( int);
return print_a_double( 1);
}

$ cat b.c
#include <stdio.h>
void print_a_double( double x) { printf("%g\n", x); }

$ gcc -Wall a.c b.c && ./a.out
5.04822

Oops. Evil. Incarnate.

$ g++ a.c b.c
/tmp/ccvpYiSi.o(.tex t+0x16): In function `main':
: undefined reference to `print_a_double (int)'
collect2: ld returned 1 exit status

So even on outright misleading prototype is caught by C++,
though only in the linking phase. The more realistic case of

extern print_a_double( );

instead of

int print_a_double( int);

won't even compile with C++.

--
Für Google, Tux und GPL!
Nov 14 '05 #55
Richard Heathfield wrote:
C and C++ are different languages.

In C++, you'd be better off doing this:

char *p = new char[n];

unless you wanted to resize the string at some point
in which case you'd be better off using a std::string. cat richard.c #include <stdio.h>

int richard(size_t n) {
char *p = new char[n];
return n;
}
gcc -Wall -std=c99 -pedantic -c richard.c

richard.c: In function `richard':
richard.c:4: `new' undeclared (first use in this function)
richard.c:4: (Each undeclared identifier is reported only once
richard.c:4: for each function it appears in.)
richard.c:4: parse error before "char"
richard.c:4: warning: unused variable `p'
The problem is that I can't get my C compiler to accept this.

Are you suggesting that
I should write and maintain two separate versions of function richard?
Just so that I can use one with C and the other with C++ programs?
Do you do that?
And, if so, does your employer/client know that you do that?

I think that most (at least my) employers/clients would prefer that
I would develop and maintain for a single version
which both C and C++ compilers could compile.

Nov 14 '05 #56
E. Robert Tisdale wrote:
Richard Heathfield wrote:
C and C++ are different languages.

In C++, you'd be better off doing this:

char *p = new char[n];

unless you wanted to resize the string at some point
in which case you'd be better off using a std::string.


> cat richard.c

#include <stdio.h>

int richard(size_t n) {
char *p = new char[n];
return n;
}
> gcc -Wall -std=c99 -pedantic -c richard.c

richard.c: In function `richard':
richard.c:4: `new' undeclared (first use in this function)
richard.c:4: (Each undeclared identifier is reported only once
richard.c:4: for each function it appears in.)
richard.c:4: parse error before "char"
richard.c:4: warning: unused variable `p'
The problem is that I can't get my C compiler to accept this.

Are you suggesting that
I should write and maintain two separate versions of function richard?
Just so that I can use one with C and the other with C++ programs?
Do you do that?
And, if so, does your employer/client know that you do that?

I think that most (at least my) employers/clients would prefer that
I would develop and maintain for a single version
which both C and C++ compilers could compile.


Why stop there? Why not make it valid Pascal, Perl, Java, Basic, and
Lisp also? Does your employer know that your code will have to be
completely rewritten when they decide they want to compile it as a Java
program?

Sometimes I write C and sometimes I write C++. I even write some
libraries that are usable in both. It's stupid, in my opinion to write
in the common subset of C and C++ without a very good reason. Plauger
seems to have a good reason to do so. Most people don't. Do you?

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Nov 14 '05 #57
Kevin Goodsell wrote:
Sometimes I write C and sometimes I write C++.
I even write some libraries that are usable in both.
Do *you* have good reasons for doing so?
It's stupid, in my opinion to write in the common subset of C and C++
without a very good reason.
Plauger seems to have a good reason to do so.
Most people don't.
How could you know that?
Do you?


My reasons are the same as Plauger's.
Nov 14 '05 #58
In article <3F************ **@jpl.nasa.gov >, E.************* *@jpl.nasa.gov
says...
My reasons are the same as Plauger's.


Gee, that was predictable.

--
Randy Howard
2reply remove FOOBAR

Nov 14 '05 #59
E. Robert Tisdale wrote:
Richard Heathfield wrote:
C and C++ are different languages.

In C++, you'd be better off doing this:

char *p = new char[n];
<snip>
The problem is that I can't get my C compiler to accept this.
I imagine your Pascal compiler has trouble with the malloc call you showed
us, too. So what?
Are you suggesting that
I should write and maintain two separate versions of function richard?
Not at all.
Just so that I can use one with C and the other with C++ programs?
Do you do that?
No, I'm suggesting that, if you wish to use C code in a C++ program, you can
generally do it without recompiling the C code.
And, if so, does your employer/client know that you do that?

I think that most (at least my) employers/clients would prefer that
I would develop and maintain for a single version
which both C and C++ compilers could compile.


Presumably they would also prefer for your single version to compile under
Pascal, Fortran, and COBOL too. Does it?

--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #60

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

Similar topics

1
3104
by: Mohammed Mazid | last post by:
Can anyone please help me on how to move to the next and previous question? Here is a snippet of my code: Private Sub cmdNext_Click() End Sub Private Sub cmdPrevious_Click() showrecord
3
5045
by: Stevey | last post by:
I have the following XML file... <?xml version="1.0"?> <animals> <animal> <name>Tiger</name> <questions> <question index="0">true</question> <question index="1">true</question> </questions>
7
2668
by: nospam | last post by:
Ok, 3rd or is it the 4th time I have asked this question on Partial Types, so, since it seems to me that Partial Types is still in the design or development stages at Microsoft, I am going to ask it differently. FOUR QUESTIONS: The background: I got three (3) files
3
3098
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table before rowID answID qryrow questionID datafield 1591 12 06e 06e 06e question 1593 12 06f 06f 06f question 1594 12 answer to the question 06f
10
3445
by: glenn | last post by:
I am use to programming in php and the way session and post vars are past from fields on one page through to the post page automatically where I can get to their values easily to write to a database or continue to process on to the next page. I am now trying to learn ASP to see if we can replace some of our applications that were written in php with an ASP alternative. However, after doing many searches on google and reading a couple...
10
3749
by: Rider | last post by:
Hi, simple(?) question about asp.net configuration.. I've installed ASP.NET 2.0 QuickStart Sample successfully. But, When I'm first start application the follow message shown. ========= Server Error in '/QuickStartv20' Application. -------------------------------------------------------------------------------- Configuration Error Description: An error occurred during the processing of a configuration file
53
4106
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
56
4829
by: spibou | last post by:
In the statement "a *= expression" is expression assumed to be parenthesized ? For example if I write "a *= b+c" is this the same as "a = a * (b+c)" or "a = a * b+c" ?
2
4291
by: Allan Ebdrup | last post by:
Hi, I'm trying to render a Matrix question in my ASP.Net 2.0 page, A matrix question is a question where you have several options that can all be rated according to several possible ratings (from less to more for example). I have a question object that has two properties that contain the collections Options and Ratings. now I want this kind of layout: --- Rating1 Rating2 Rating3 Option 1 () () ...
3
2562
by: Zhang Weiwu | last post by:
Hello! I wrote this: ..required-question p:after { content: "*"; } Corresponding HTML: <div class="required-question"><p>Question Text</p><input /></div> <div class="not-required-question"><p>Question Text</p><input /></div>
0
9814
marktang
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...
0
10250
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
9369
agi2029
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...
1
7788
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
5645
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...
0
5821
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4447
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
4010
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3111
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.