473,756 Members | 2,383 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

calloc()

Hi,

What would cause calloc() to return a NULL pointer?

Is the system simply out of memory?

Regards,

Michael

Aug 7 '06
21 1947
Richard said:

<snip>
Would Linux, in default, ever return NULL?
Yes. Try a GREAT BIG allocation.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Aug 7 '06 #11
Keith Thompson <ks***@mib.orgw rites:
Ian Collins <ia******@hotma il.comwrites:
[...]
>Assuming you implementation has errno, you should test its value after
the failing call to determine the cause of the failure.

His implementation certainly has error if it's a conforming hosted
implementation. But the standard doesn't say that malloc() sets errno
on a failure. And even if it does, it's not likely to distinguish
between different forms of "not enough memory".
That was a particularly misleading typo; I meant that his
implementation certainly has *errno* if it's a conforming hosted
implementation.

--
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.
Aug 7 '06 #12
On Tue, 08 Aug 2006 00:37:17 +0200, Richard <rg****@gmail.c omwrote:
>
Q: does the swapfile come on "seamlessly " on most platforms here?
In HP-UX, yes.
In
which case would we ever see malloc failing assuming enough swap?
Yes, if your program is ambitious enough.
>How
does that work (yes I know OT)?
There is a per-process limit. For the (rather old, HP-UX 11.0) system
I'm logged into now, it's 67 MB data segment, 83 MB stack, and 67 MB
text (program). That's configurable, and the defaults are larger on
newer systems.
>Even if the swap is fragmented is the
memory block contiguous?
As far as a C program is concerned, yes.

--
Al Balmer
Sun City, AZ
Aug 7 '06 #13
On Mon, 07 Aug 2006 23:21:11 +0100, Flash Gordon
<sp**@flash-gordon.me.ukwro te:
>Ian Collins wrote:
>Michael McGarry wrote:
>>Default User wrote:

Michael McGarry wrote:
Hi,
>
What would cause calloc() to return a NULL pointer?
>
Is the system simply out of memory?
It's unlikely to be out. However, it may not have a piece available to
your program that is large enough to fill the request.

Is there anything I can do to fix that?

I guess if there is not contiguous memory of the size I need I am out
of luck?

Thanks for any help,
Please don't top post.

Assuming you implementation has errno,

If it is a hosted C implementation then it has errno.
you should test its value after
the failing call to determine the cause of the failure.

The standard does not require that errno be set. The Unix98 standard
requires that it be set to ENOMEM on failure if the linux documentation
is accurate, but if it is always set to ENOMEM on failure that gives you
no additional information.
On HP-UX, errno is set to EINVAL if memory has been detectably
corrupted. If there's insufficient memory, there's no clue as to the
reason. Of course, there's nothing to say that logically contiguous
memory must be physically contiguous.
>
In other words, checking errno in this instance is pointless on at least
one implementation and quite likely pointless on a number of others.
>If there isn't contiguous memory of the size you need, you are out of
luck and should consider a different design. As an alternative, try
allocating the bigger memory blocks earlier.

This is better advice. I would also suggest checking for memory leaks
and making sure you are passing sensible numbers to calloc.
--
Al Balmer
Sun City, AZ
Aug 7 '06 #14
It turns out I was just eating up way too much memory.

I am trying to figure out how that happened.

Thanks for all the advice,

Michael

Al Balmer wrote:
On Mon, 07 Aug 2006 23:21:11 +0100, Flash Gordon
<sp**@flash-gordon.me.ukwro te:
Ian Collins wrote:
Michael McGarry wrote:
Default User wrote:

Michael McGarry wrote:
Hi,

What would cause calloc() to return a NULL pointer?

Is the system simply out of memory?
It's unlikely to be out. However, it may not have a piece available to
your program that is large enough to fill the request.

Is there anything I can do to fix that?

I guess if there is not contiguous memory of the size I need I am out
of luck?

Thanks for any help,

Please don't top post.

