473,765 Members | 2,121 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
34 13406
In article <11************ **********@57g2 000hsv.googlegr oups.com>,
crox <cr*****@gmail. comwrote:
>it is really platform specific, for example in my system libc, malloc
has this implemetation:

...
/* why would you ever want a negative malloc, this would mean */
/* a very large positive number which can't be filled */

if ((int)nbytes < 0){
The argument to malloc() has type size_t, which is unsigned, so you
*can't* pass it a negative value. It may well be true that values
corresponding to negative ints are usually the result of a bug in the
program causing a negative value to be converted to a large size_t,
but I'm not sure why your code bothers to make this check, since if
requests for "very large" amounts of memory can't be filled, it will
fail anyway without the check.

I think the comment would be more accurate if it read "Why would you
ever want to malloc such a huge amount? It can't be done and is more
likely an erroneous attempt to malloc a negative size".

-- Richard
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Jul 25 '07 #31
On Jul 24, 3:12 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
Ben Pfaff said:

Please, tell us how.

Sorry to be so vague about this. Will it suffice to say that I use a
wrapper?


How?
Assume that you have a program in which malloc is not wrappred.
How do you wrap it without going through and replacing all the calls
to
malloc? Sure

#define malloc wrap_malloc

will probably work, but you can't redefine a library function in
portable code.
(given that you have a wrapper function things are straightforward .
Store state as a global or pseudo-global (static in a module),
manipulate with wrap_malloc_cha nge_state()
then the wrapper function will return NULL (fail) or call
malloc, depending on the state)
- William Hughes

Jul 25 '07 #32
On Jul 25, 4:41 am, rich...@cogsci. ed.ac.uk (Richard Tobin) wrote:
In article <1185352068.681 434.313...@57g2 000hsv.googlegr oups.com>,

crox <craz...@gmail. comwrote:
it is really platform specific, for example in my system libc, malloc
has this implemetation:
...
/* why would you ever want a negative malloc, this would mean */
/* a very large positive number which can't be filled */
if ((int)nbytes < 0){

The argument to malloc() has type size_t, which is unsigned, so you
*can't* pass it a negative value. It may well be true that values
corresponding to negative ints are usually the result of a bug in the
program causing a negative value to be converted to a large size_t,
but I'm not sure why your code bothers to make this check, since if
requests for "very large" amounts of memory can't be filled, it will
fail anyway without the check.

I think the comment would be more accurate if it read "Why would you
ever want to malloc such a huge amount? It can't be done and is more
likely an erroneous attempt to malloc a negative size".
If the chip can address it, and if I have that much available, I would
be rather put out if the malloc() function arbitrarily decided not to
give it to me.
That little snippet of code is by a programmer trying to be more
clever than he ought to be.
IMO-YMMV.

Jul 25 '07 #33
REH
On Jul 17, 6:36 am, "niranjan.si... @gmail.com"
<niranjan.si... @gmail.comwrote :
This is regarding to test an SDK memory stuff.
In what situation malloc gets fail.
any comment/reply pls....
regards
If you just wish to test your error paths, you don't really need to
make malloc fail, just make your program think that it did:

#if TESTING

/* function to determine if malloc should fail */
int malloc_should_f ail(void);

#define malloc(S) (malloc_should_ fail() ? NULL : (malloc)(S))

#endif

I don't believe it is portable to override malloc with a macro, but
its only for testing purposes, not your release builds.

Jul 25 '07 #34
In article <11************ **********@o61g 2000hsh.googleg roups.com>,
user923005 <dc*****@connx. comwrote:
>/* why would you ever want a negative malloc, this would mean */
/* a very large positive number which can't be filled */
>If the chip can address it, and if I have that much available, I would
be rather put out if the malloc() function arbitrarily decided not to
give it to me.
I assume from the comment that it isn't available. It's not unusual
for half the address space to be used for kernel memory. Of course,
if size_t is 64 bits, there's no prospect of more than half of it being
available anyway in the foreseeable future.

-- Richard
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Jul 25 '07 #35

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

Similar topics

25
5076
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
4279
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
7860
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
3292
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
3166
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
19123
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
8167
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
9398
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
10007
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
9951
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
9832
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
8831
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
6649
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
5275
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
5419
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.