473,545 Members | 1,558 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to make malloc() fail ?

This is regarding to test an SDK memory stuff.
In what situation malloc gets fail.
any comment/reply pls....
regards

Jul 17 '07 #1
34 13325
ni************@ gmail.com wrote:
This is regarding to test an SDK memory stuff.
In what situation malloc gets fail.
any comment/reply pls....
In statement:

ptr = malloc(N);

malloc will return NULL when it's not able to allocate N bytes of
storage.

Ways to force malloc to fail in it's allocation are system specific,
so asking in an appropriate group would be more suitable.

<OT>
There are "stress testing" programs which can simulate a low free
memory condition. Alternatively your operating system might have
facilities for enforcing per user or per program quotas for memory
allocation.
</OT>

Jul 17 '07 #2
santosh said:
ni************@ gmail.com wrote:
>This is regarding to test an SDK memory stuff.
In what situation malloc gets fail.
any comment/reply pls....

In statement:

ptr = malloc(N);

malloc will return NULL when it's not able to allocate N bytes of
storage.

Ways to force malloc to fail in it's allocation are system specific,
so asking in an appropriate group would be more suitable.
You see, this is what I don't like about this group. "Always with you it
cannot be done", as Yoda rightly said.

Okay, in the obvious sense, you're right that you can't force malloc to
fail at your command, in the general case (requests for 0xFFFFFFFF
bytes don't count, of course - we want a more general solution than
that).

But it seems to me that this isn't really what the OP wants, despite the
wording of his question. What he wants is a software environment in
which memory allocation failures can be simulated for testing purposes.
And that *is* possible within ISO C. I know because I've done it, and
quite recently too (like, within the last month or so).

I'm not going to explain how, because I wouldn't want to distract the OP
from focusing on the more basic skills he should be developing before
embarking on something as intellectually challenging as programming.

But I invite you to consider the problem yourself, as an exercise in
creative thinking. If you trust what I say (and I suspect that you
might), you can reason thusly: "there exists at least one solution to
the problem of testing allocation failure paths in portable C code;
knowing this, and knowing that I'm no fool, I ought to be able to find
a solution myself - and maybe it will be useful in my own programming".

You know it won't be obvious. You know it will involve a little effort.
But you also know that, if Richard can do it, it can't be *that* hard.