Assuming you implementation has errno,
If it is a hosted C implementation then it has errno.
you should test its value after
the failing call to determine the cause of the failure.
The standard does not require that errno be set. The Unix98 standard
requires that it be set to ENOMEM on failure if the linux documentation
is accurate, but if it is always set to ENOMEM on failure that gives you
no additional information.

On HP-UX, errno is set to EINVAL if memory has been detectably
corrupted. If there's insufficient memory, there's no clue as to the
reason. Of course, there's nothing to say that logically contiguous
memory must be physically contiguous.

In other words, checking errno in this instance is pointless on at least
one implementation and quite likely pointless on a number of others.
If there isn't contiguous memory of the size you need, you are out of
luck and should consider a different design. As an alternative, try
allocating the bigger memory blocks earlier.
This is better advice. I would also suggest checking for memory leaks
and making sure you are passing sensible numbers to calloc.

--
Al Balmer
Sun City, AZ
Aug 8 '06 #15
Flash Gordon wrote:
>Assuming you implementation has errno,


If it is a hosted C implementation then it has errno.
>you should test its value after
>the failing call to determine the cause of the failure.


The standard does not require that errno be set. The Unix98 standard
requires that it be set to ENOMEM on failure if the linux documentation
is accurate, but if it is always set to ENOMEM on failure that gives you
no additional information.

In other words, checking errno in this instance is pointless on at least
one implementation and quite likely pointless on a number of others.
Perhaps Solaris is unique in making the distinction by setting errno to
ENOMEM if the request is bigger than the system limits and EAGAIN if
there isn't enough memory available at the instant the request was made.

Either way, it's still worth checking your implementation' s documentation.

--
Ian Collins.
Aug 8 '06 #16
Ian Collins wrote:
Flash Gordon wrote:
>>Assuming you implementation has errno,

If it is a hosted C implementation then it has errno.
>>you should test its value after
the failing call to determine the cause of the failure.

The standard does not require that errno be set. The Unix98 standard
requires that it be set to ENOMEM on failure if the linux documentation
is accurate, but if it is always set to ENOMEM on failure that gives you
no additional information.

In other words, checking errno in this instance is pointless on at least
one implementation and quite likely pointless on a number of others.
Perhaps Solaris is unique in making the distinction by setting errno to
ENOMEM if the request is bigger than the system limits and EAGAIN if
there isn't enough memory available at the instant the request was made.
Interesting that there are some systems that set errno to different values.
Either way, it's still worth checking your implementation' s documentation.
That I'll agree with. When try to debug a nasty problem it is always
worth checking the implementations documentation to see what tools are
available. Whether that be errno being set to useful values when it is
not required or debugging versions of malloc or anything else.
--
Flash Gordon
Still sigless on this computer.
Aug 8 '06 #17
Flash Gordon <sp**@flash-gordon.me.ukwri tes:
Ian Collins wrote:
>Flash Gordon wrote:
>>>Assuming you implementation has errno,

If it is a hosted C implementation then it has errno.

you should test its value after
the failing call to determine the cause of the failure.

The standard does not require that errno be set. The Unix98 standard
requires that it be set to ENOMEM on failure if the linux documentation
is accurate, but if it is always set to ENOMEM on failure that gives you
no additional information.

In other words, checking errno in this instance is pointless on at least
one implementation and quite likely pointless on a number of others.
Perhaps Solaris is unique in making the distinction by setting errno to
ENOMEM if the request is bigger than the system limits and EAGAIN if
there isn't enough memory available at the instant the request was made.

Interesting that there are some systems that set errno to different values.
>Either way, it's still worth checking your implementation' s documentation.

That I'll agree with. When try to debug a nasty problem it is always
worth checking the implementations documentation to see what tools are
available. Whether that be errno being set to useful values when it is
not required or debugging versions of malloc or anything else.
In the greater majority of C development that requires use of such
functions I'd go even further and say its mandatory to check the
implementation specific errno settings. Not everything can be fully
portable : in which case make it fully platform safe - with the usual
documented caveats.
Aug 8 '06 #18
Michael McGarry wrote:
It turns out I was just eating up way too much memory.
You were asked previously in this thread not to top-post. Why do you
continue to abuse the newsgroup?


