473,770 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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*)mallo c(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 7868
In <cn**********@r eader1.imaginet .fr> "Charlie Gordon" <ne**@chqrlie.o rg> 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.c h> wrote in message
news:cn******** **@sunnews.cern .ch...
In <cn**********@r eader1.imaginet .fr> "Charlie Gordon" <ne**@chqrlie.o rg> 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.o rg> writes:
"Dan Pop" <Da*****@cern.c h> wrote in message
news:cn******** **@sunnews.cern .ch...
In <cn**********@r eader1.imaginet .fr> "Charlie Gordon" <ne**@chqrlie.o rg>

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_Keit h) 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.or g> wrote in message
news:ln******** ****@nuthaus.mi b.org...
"Charlie Gordon" <ne**@chqrlie.o rg> writes:
"Dan Pop" <Da*****@cern.c h> wrote in message
news:cn******** **@sunnews.cern .ch...
In <cn**********@r eader1.imaginet .fr> "Charlie Gordon" <ne**@chqrlie.o rg> 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.o rg> writes:
"Keith Thompson" <ks***@mib.or g> wrote in message
news:ln******** ****@nuthaus.mi b.org...
"Charlie Gordon" <ne**@chqrlie.o rg> writes:
> "Dan Pop" <Da*****@cern.c h> wrote in message
> news:cn******** **@sunnews.cern .ch...
>> In <cn**********@r eader1.imaginet .fr> "Charlie Gordon" <ne**@chqrlie.o rg>
> 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_Keit h) 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
83901
by: mike79 | last post by:
hi all.. if I wanted to malloc a struct, say the following: struct myStruct1 { int number; char *string; }
10
2318
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
2216
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 http://www.eskimo.com/~scs/C-faq/faq.html but it doesn't seem to answer my questions... So, I've made an example behind, with some included questions...
42
2180
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 understand from other postings that casting a result from malloc isn't good. In the past I have always been casting the malloc. I think it was even necessary. (But that's a long time ago, I hadn't heard of a standard for C these days). Was it...
14
3297
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) { exit(EXIT_FAILURE); fprintf(stderr, "malloc failed"); } for each individual call if there is a stylistically better way. How
3
11445
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 () from /lib/tls/libc.so.6 It's really strange; I just call malloc() like "tmp=malloc(size);" the system gives me Segmentation fault I want to write a code to do like a dynamic array, and the code is as
18
3318
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 ******************************************** /* mallocm.c */ /* funzione di libreria per trattamento della memoria e rilevamento errori */ /* uso: c_compiler malloc.c e usare malloc .obj */
3
1437
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 this snapshot (an outline of a picturebox or textbox). Is there anything I can do about this? Thanks!!!! Mel
71
19131
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 problem? I cannot see why using malloc instead of new does not give the same result.
0
9617
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
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9904
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
8931
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
7456
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
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
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
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
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

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.