I am beginning to despair of comp.lang.c. :-(

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 17 '07 #3
Richard Heathfield wrote:

I apologise in advance if this post appears multiple times. Google has
been wretched to use today.
santosh said:
ni************@ gmail.com wrote:
>
This is regarding to test an SDK memory stuff.
In what situation malloc gets fail.
any comment/reply pls....
In statement:

ptr = malloc(N);

malloc will return NULL when it's not able to allocate N bytes of
storage.

Ways to force malloc to fail in it's allocation are system specific,
so asking in an appropriate group would be more suitable.

You see, this is what I don't like about this group. "Always with you it
cannot be done", as Yoda rightly said.

Okay, in the obvious sense, you're right that you can't force malloc to
fail at your command, in the general case (requests for 0xFFFFFFFF
bytes don't count, of course - we want a more general solution than
that).

But it seems to me that this isn't really what the OP wants, despite the
wording of his question. What he wants is a software environment in
which memory allocation failures can be simulated for testing purposes.
And that *is* possible within ISO C. I know because I've done it, and
quite recently too (like, within the last month or so).
Yes. That's why I said "ways to force malloc", which isn't, AFAIK,
possible within portable C. As you've noted, ways to simulate
allocation failures *are* possible within Standard C.

I still don't really understand exactly what the OP wants to know
about. In hindsight, I should have pointed out to the OP that it's
possible to simulate memory allocation failures with just Standard C,
but since I myself usually rely on external programs for this purpose,
the fact skipped my mind.

<snip>
I am beginning to despair of comp.lang.c. :-(
I still consider this an excellent group for C, but then again, I've
been here only for two years, so I don't have a first hand experience
of it's heydays.

Jul 17 '07 #4
Keith Thompson said:

<snip>
If you're trying to *cause* malloc to fail, the obvious way to do it
is to request some huge amount of memory, repeatedly if necessary.
Fortunately, there is also at least one non-obvious way. :-)

(And yes, it involves cheating, but *not* by resorting to non-portable
code.)

<snip>

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 17 '07 #5
On Jul 17, 4:03 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
santosh said:
niranjan.si...@ gmail.com wrote:
This is regarding to test an SDK memory stuff.
In what situation malloc gets fail.
any comment/reply pls....
In statement:
ptr = malloc(N);
malloc will return NULL when it's not able to allocate N bytes of
storage.
Ways to force malloc to fail in it's allocation are system specific,
so asking in an appropriate group would be more suitable.

You see, this is what I don't like about this group. "Always with you it
cannot be done", as Yoda rightly said.

Okay, in the obvious sense, you're right that you can't force malloc to
fail at your command, in the general case (requests for 0xFFFFFFFF
bytes don't count, of course - we want a more general solution than
that).

But it seems to me that this isn't really what the OP wants, despite the
wording of his question. What he wants is a software environment in
which memory allocation failures can be simulated for testing purposes.
And that *is* possible within ISO C. I know because I've done it, and
quite recently too (like, within the last month or so).

I'm not going to explain how, because I wouldn't want to distract the OP
from focusing on the more basic skills he should be developing before
embarking on something as intellectually challenging as programming.

But I invite you to consider the problem yourself, as an exercise in
creative thinking. If you trust what I say (and I suspect that you
might), you can reason thusly: "there exists at least one solution to
the problem of testing allocation failure paths in portable C code;
knowing this, and knowing that I'm no fool, I ought to be able to find
a solution myself - and maybe it will be useful in my own programming".

You know it won't be obvious. You know it will involve a little effort.
But you also know that, if Richard can do it, it can't be *that* hard.

I am beginning to despair of comp.lang.c. :-(
I use this to create low memory situations.
You can do it all in a big clump, or start lots of them with smaller
mouthfuls.
Of course, to find the exact failure point, a binary search is what is
wanted.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static char string[32767];
int main(int argc, char **argv)
{
size_t megabytes = 1024u * 1024u;
size_t megs = 256;
size_t memreq;
char *waster;
if (argc 1)
megs = atoi(argv[1]);
if (megs == 0)
megs = 256;

memreq = megs * megabytes;
waster = malloc(memreq);
if (waster == NULL) {
puts("Memory allocation failed");
} else {
printf("Press the <Enterkey to release %u megs memory\n",
(unsigned) megs);
fgets(string, sizeof string, stdin);
free(waster);
}
return 0;
}

Jul 18 '07 #6
user923005 said:

<snip>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static char string[32767];
int main(int argc, char **argv)
{
size_t megabytes = 1024u * 1024u;
1024u * 1024u is not guaranteed to fit in a size_t, so you might end up
with a lower value therein (e.g. 0, if size_t is 16 bits).
size_t megs = 256;
size_t memreq;
char *waster;
if (argc 1)
megs = atoi(argv[1]);
I recommend strtoul(argv[1], NULL, 10) instead of atoi(argv[1]).

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 18 '07 #7
At about the time of 7/17/2007 3:36 AM, ni************@ gmail.com stated
the following:
This is regarding to test an SDK memory stuff.
In what situation malloc gets fail.
any comment/reply pls....
regards
something like this:

ptr = malloc(0xFFFFFF FF);

That will usually cause the allocation to fail...it might also cause the
host OS to abort the program too. I know that FreeBSD does not give a
failure indication, the OS kills the program.

--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m
Jul 18 '07 #8
On Jul 17, 12:03 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
santosh said:
niranjan.si...@ gmail.com wrote:
This is regarding to test an SDK memory stuff.
In what situation malloc gets fail.
any comment/reply pls....
In statement:
ptr = malloc(N);
malloc will return NULL when it's not able to allocate N bytes of
storage.
Ways to force malloc to fail in it's allocation are system specific,
so asking in an appropriate group would be more suitable.

You see, this is what I don't like about this group. "Always with you it
cannot be done", as Yoda rightly said.
<snip>
I'm not going to explain how, because I wouldn't want to distract the OP
from focusing on the more basic skills he should be developing before
embarking on something as intellectually challenging as programming.
You see, this is what I don;t like about this group. "Always with you
it must be condescending."

I'm not sure why you feel you have to comment on the ability of the
OP. Stinks of pure arrogance to me.

What is clc for, if not to ask questions? You yourself have said this
was on-topic. Why not give an answer, instead of your usual b.s. "I
know but I'm not telling you"?

Please get over yourself. If you're not going to help, be quiet. And
please stop answering spam.

To the OP - this can be done, but is probably best done in a platform-
specific manner. Try asking in a group specific to your platform, or
come back if you want a general solution.

Doug

P.S. My apologies if someone else is posting in RH's name

Jul 18 '07 #9
Doug said:
On Jul 17, 12:03 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
>>
niranjan.si...@ gmail.com wrote:
This is regarding to test an SDK memory stuff.
In what situation malloc gets fail.
any comment/reply pls....
<snip>
>
>I'm not going to explain how, because I wouldn't want to distract the
OP from focusing on the more basic skills he should be developing
before embarking on something as intellectually challenging as
programming.

You see, this is what I don;t like about this group. "Always with you
it must be condescending."

I'm not sure why you feel you have to comment on the ability of the
OP. Stinks of pure arrogance to me.
Well, it wasn't intended that way, but how you interpret it is your
affair, not mine.
What is clc for, if not to ask questions?
It's for discussions about C programming. The whole question-answer
thing is, in my view, merely one way in which discussions start. It is
true that it happens to be the most common way. But if it were not,
then some other way would be, and by your apparent reasoning we'd all
presume /that/ way best to describe the raison d'etre of this group.
You yourself have said this
was on-topic. Why not give an answer, instead of your usual b.s. "I
know but I'm not telling you"?
Usual? It is actually quite rare for me to answer in such a way, as you
ought to know if you believe yourself to be in a position to comment on
my "usual" style. If you truly believe that what I usually post is
"b.s.", then would it not have made sense for you to killfile me a long
time ago?
Please get over yourself. If you're not going to help, be quiet.
1) I have the same freedom to post here as anyone else.
2) I didn't see you providing an answer to his question, so your
position is somewhat hypocritical, is it not?
3) (This is the one you won't believe, but so be it.) I *was* actually
trying to help the OP, by suggesting that he focuses on developing his
ability to express his ideas cogently; if he takes my advice, this will
have a very positive effect on his ability to write computer programs.
And please stop answering spam.
It is true that I occasionally post replies to spam, although it is
rare. I have the same freedom to do that as you have to ask me to stop.
I rarely use that freedom, but yes, I do use it sometimes. That's up to
me. If you don't like what I write, well, that's up to you.

And now I have my own suggestion to make to you: if you must tell other
people how to behave, it might be as well for you to be prepared for
the possibility that those others might not see the world the way you
do. For example, you say you found my tone arrogant, and yet I know
that I did not intend to convey arrogance. That suggests that I may
need to pay closer attention to my tone. Likewise, however, I found
your reply to be self-serving, hypocritical, and non-constructive. If
that was not the kind of reply you intended to write, perhaps you, too,
need to pay closer attention to your tone.

I am not aware that you have been particularly active in providing help
to those who seek it in this forum, or that you have contributed very
much to the discussions here in comp.lang.c. It's easy to snipe from
the sidelines. If you want me to take your criticisms seriously, *get
involved*. Taking part in discussions, answering questions, getting
tangled up in amicable fights about C, and taking one's lumps when one
gets it wrong, are all part of the rough and tumble of daily
comp.lang.c life, and those who are actively involved are far more
likely to be taken seriously than those who are not.
>
To the OP - this can be done, but is probably best done in a platform-
specific manner. Try asking in a group specific to your platform, or
come back if you want a general solution.
<sarcasm>Very good.</sarcasmAnd what did that add to what has already
been said elsethread?
Doug

P.S. My apologies if someone else is posting in RH's name
This P.S. suggests that you are perfectly well aware that my "usual"
tone is not what you claimed it to be, earlier in your article.

FCOL, furrfu, etc.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 18 '07 #10

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

Similar topics

25
5047
by: H.A. Sujith | last post by:
If malloc fails what should I do? 1. Exit imediately. 2. Print an error message (or put a log entry) and exit. 3. Print an error message (or put a log entry) and continue execution (after possibly recovering from the error). Printing an error message might be difficult in a graphical environment. --
27
4242
by: Chess Saurus | last post by:
I'm getting a little bit tired of writing if (a = malloc(...) == NULL) { // error code } I mean, is it really possible that a malloc call could fail, except in the case of running out of virtual memory? -Chess
54
7795
by: Neo | last post by:
Hi Folks, I've a simple qestion related to dynamic memory allocation in C here is the code: #include <stdio.h> int main() {
14
3259
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
42
3094
by: Martin Jørgensen | last post by:
Hi, I'm trying to move a matlab program into c language. For those who knows matlab, this is the line I want to program in c: hx(1:nx,1:ny) = 0; % nx=10, ny=10 It works on a 2-dimensional array (size is 10*10), setting all the values inside the 10*10 matrix to zero. My C-function looks like this:
40
540
by: ramu | last post by:
Hi, what happens when i run the below code? main() { int *p; while(1) p= (int *)malloc(1000); } Do i get segmentation fault?
71
19040
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.
173
7952
by: Marty James | last post by:
Howdy, I was reflecting recently on malloc. Obviously, for tiny allocations like 20 bytes to strcpy a filename or something, there's no point putting in a check on the return value of malloc. OTOH, if you're allocating a gigabyte for a large array, this might fail, so you should definitely check for a NULL return.
0
7401
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...
0
7656
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. ...
0
7807
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...
0
5971
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...
1
5326
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...
0
4944
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...
0
3442
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1014
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
703
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...

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.