Brian
Aug 8 '06 #19
On Mon, 7 Aug 2006 21:26:41 UTC, "Michael McGarry"
<mi************ *@gmail.comwrot e:
Hi,

What would cause calloc() to return a NULL pointer?
calloc/malloc is unable to give you continous memory in the requested
size.
Is the system simply out of memory?
Maybe, maybe not.

The system may have some TB available but the process your calloc is
in has eaten all memory the system will give it.

By that: calloc is alöways useless except you are allocating real char
arrays.

calloc fills the memory block with binary 0. But there is no guaratee
that ponters floatingpoint variables or even structs are valid when
set to binary 0 chars. Character arrays are the only guaranteed to be
ready for usage when filled with binary 0. Pointers are not,
floatingpoint variables are not and even structs are not. On some
environments it may work, on others it will crash depending on the
underlying hardware.

So use malloc instead and initialise the variables with theyr typed
form of 0, e.g 0.0, or 0. calloc has no idea which kind of 0 the
variable needs, the compiler does, so it will convert a simple 0 to
its real type (0.0, (void*)0,...) for you.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
Aug 8 '06 #20

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

Similar topics

5
3643
by: Koster | last post by:
Sorry for the re-post, but my previous question was left unanswered. I have a question about the appropriateness of calloc. Consider an array of pointers to structs which need to be allocated space on the heap, for example: typedef struct myStruct *PMYSTRUCT; struct myStruct { int i; int j;
29
40416
by: David Hill | last post by:
Is there a difference between: /* code 1 */ struct sample test; test = malloc(sizeof(struct sample)); memset(&test, 0, sizeof(test)); /* code 2 */ struct sample test; test = calloc(1, sizeof(struct sample));
26
6764
by: dagger | last post by:
Hi there. I'm using C under FreeBSD with the gcc compiler and am having a bit of trouble using the calloc and realloc calls. As an example the code snippet: #include <stdio.h> int main() { char *ptr;
16
8994
by: laberth | last post by:
I've got a segmentation fault on a calloc and I don'tunderstand why? Here is what I use : typedef struct noeud { int val; struct noeud *fgauche; struct noeud *fdroit; } *arbre; //for those who don't speak french arbre means tree.
37
2584
by: Harsimran | last post by:
Can any one explain what are far pointers and what is the difference between malloc and calloc .Which is better ?
40
2530
by: boris | last post by:
Hi! I'm seeking some answers about what seems to be a memory leak. I have a loop that looks much like this: double *largeArray = (double*) calloc(); for (...) { printf("iteration #...\n"); for (...) { double *foo = (double*) calloc();
11
5800
by: lohith.matad | last post by:
Hi all, Though the purpose of both malloc() and calloc() is the same, and as we also know that calloc() initializes the alloacted locations to 'zero', and also that malloc() is used for bytes allocation whereas calloc() for chunk of memory allocation. Apart from these is there any strong reason that malloc() is prefered over calloc() or vice-versa? Looking forward for your clarrifications , possibly detailed.
2
1750
by: chingfulan | last post by:
I have the following code and I can not figure out why Stg2In returns a null pointer? "Stg2In = (float *)calloc(9*DataLen, sizeof(float)); " while "Stg2Out = (float *)calloc(9*DataLen, sizeof(float));" is sucessful? Can anyone Help? Thx
8
11182
by: venkatesh | last post by:
hai to everybody, i am having doubt in difference between the malloc and calloc function. please tell where to use and when to use those functions?
14
7503
by: Roka100 | last post by:
Hi all, I tried 2 programs : #include <stdio.h> #include <string.h> 1, int main(void){ char *str = NULL;
0
9456
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
9872
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9843
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
9713
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
8713
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...
0
5142
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
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3358
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2666
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.