N
niranjan.singh
This is regarding to test an SDK memory stuff.
In what situation malloc gets fail.
any comment/reply pls....
regards
In what situation malloc gets fail.
any comment/reply pls....
regards
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.
santosh said:
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 am beginning to despair of comp.lang.c. :-(
[email protected] said:This is regarding to test an SDK memory stuff.
In what situation malloc gets fail.
Keith said:Second, on some systems (I think Linux is an example), malloc doesn't
actually indicate failure if it can't allocate the requested memory.
Instead, it allocates a range of addresses to your program without
actually allocating the memory. The memory is only allocated when you
attempt to access it. At that point, it's too late to indicate a
simple allocation failure, and the OS responds by killing processes.
This behavior is arguably non-conforming.
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.
santosh said:
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. :-(
#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]);
This is regarding to test an SDK memory stuff.
In what situation malloc gets fail.
any comment/reply pls....
regards
santosh said:
You see, this is what I don't like about this group. "Always with you it
cannot be done", as Yoda rightly said.
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
This is regarding to test an SDK memory stuff.
In what situation malloc gets fail.
any comment/reply pls....
regards
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.
santosh said:"]
At about the time of 7/17/2007 3:36 AM, (e-mail address removed)
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(0xFFFFFFFF);
That will usually cause the allocation to fail...
replace 0xFFFFFFFF with (size_t)-1 or the macro SIZE_MAX (from
<stdint.h>; size_t is not guaranteed to be unsigned),
Flash said:Bernhard Müller wrote, On 22/07/07 20:24:
Actually, it *is* guaranteed to be unsigned. This is why you have
SIZE_MAX but not SIZE_MIN.
It is guaranteed to be an unsigned integer type, which might or might not be
the type specified by "unsigned" (aka "unsigned int").
Flash said:Harald van Dijk wrote, On 22/07/07 21:08:
I meant an unsigned type, not unsigned int. I should have been clear.
Even if size_t is an unsigned type other than unsigned int,I know that's what you meant. I meant that it doesn't necessarily contradict
Bernhard Müller's claim.
I think it also depends on the size of the blocks.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.
There are two problems with this approach. [snip]
Second, on some systems (I think Linux is an example), malloc doesn't
actually indicate failure if it can't allocate the requested memory.
Instead, it allocates a range of addresses to your program without
actually allocating the memory. The memory is only allocated when you
attempt to access it. At that point, it's too late to indicate a
simple allocation failure, and the OS responds by killing processes.
This behavior is arguably non-conforming.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.