473,397 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

call to malloc with size 0

Neo
Hi Folks,

I've a simple qestion related to dynamic memory allocation in C here is the
code:

#include <stdio.h>

int main()

{

char *p;

if( (p=(char*)malloc(0)) == NULL)

printf("NULL\n");

else

printf("Valid Pointer\n");

return 0;

}

Output : "Valid Pointer"

Why this code fragment returns a valid pointer to a memory block???

-Neo


Nov 14 '05
54 7770
In <cn**********@reader1.imaginet.fr> "Charlie Gordon" <ne**@chqrlie.org> writes:
Of course a compiler that does not complain about calling a function without
proper prototype is either broken or misconfigured. Fix that first.


A conforming C89 compiler need not be configurable to complain about that.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #51
"Dan Pop" <Da*****@cern.ch> wrote in message
news:cn**********@sunnews.cern.ch...
In <cn**********@reader1.imaginet.fr> "Charlie Gordon" <ne**@chqrlie.org> writes:
A conforming C89 compiler need not be configurable to complain about that.


Granted.

A compiler that does not complain about calling a function without
proper prototype is either outdated or misconfigured or outdated. Fix that
first.

Chqrlie.


Nov 14 '05 #52
"Charlie Gordon" <ne**@chqrlie.org> writes:
"Dan Pop" <Da*****@cern.ch> wrote in message
news:cn**********@sunnews.cern.ch...
In <cn**********@reader1.imaginet.fr> "Charlie Gordon" <ne**@chqrlie.org>

writes:
>> A conforming C89 compiler need not be configurable to complain
>> about that.


Granted.

A compiler that does not complain about calling a function without
proper prototype is either outdated or misconfigured or outdated. Fix that
first.


That's not always an option, and a good C programmer should be able to
work even with outdated tools. Your compiler doesn't require you to
include the right headers? Do it anyway.

Surprise your compiler. Write better code than it asks you to.

--
Keith Thompson (The_Other_Keith) 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 #53
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"Charlie Gordon" <ne**@chqrlie.org> writes:
"Dan Pop" <Da*****@cern.ch> wrote in message
news:cn**********@sunnews.cern.ch...
In <cn**********@reader1.imaginet.fr> "Charlie Gordon" <ne**@chqrlie.org> writes:
>> A conforming C89 compiler need not be configurable to complain
>> about that.


Granted.

A compiler that does not complain about calling a function without
proper prototype is either outdated or misconfigured or outdated. Fix that
first.


That's not always an option, and a good C programmer should be able to
work even with outdated tools. Your compiler doesn't require you to
include the right headers? Do it anyway.


The vast majority of posters with such problems (calling unprototyped functions)
use fine compilers with default options : none. It is a shame to give newbie C
programmers "full fledged" environments that do not enforce a minimum of
consistency checking, especially when all the needed support is readily
available from the compiler. Turn those warnings on.
Surprise your compiler. Write better code than it asks you to.


Yes, but make it ask you to if it can. Know your tools and take advandage of
them !

Chqrlie
Nov 14 '05 #54
"Charlie Gordon" <ne**@chqrlie.org> writes:
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"Charlie Gordon" <ne**@chqrlie.org> writes:
> "Dan Pop" <Da*****@cern.ch> wrote in message
> news:cn**********@sunnews.cern.ch...
>> In <cn**********@reader1.imaginet.fr> "Charlie Gordon" <ne**@chqrlie.org>
> writes:
>> >> A conforming C89 compiler need not be configurable to complain
>> >> about that.
>
> Granted.
>
> A compiler that does not complain about calling a function
> without proper prototype is either outdated or misconfigured or
> outdated. Fix that first.


That's not always an option, and a good C programmer should be able to
work even with outdated tools. Your compiler doesn't require you to
include the right headers? Do it anyway.


The vast majority of posters with such problems (calling
unprototyped functions) use fine compilers with default options :
none. It is a shame to give newbie C programmers "full fledged"
environments that do not enforce a minimum of consistency checking,
especially when all the needed support is readily available from the
compiler. Turn those warnings on.
Surprise your compiler. Write better code than it asks you to.


Yes, but make it ask you to if it can. Know your tools and take
advandage of them !


(Please keep your lines down to 72 columns or so. I've reformatted
some of the quoted text here.)

My followup implied more disagreement than I intended. Certainly you
should use good tools (including compilers that issue good warnings)
whenever possible. No matter how careful you are, you'll make
mistakes, and a good compiler may catch *some* of them for you.

You wrote "Fix that first", meaning to use a better compiler. I agree
with the "Fix that", but not necessarily with the "first". You need
to be careful to write good code in the first place, *and* you should
use the best compiler available with the best set of options. The
first is always possible with some effort and knowledge; the second is
usually possible, but not always.

And of course no compiler can catch all your errors. Experienced
programmers know this; newbies need to learn it.

--
Keith Thompson (The_Other_Keith) 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 #55

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

Similar topics

13
by: mike79 | last post by:
hi all.. if I wanted to malloc a struct, say the following: struct myStruct1 { int number; char *string; }
10
by: pembed2003 | last post by:
Hi, If I have the folllowing: char* p = malloc(5); memset(p,-1,5); *p = 0; printf("%d\n",strlen(p)); free(p); It will print 0. Is there a way to retrive the initial size of memory
7
by: Rano | last post by:
/* Hello, I've got some troubles with a stupid program... In fact, I just start with the C language and sometime I don't understand how I really have to use malloc. I've readden the FAQ...
42
by: Joris Adriaenssens | last post by:
This is my first posting, please excuse me if it is off-topic. I'm learning to program in C. It's been almost ten years I've been programming and a lot of things have changed apparently. I...
14
by: Marlene Stebbins | last post by:
At one point in my program I have about a dozen calls to malloc. I want to check for malloc failure, but I don't want to write: if((buffer_x = malloc(BUFSIZE * sizeof(*buffer_x))) == NULL) {...
3
by: Zheng Da | last post by:
Program received signal SIGSEGV, Segmentation fault. 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 (gdb) bt #0 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 #1 0x40094c54 in malloc...
18
by: cs | last post by:
This is the function malloc_m() that should be like malloc() but it should find memory leak, and over bound writing in arrays. How many errors do you see? Thank you...
3
by: melanieab | last post by:
Hi, I'm having trouble when I leave a tabpage to go on to the next. On Focus Leave, I take a snapshot of that tab. It usually turns out ok, but often, part of the next tabpage will appear in...
71
by: desktop | last post by:
I have read in Bjarne Stroustrup that using malloc and free should be avoided in C++ because they deal with uninitialized memory and one should instead use new and delete. But why is that a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...
0
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...
0
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...

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.