473,725 Members | 2,070 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 #1
21 1940
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.


Brian
Aug 7 '06 #2
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,

Michael

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.


Brian
Aug 7 '06 #3
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, you should test its value after
the failing call to determine the cause of the failure.

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.

--
Ian Collins.
Aug 7 '06 #4
Michael McGarry wrote:
Hi,

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

Is the system simply out of memory?

Regards,

Michael
Something like that. Given rational arguments, malloc, calloc and
realloc seldom error out. But you can stress them. For example size_t is
an unsigned 32-bit type on my system. calloc multiplies its two size_t
arguments to come up with size. If these two are not chosen with some
sanity, calloc will barf because I don't have anything near 2^64 bytes
of memory to address.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Aug 7 '06 #5
Ian Collins <ia******@hotma il.comwrites:
Michael McGarry wrote:
>Default User wrote:
>>>Michael McGarry wrote:
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, 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".
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.
Some systems impose limits on how much memory a given program (or
process, or user, or whatever) can allocate. You may or may not be
able to increase that limit; the details are system-specific.

It's also possible that you haven't run out of available memory,
rather than you've done something to mess up the system's internal
data structures.

Incidentally, calloc() isn't necessarily more useful than the simpler
malloc(); calloc() initializes the allocated memory to all-bits-zero,
but that's not necessarily as useful as you might think. In
particular, neither floating-point 0.0 nor null pointers are
guaranteed to be represented as all-bits-zero.

--
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.
u
Aug 7 '06 #6
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.
Don't top-post, text rearranged.
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?
Pretty much. However, it's not a common occurrence for modern systems.
Is this theoretical, or an actual problem you've encountered?


Brian
Aug 7 '06 #7
Keith Thompson wrote:
Ian Collins <ia******@hotma il.comwrites:
>>Michael McGarry wrote:
>>>Default User wrote:

Michael McGarry wrote:

>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, 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.
I was careful not to make that assumption.
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".
True, but all it takes is a quick read of the platform's documentation
to see if it does. Solaris and I think Linux makes the distinction.
>
>>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.

Some systems impose limits on how much memory a given program (or
process, or user, or whatever) can allocate. You may or may not be
able to increase that limit; the details are system-specific.
If you know your allocation is within these bounds, there's a pretty
good chance you have run into a fragmentation issue, so my suggestion is
still worth investigation.

--
Ian Collins.
Aug 7 '06 #8
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.

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.
--
Flash Gordon
Still sigless on this computer.
Aug 7 '06 #9
Ian Collins <ia******@hotma il.comwrites:
Keith Thompson wrote:
>Ian Collins <ia******@hotma il.comwrites:
>>>Michael McGarry wrote:

Default User wrote:

>Michael McGarry wrote:
>
>>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, 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 .

I was careful not to make that assumption.
>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".
True, but all it takes is a quick read of the platform's documentation
to see if it does. Solaris and I think Linux makes the distinction.
Would Linux, in default, ever return NULL? The default is to return
pointers to memory that is not even available : optimistic
strategy. Yuck :) I might, time allowing, knock up a program to fragment
up the memory and then try a huge alloc.

Q: does the swapfile come on "seamlessly " on most platforms here? In
which case would we ever see malloc failing assuming enough swap? How
does that work (yes I know OT)? Even if the swap is fragmented is the
memory block contiguous?

Aug 7 '06 #10

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

Similar topics

5
3640
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
40405
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
6757
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
8990
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
2573
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
2526
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
5794
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
1746
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
11178
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
7497
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
8888
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
8752
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
9401
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9113
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
8097
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
6011
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
4519
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
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3221
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.