malloc problem

K

Keith Thompson

Indeed it is. However, I'm not talking about "a function definition";
I'm talking about the two forms for main specified by 5.1.2.2.1.

Which says:

The function called at program startup is named main. The
implementation declares no prototype for this function. It shall
be defined with a return type of int and with no parameters:

int main(void) { /* ... */ }

or with two parameters (referred to here as argc and argv, though
any names may be used, as they are local to the function in which
they are declared):

int main(int argc, char *argv[]) { /* ... */ }

or equivalent; or in some other implementation-defined manner.

I believe the "or equivalent" covers "int main() {/*...*/}" (though I
still prefer "int main(void) {/*...*/}").
 
P

pete

Keith said:
It appears that in the context of a function *definition*, int main()
is exactly equivalent to int main(void).

There's no void in K&R C.

int main() {return 0;}
is a valid C program in K&R C, C89, and C99.
 
K

Keith Thompson

pete said:
There's no void in K&R C.

int main() {return 0;}
is a valid C program in K&R C, C89, and C99.

Sure, but C89 (unlike C99) is sufficiently widely implemented that we
can assume at least C89 for purposes of this newsgroup. (I think the
latest versions of gcc can't even be bootstrapped with a pre-C89
compiler.)

Writing int main() rather than int main(void) to satisfy K&R C
compilers is, in my opinion, no longer worth the effort.
 
P

pete

Keith said:
pete said:
Keith said:
[...]
Old style function types don't include the parameter types.
int main() is old style.
int main(void) is more better.

It appears that in the context of a function *definition*, int main()
is exactly equivalent to int main(void).

There's no void in K&R C.

int main() {return 0;}
is a valid C program in K&R C, C89, and C99.

Sure, but C89 (unlike C99) is sufficiently widely implemented that we
can assume at least C89 for purposes of this newsgroup. (I think the
latest versions of gcc can't even be bootstrapped with a pre-C89
compiler.)

Writing int main() rather than int main(void) to satisfy K&R C
compilers is, in my opinion, no longer worth the effort.

That's what I think too.
 
P

Peter Nilsson

Keith said:
It appears that in the context of a function *definition*, int main()
is exactly equivalent to int main(void).

Nevertheless, I consider int main(void) to be better style. It's more
explicit, it avoid confusion with the use of int main() in function
*declarations* (which is different from int main(void)), and, as I
recently discovered, some compilers don't catch certain errors with ()
that they do catch with (void).

Personally, the sooner non-prototype function declarations are removed
from C, the better. I have no problem with () meaning _explicitly_ that
a function that takes no parameters. Certainly, C++ programmers don't.
 
J

Joe Estock

Peter said:
Personally, the sooner non-prototype function declarations are removed
from C, the better. I have no problem with () meaning _explicitly_ that
a function that takes no parameters. Certainly, C++ programmers don't.
Yes, but this is not c++, this is c.

Joe Estock
 
K

Keith Thompson

Peter Nilsson said:
Personally, the sooner non-prototype function declarations are removed
from C, the better. I have no problem with () meaning _explicitly_ that
a function that takes no parameters. Certainly, C++ programmers don't.

If the language were being designed from scratch today, certainly it
would make sense for () to mean that the function takes no parameters.
But it already has a well-defined (though perhaps obsolescent) meaning.

Removing non-prototype function declarations from the language would
break existing code. Even assuming that's tolerable, changing the
existing meaning of () could make it more difficult to generate good
diagnostics for old code.
 
P

pete

pete said:
Keith said:
pete said:
Keith Thompson wrote:

[...]
Old style function types don't include the parameter types.
int main() is old style.
int main(void) is more better.

It appears that in the context of a function *definition*,
int main()
is exactly equivalent to int main(void).

There's no void in K&R C.

int main() {return 0;}
is a valid C program in K&R C, C89, and C99.

Sure,
but C89 (unlike C99) is sufficiently widely implemented that we
can assume at least C89 for purposes of this newsgroup.
(I think the
latest versions of gcc can't even be bootstrapped with a pre-C89
compiler.)

Writing int main() rather than int main(void) to satisfy K&R C
compilers is, in my opinion, no longer worth the effort.

That's what I think too.

Here's what I was looking for:

N869
Introduction

[#2] Certain features are obsolescent, which means that they
may be considered for withdrawal in future revisions of this
International Standard. They are retained because of their
widespread use, but their use in new implementations (for
implementation features) or new programs (for language
[6.11] or library features [7.26]) is discouraged.

6.11 Future language directions

6.11.4 Function declarators
[#1] The use of function declarators with empty parentheses
(not prototype-format parameter type declarators) is an
obsolescent feature.

6.11.5 Function definitions
[#1] The use of function definitions with separate parameter
identifier and declaration lists (not prototype-format
parameter type and identifier declarators) is an obsolescent
feature.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,431
Latest member
ElyseG3173

Latest Threads

Top