Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   How to check whether malloc has allocated memory properly in case ifmalloc(0) can return valid pointer (http://www.velocityreviews.com/forums/t740228-how-to-check-whether-malloc-has-allocated-memory-properly-in-case-ifmalloc-0-can-return-valid-pointer.html)

Shivanand Kadwadkar 12-16-2010 09:10 AM

How to check whether malloc has allocated memory properly in case ifmalloc(0) can return valid pointer
 
i want to check after calling malloc whether allocation is successful
or not. previously i have done like

ptr=malloc(size);

if(ptr==NULL)

But i came to know malloc can return valid pointer in case if argument
is zero.

one solution is

if(size==0)
ptr=NULL;
else
ptr=malloc(size);

if you have any other suggestion please welcome.

Mark Bluemel 12-16-2010 09:34 AM

Re: How to check whether malloc has allocated memory properly incase if malloc(0) can return valid pointer
 
On 12/16/2010 09:10 AM, Shivanand Kadwadkar wrote:
> i want to check after calling malloc whether allocation is successful
> or not. previously i have done like
>
> ptr=malloc(size);
>
> if(ptr==NULL)
>
> But i came to know malloc can return valid pointer in case if argument
> is zero.


And that pointer can successfully be passed to free(). And you are
certain to be able to store at least zero bytes at the location pointed
to by the pointer, so what's the problem?

If the issue is that you are likely to call malloc() with an argument of
zero and then attempt to store data at the address returned, your
problem is not really with malloc() is it?

I think it would be useful for you to explain in more detail what
problem you think you need help with, as I'm not at all clear about that.

BartC 12-16-2010 09:58 AM

Re: How to check whether malloc has allocated memory properly in case if malloc(0) can return valid pointer
 

"Mark Bluemel" <mark_bluemel@pobox.com> wrote in message
news:iecmem$ism$1@news.eternal-september.org...
> On 12/16/2010 09:10 AM, Shivanand Kadwadkar wrote:
>> i want to check after calling malloc whether allocation is successful
>> or not. previously i have done like
>>
>> ptr=malloc(size);
>>
>> if(ptr==NULL)
>>
>> But i came to know malloc can return valid pointer in case if argument
>> is zero.

>
> And that pointer can successfully be passed to free(). And you are certain
> to be able to store at least zero bytes at the location pointed to by the
> pointer, so what's the problem?


One issue might be that, if you call malloc(0) enough times, you can run out
of memory!

--
Bartc




Malcolm McLean 12-16-2010 11:38 AM

Re: How to check whether malloc has allocated memory properly in caseif malloc(0) can return valid pointer
 
On Dec 16, 11:34*am, Mark Bluemel <mark_blue...@pobox.com> wrote:
>
> If the issue is that you are likely to call malloc() with an argument of
> zero and then attempt to store data at the address returned, your
> problem is not really with malloc() is it?
>

The code could be like this

employees = malloc(N * sizeof(struct Employee));
if(!employees)
goto error_handler;
memcpy(employees, temparray, N * sizeof(struct Employee));

the code is correct is malloc() returns non-zero for a zero allocation
and N == 0 is allowed. It's incoorect is malloc() returns zero in this
situation.



Tom St Denis 12-16-2010 12:04 PM

Re: How to check whether malloc has allocated memory properly in caseif malloc(0) can return valid pointer
 
On Dec 16, 6:38*am, Malcolm McLean <malcolm.mcle...@btinternet.com>
wrote:
> On Dec 16, 11:34*am, Mark Bluemel <mark_blue...@pobox.com> wrote:
>
> > If the issue is that you are likely to call malloc() with an argument of
> > zero and then attempt to store data at the address returned, your
> > problem is not really with malloc() is it?

>
> The code could be like this
>
> employees = malloc(N * sizeof(struct Employee));
> if(!employees)
> * goto error_handler;
> memcpy(employees, temparray, N * sizeof(struct Employee));
>
> the code is correct is malloc() returns non-zero for a zero allocation
> and N == 0 is allowed. It's incoorect is malloc() returns zero in this
> situation.


if (N) {
employees = calloc(N, sizeof *employees);
if(!employees) {
goto error_handler;
}
memcpy(employees, temparray, N * sizeof *employees);
} else {
employees = NULL;
}

There I both fixed the problem and cleaned up your code, that'll be
100 shillings please, due net 30.

Tom

Eric Sosman 12-16-2010 02:21 PM

Re: How to check whether malloc has allocated memory properly incase if malloc(0) can return valid pointer
 
On 12/16/2010 4:10 AM, Shivanand Kadwadkar wrote:
> i want to check after calling malloc whether allocation is successful
> or not. previously i have done like
>
> ptr=malloc(size);
>
> if(ptr==NULL)
>
> But i came to know malloc can return valid pointer in case if argument
> is zero.


Yes: Just like malloc(N), malloc(0) can return NULL or non-NULL.
Whichever it returns, though, you must not try to read or store through
that pointer. If it's NULL, well, you mustn't try to read or store
through NULL. If it's non-NULL you can read or store anything whose
size does not exceed the argument value, but since *every* C data type
is at least one byte wide there is nothing that fits in 0 bytes, hence
nothing you can safely store in 0 bytes.

To test for "success" of malloc(N), you could write

ptr = malloc(N);
if (ptr == NULL && N > 0) {
// failure
} else {
// "success"
}

I put "success" in quotes because of an ambiguity that is meaningless
in practice: If malloc(0) returns NULL, we cannot tell whether it
"succeeded" or "failed," but it really doesn't make any difference.

> one solution is
>
> if(size==0)
> ptr=NULL;
> else
> ptr=malloc(size);


After this, a simple `if (ptr == NULL)' leaves you with the same
problem you had to begin with.

> if you have any other suggestion please welcome.


One thing to keep in mind is that this `size' value comes from
somewhere, and will (presumably) govern what you later try to store
in the allocated area. If your program can calculate `size' as 0,
it should also never try to store more than 0 bytes in the allocated
area -- in which case, all will be well no matter what malloc(0)
returned.

Since a 0-byte memory area is mostly useless, you probably should
not be allocating a large number of them -- if you are, you've most
likely got a bug somewhere. Usually, 0-byte allocations arise when
you've got a data structure whose size you adjust up and down as the
circumstances require. Maybe you're keeping track of employees'
birthdays using 366 dynamically-allocated blocks, and the only person
born on February 29 quits: Your program might do

count[date] -= 1; // making it zero
Person *tmp = realloc(persons[date], count[date] * size *tmp);
if (tmp == NULL && count[date] > 0) {
// Can't delete: Hire him back again!
} else {
persons[date] = tmp;
}

In situations like this, a 0-byte allocation could be considered
"normal." But if you're routinely making thousands and thousands of
0-byte allocations, there's likely something wrong elsewhere.

--
Eric Sosman
esosman@ieee-dot-org.invalid

Joachim Schmitz 12-16-2010 03:39 PM

Re: How to check whether malloc has allocated memory properly in case if malloc(0) can return valid pointer
 
Shivanand Kadwadkar wrote:
> i want to check after calling malloc whether allocation is successful
> or not. previously i have done like
>
> ptr=malloc(size);
>
> if(ptr==NULL)
>
> But i came to know malloc can return valid pointer in case if argument
> is zero.
>
> one solution is
>
> if(size==0)
> ptr=NULL;
> else
> ptr=malloc(size);
>
> if you have any other suggestion please welcome.


IMHO the case when malloc(0) return NULL is more interesting, as there you
need to adjust the error checking, i.e. not exit the program due to lack of
memory if size was 0.

Bye, Jojo


Tom St Denis 12-16-2010 03:43 PM

Re: How to check whether malloc has allocated memory properly in caseif malloc(0) can return valid pointer
 
On Dec 16, 10:39*am, "Joachim Schmitz" <nospam.j...@schmitz-
digital.de> wrote:
> Shivanand Kadwadkar wrote:
> > i want to check after calling malloc whether allocation is successful
> > or not. previously i have done like

>
> > ptr=malloc(size);

>
> > if(ptr==NULL)

>
> > But i came to know malloc can return valid pointer in case if argument
> > is zero.

>
> > one solution is

>
> > if(size==0)
> > ptr=NULL;
> > else
> > ptr=malloc(size);

>
> > if you have any other suggestion please welcome.

>
> IMHO the case when malloc(0) return NULL is more interesting, as there you
> need to adjust the error checking, i.e. not exit the program due to lack of
> memory if size was 0.


Agreed, I can't ever think of a legit use for malloc'ing zero bytes.
It should return a NULL to trip up [what should be in place] normal
error detection.

August Karlstrom 12-16-2010 03:50 PM

Re: How to check whether malloc has allocated memory properly incase if malloc(0) can return valid pointer
 
On 2010-12-16 10:10, Shivanand Kadwadkar wrote:
> i want to check after calling malloc whether allocation is successful
> or not. previously i have done like
>
> ptr=malloc(size);
>
> if(ptr==NULL)
>
> But i came to know malloc can return valid pointer in case if argument
> is zero.
>
> one solution is
>
> if(size==0)
> ptr=NULL;
> else
> ptr=malloc(size);
>
> if you have any other suggestion please welcome.


If possible I would design the program so that `size' is only allowed to
be a positive number. If it is a library I would simply assert that size
> 0 and put the responsibility on the client to make sure that the

condition is met (the fail fast strategy).


/August

--
The competent programmer is fully aware of the limited size of his own
skull. He therefore approaches his task with full humility, and avoids
clever tricks like the plague. --Edsger Dijkstra

Jens Thoms Toerring 12-16-2010 04:28 PM

Re: How to check whether malloc has allocated memory properly in case if malloc(0) can return valid pointer
 
Ered China Luin <chine.bleu@yahoo.com> wrote:
> You are also allowed to envelope malloc and allocations for your own use.
> For example allocate could add an extra 4 bytes before and after with a
> check pattern, with dispose verifying that to try to find block overruns.


I would be a bit careful with putting stuff at the start of the
allocated area - if you put e.g. 4 bytes at the start and then
use the original address + 4 then this may not be properly aligned
for all possible types. You can be only be sure about that for the
original address returned by malloc().

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de


All times are GMT. The time now is 08:41 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.