473,699 Members | 2,417 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Where does fatal() come from?

Given the following snippet of code:
(taken from
http://www.gnu.org/software/libc/man...-Examples.html)

void *
xmalloc (size_t size)
{
register void *value = malloc (size);
if (value == 0)
fatal ("virtual memory exhausted");
return value;
}
What header file is fatal() located in? Also, why not just use
something like printf() or perror()?

Nov 14 '05 #1
7 1462
"grocery_stocke r" <cd*****@gmail. com> writes:
Given the following snippet of code:
(taken from
http://www.gnu.org/software/libc/man...-Examples.html)

void *
xmalloc (size_t size)
{
register void *value = malloc (size);
if (value == 0)
fatal ("virtual memory exhausted");
return value;
}
What header file is fatal() located in? Also, why not just use
something like printf() or perror()?


There is nothing called fatal() in any of the C standard headers. It
may be part of libc. Presumably it does something more than printing
an error message to stdout or stderr (my guess is that it then aborts
the program).

--
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 #2

"grocery_stocke r" <cd*****@gmail. com> wrote in message news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
Given the following snippet of code:
(taken from
http://www.gnu.org/software/libc/man...-Examples.html)

void *
xmalloc (size_t size)
{
register void *value = malloc (size);
if (value == 0)
fatal ("virtual memory exhausted");
return value;
}
What header file is fatal() located in? Also, why not just use
something like printf() or perror()?


Example
int *a = malloc(size)
if(a == NULL)
error
is same as the above xmalloc.The above xmalloc normally called as a malloc wrapper function.

Xmalloc some time is used to contain the malloc argument is zero conditions check. If we pass zero to a malloc
argument it's return value is null.(I am correct).But the following code works as expected:

#include<stdio. h>
#include<stdlib .h>

int main(void)
{
char *a;
a = malloc(0);
if(a==NULL)
printf("malloc is NULL\n");
else
{
a = "comp.lang. c";
printf("%s",a);
}
return 0;
}

Is the above UB?

--
"combinatio n is the heart of chess"

A.Alekhine

Mail to:
sathyashrayan AT gmail DOT com

Nov 14 '05 #3
sathyashrayan wrote:
.... snip ...
#include<stdio. h>
#include<stdlib .h>

int main(void)
{
char *a;
a = malloc(0);
if(a==NULL)
printf("malloc is NULL\n");
else
{
a = "comp.lang. c";
printf("%s",a);
}
return 0;
}

Is the above UB?


Yes, assuming you meant to write *a = ".." and mistyped a. Even if
malloc(0) returns non-NULL, the space allocated has no room, so you
can't store anything there. If you didn't mean *a you have just
discarded a malloced pointer and had a memory leak.

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #4
CBFalconer wrote:
sathyashrayan:

#include<stdio. h>
#include<stdlib .h>

int main(void)
{
char *a;
a = malloc(0);
if(a==NULL)
printf("malloc is NULL\n");
else
{
a = "comp.lang. c";
printf("%s",a);
}
return 0;
}

Is the above UB?

No, but the output depends on implementation-defined behaviour.
And your second printf needs a '\n'.
Yes, assuming you meant to write *a = ".." and mistyped a. Even if
malloc(0) returns non-NULL, the space allocated has no room, so you
can't store anything there.
Not even if strcpy would be used. ;-)
If you didn't mean *a you have just
discarded a malloced pointer and had a memory leak.


Are you sure that failing to free a malloc(0) call leads to a memory leak?

Jirka
Nov 14 '05 #5
Jirka Klaue wrote:
CBFalconer wrote:
.... snip ...
If you didn't mean *a you have just
discarded a malloced pointer and had a memory leak.


Are you sure that failing to free a malloc(0) call leads to a
memory leak?


Yes, I know of such systems. There is always a chance it may not,
on a particular system. From N869:

7.20.3 Memory management functions

[#1] The order and contiguity of storage allocated by
successive calls to the calloc, malloc, and realloc
functions is unspecified. The pointer returned if the
allocation succeeds is suitably aligned so that it may be
assigned to a pointer to any type of object and then used to
access such an object or an array of such objects in the
space allocated (until the space is explicitly freed or
reallocated). Each such allocation shall yield a pointer to
an object disjoint from any other object. The pointer
returned points to the start (lowest byte address) of the
allocated space. If the space cannot be allocated, a null
pointer is returned. If the size of the space requested is
zero, the behavior is implementation-defined: either a null
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^
pointer is returned, or the behavior is as if the size were
some nonzero value, except that the returned pointer shall
not be used to access an object. The value of a pointer
that refers to freed space is indeterminate.

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #6
On Tue, 14 Jun 2005 16:01:00 +0200, Jirka Klaue wrote:

....
If you didn't mean *a you have just
discarded a malloced pointer and had a memory leak.


Are you sure that failing to free a malloc(0) call leads to a memory leak?


If malloc() returned non-null is very likely will. In addition to the
memory available for the program to use, of which there may be none, a
successful malloc() typically creates internal housekeeping information
which takes up space. Not freeing this up appropriately constitutes a
memory leak.

Lawrence
Nov 14 '05 #7
CBFalconer <cb********@yah oo.com> writes:
sathyashrayan wrote:

... snip ...

#include<stdio. h>
#include<stdlib .h>

int main(void)
{
char *a;
a = malloc(0);
if(a==NULL)
printf("malloc is NULL\n");
else
{
a = "comp.lang. c";
printf("%s",a);
}
return 0;
}

Is the above UB?


Yes, assuming you meant to write *a = ".." and mistyped a. Even if
malloc(0) returns non-NULL, the space allocated has no room, so you
can't store anything there. If you didn't mean *a you have just
discarded a malloced pointer and had a memory leak.


I'm not sure why you're assuming he meant *a; the suggested *a = ".."
is illegal, but the program as written is ok.

If the code above were executed somewhere other than just before
leaving main(), it would be a (small) memory leak. But once the
program terminates (and returns a status to the environment), the
standard becomes silent. It's probably good style to release all
allocated memory, even if the program is about to terminate (after all
the code could later be moved into a function that might be called
repeatedly). But on any sensible system, allocated memory should be
returned to the system when the program terminates, whether it's been
free()d or not.

To put it another way, the standard doesn't guarantee that malloc()ed
memory that hasn't been free()d is returned to the operating system --
but neither does it guarantee that malloc()ed memory that *has* been
free()d is returned to the operating system. Failing to reclaim
un-free()d memory and failing to reclaim free()d memory are both bugs
(but not violations of the C standard); neither bug, IMHO, is much
worse than the other.

(Possibly the tradeoffs are different in embedded systems.)

--
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 #8

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

Similar topics

3
2445
by: PeterF | last post by:
Hello, what is wrong here? the purpose is to create an array of objects and then interate over it, calling some method from all of them. I get just the following at the marked line ("// FATAL ERROR"): Fatal error: Call to a member function on a non-object See the code:
8
9992
by: Tim Tyler | last post by:
I'm getting fatal errors when executing code - and my error handler is failing to trap them - so I get no stack backtrace :-( The error I am getting is: "Fatal error: Call to a member function fetchRow() on a non-object." What are the available options here? Am I supposed to check I have a real object whenever I perform
1
13664
by: Mike | last post by:
Last weekend I decided to install Apache 2.0.53-win32-x86-no_ssl PHP 5.0.3 Smarty 2.6.7 MySQL essential-4.1.10-win32 I have Apache up (Port 80 blocked at the router and firewall!) and I have got Smarty working. I haven't got around to installing MySQL. I am now in chapter 8 'Error and Exception Handling' of
2
2951
by: Itjalve | last post by:
This gives me a fatal error. I'm using .NET VC7.1 and made a win32 consol app, I have no problems with VC6. Debug build. I have removed nearly all my code this is whats left. From the beginning the template was defined in a .h file, but that has no effect, same error. I have seen when searching for fatal error that there are some problems with templates and optimization, but nothing as simple like this. I'm i doing something wrong?
6
5201
by: Steve Crawford | last post by:
I've started seeing the following in my logs: FATAL: invalid frontend message type 8 I searched back over a month and there are 5 instances of this error of which 4 are in the last 24 hours. I could not find this error defined. Any ideas of what it means, it's severity, how to track the cause and cure? Cheers,
4
5647
by: ARF | last post by:
I'm testing AutoCAD 2005 automation via VS2005 Pro C++/CLR and I'm getting fatal compiler errors. I start with a default C++/CLR class library project and modify it by adding the following references: acdbmgd.dll acmgd.dll the entire source for the default library header file is:
25
2591
by: Gordon Cowie | last post by:
Wouldn't it make sense if IndexOf just threw a character not found exception instead of -1? If -1 is what you were expecting there should be a function ContainsChar that returns bool. Thoughts?
1
3768
by: kvarada | last post by:
Hello Experts, I am building my application on WinNT.4.0_i386_MSVC.7.1 platform. When I build the application on a stand alone machine, it builds fine. But when I build the same application from a linux box using rsh it gives me the errors below Microsoft (R) Development Environment Version 7.10.3077. Copyright (C) Microsoft Corp 1984-2001. All rights reserved. ------ Build started: Project: SigComp, Configuration: Release Win32 ------...
1
22835
by: edwige | last post by:
I just installed PostgreSQL on ubuntu linux. the installer does everything for, but I try to use "createuser" I get this message: { FATAL: role "root" does not exist } I have tried different suggestions from the Net, nothing seems to work. How do I correct the problem?
0
8685
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
8613
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,...
1
8908
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7745
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
6532
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
4374
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
4626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3054
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
3
2008
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.