----- Original Message -----
From: "b_bharat_shetty" <b_bharat_shetty@yahoo.co.in>
To: <C-Guru@yahoogroups.com>
Sent: Monday, April 05, 2004 9:58 AM
Subject: [C-Guru] C !
> Hi friends,
> I came across these questions and seem baffled by these
> Can anyone tell me answers to this?
>
> This code may have bugs!. If any suggest remedy
> to this else give output!
>
> #1.c
> #include<stdio.h>
You should include <stdlib.h> for malloc.
> main()
> {
> void *pointer;
> void *vector;
> void *address;
> void *location;
>
The prototype of malloc is:
void *malloc ( size_t );
where,
size_t is an unsigned integer typedefe in <stdlib.h>.
> pointer=malloc(-1);
Read: http://www.oocities.org/vijoeyz/faq/c/unsigned2.txt
to know about how a negative integer is assigned to an unsigned
int.
> vector=malloc(0);
Each compiler is free to define the behaviour of malloc () when
the size is 0. Usually, the compiler documents how it does.
Two possibilies are:
When the requested size is zero
* return a NULL pointer.
* Assume some non-zero size
In either case, using the return value malloc() can cause undefined
behaviour.
> address=malloc(1);
This is OK.
> location=NULL;
>
> free(pointer);
> free(vector);
> free(address);
If the corresponding mallocs were successful, then this is OK.
> free(location);
This is also legal, but no action occurs for free ( NULL ).
> }
> -----------------------------
> #2.c
> #include<stdio.h>
> main(int argc)
> {
There are two valid definitions of main (). In either case,
it returns an int.
int main ( void ) { /* .. */ }
or
int main ( int argc, char *argv[] ) { /* .. */ }
> staticint _main=10;
At least, one white character must separate keywords.
> _main--;
> if(main)
> main(10,NULL,NULL);
What are you trying to do here? Mismatch between definition
and calling of main(). Why do you decrement _main when you
are not using it to control the depth of recursion?
More ever, using the third argument for main is a compiler
extension, and is unportable.
--
Vijay Kumar R Zanvar
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"Vijay Kumar R Zanvar" <vijaykumar.rz@globaledgesoft.com> wrote:
>
> > vector=malloc(0);
>
> Each compiler is free to define the behaviour of malloc () when
> the size is 0. Usually, the compiler documents how it does.
> Two possibilies are:
>
> When the requested size is zero
> * return a NULL pointer.
This is correct.
> * Assume some non-zero size
This is not; or rather, it is misleading. malloc(0) is allowed to return
(in C89) just any unique pointer; in C99, it is allowed to behave as if
it were some non-zero size _except that the pointer must not be
dereferenced_. That restriction is not present when you _just_ assume
some non-zero size.
> In either case, using the return value malloc() can cause undefined
> behaviour.
Well, no. Not quite. Using it _as if it pointed to some usable object_
causes undefined behaviour.
In C89, if you get a non-null result, I'm not sure whether, e.g.,
comparing it for equality with any other same-type or null pointer
invokes UB; I can't determine whether it is supposed to be a _valid_
unique pointer, and if it isn't, even the comparison is UB. If it is,
the comparison must work.
In C99, if you get a non-null result, that result must be a valid
pointer, and can be treated as any pointer _except_ that it cannot be
dereferenced. For example, you can compare it for equality, you can pass
it to a function (which may then not deref it, but...), you can assign
it to a pointer variable. This is also true if you get a null pointer,
under either Standard.
> > address=malloc(1);
>
> This is OK.
Albeit useless, of course, but I presume that was not the point of the
code.
> > location=NULL;
> >
> > free(pointer);
> > free(vector);
> > free(address);
>
> If the corresponding mallocs were successful, then this is OK.
Even if not, this is OK. An unsuccessful malloc() returns a null
pointer, and free(0) is legal (and a no-op) under both Standards.
Richard
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
On Mon, 5 Apr 2004 17:48:57 +0530, "Vijay Kumar R Zanvar"
<vijaykumar.rz@globaledgesoft.com> wrote in comp.lang.c:
One more point I have not seen:
>
> [BEGINS]
>
> > Hi friends,
> > I cam across these questions and seem baffled by these
> > Can anyone tell me answers to this?
> >
> > This code may have bugs!. If any suggest remedy
> > to this else give output!
> >
> > #1.c
> > #include<stdio.h>
>
> You should include <stdlib.h> for malloc.
>
> > main()
> > {
> > void *pointer;
> > void *vector;
> > void *address;
> > void *location;
> >
>
> The prototype of malloc is:
>
> void *malloc ( size_t );
>
> where,
> size_t is an unsigned integer typedefe in <stdlib.h>.
>
> > pointer=malloc(-1);
>
> Read: http://www.oocities.org/vijoeyz/faq/c/unsigned2.txt
> to know about how a negative integer is assigned to an unsigned
> int.
>
> > vector=malloc(0);
>
> Each compiler is free to define the behaviour of malloc () when
> the size is 0. Usually, the compiler documents how it does.
Both versions of the C standard state that the result of calling
malloc() with a parameter of 0 is implementation-defined, which is a
term with a very specific definition in the standard.
A compiler is "free" to select among the behaviors specifically
allowed by the standard, not any behavior. And it must document its
choice.
> Two possibilies are:
In fact, the only two possibilities...
> When the requested size is zero
> * return a NULL pointer.
> * Assume some non-zero size
Richard seems to think there is something vague about the "unique
pointer" phrase used by c89/90. I think the meaning is rather simple.
It merely means that a non-NULL pointer returned by malloc(0) must be
different every time you make that call in a program. It must also be
different from every pointer returned by a successful non-zero
allocation call, and the address of every non-dynamic object existing
in a program.
In other words, it must not compare equal to any other valid pointer
value in a program.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html