Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Are enormous malloc()s conforming?

Reply
Thread Tools

Are enormous malloc()s conforming?

 
 
ais523
Guest
Posts: n/a
 
      12-13-2006
Consider the following program:

#include <stdlib.h>
#include <limits.h>

int main(void)
{
size_t s=SIZE_MAX;
size_t i;
char* p;
while(s--)
{
p=malloc(s);
if(p)
{
i=s;
while(i--) p[i]=42;
free(p);
}
}
return 0;
}

This program would be incredibly slow to run, but I'm interested in
whether or not it causes UB. There seem to be implementations (I
include the operating system as part of the implementation) which would
crash if they tried to run this, due to malloc returning non-NULL when
the system doesn't actually have the memory available (which is
non-conforming behaviour AFAICT). So: Is this UB, or is it merely a
common implementation non-conformance?
--
ais523

 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      12-13-2006
ais523 said:

> Consider the following program:
>
> #include <stdlib.h>
> #include <limits.h>
>
> int main(void)
> {
> size_t s=SIZE_MAX;
> size_t i;
> char* p;
> while(s--)
> {
> p=malloc(s);
> if(p)
> {
> i=s;
> while(i--) p[i]=42;
> free(p);
> }
> }
> return 0;
> }
>
> This program would be incredibly slow to run, but I'm interested in
> whether or not it causes UB.


It doesn't, as far as I can see.

> There seem to be implementations (I
> include the operating system as part of the implementation) which would
> crash if they tried to run this, due to malloc returning non-NULL when
> the system doesn't actually have the memory available (which is
> non-conforming behaviour AFAICT). So: Is this UB, or is it merely a
> common implementation non-conformance?


The latter.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
 
 
 
Peter Nilsson
Guest
Posts: n/a
 
      12-13-2006
Richard Heathfield wrote:
> ais523 said:
> > Consider the following program:
> >
> > #include <stdlib.h>
> > #include <limits.h>
> >
> > int main(void)
> > {
> > size_t s=SIZE_MAX;
> > size_t i;
> > char* p;
> > while(s--)
> > {
> > p=malloc(s);
> > if(p)
> > {
> > i=s;
> > while(i--) p[i]=42;
> > free(p);
> > }
> > }
> > return 0;
> > }
> >
> > This program would be incredibly slow to run, but I'm interested in
> > whether or not it causes UB.

>
> It doesn't, as far as I can see.
>
> > There seem to be implementations (I
> > include the operating system as part of the implementation) which would
> > crash if they tried to run this, due to malloc returning non-NULL when
> > the system doesn't actually have the memory available (which is
> > non-conforming behaviour AFAICT).
> > So: Is this UB, or is it merely a
> > common implementation non-conformance?

>
> The latter.


As usual, Richard giving both sides of the Möbius fence.

Michael Wojcik once wrote:

"Oh good, the lazy-allocation religious war has broken out again.

"If no implementation is allowed to fail under the conditions "malloc

returns non-null and later the program tries to use the returned
memory", then no real implementations are not broken. Memory access
can fail for reasons beyond the implementation's control.

"Lazy allocation has its proponents and opponents, and both have
reasonable arguments at their disposal, but this "lazy allocation
breaks conformance" one is not among them. Even if it's true (and no

one has presented a convincing proof of that, IMO), it's pointless,
since many of us have to use implementations on platforms which use
lazy allocation. If those implementations conform except in using
lazy allocation, that will have to be good enough, since there is no
alternative."

--
Peter

 
Reply With Quote
 
Spiros Bousbouras
Guest
Posts: n/a
 
      12-13-2006
Peter Nilsson wrote:
> Richard Heathfield wrote:
> > ais523 said:
> > > Consider the following program:
> > >
> > > #include <stdlib.h>
> > > #include <limits.h>
> > >
> > > int main(void)
> > > {
> > > size_t s=SIZE_MAX;
> > > size_t i;
> > > char* p;
> > > while(s--)
> > > {
> > > p=malloc(s);
> > > if(p)
> > > {
> > > i=s;
> > > while(i--) p[i]=42;
> > > free(p);
> > > }
> > > }
> > > return 0;
> > > }
> > >
> > > This program would be incredibly slow to run, but I'm interested in
> > > whether or not it causes UB.

> >
> > It doesn't, as far as I can see.
> >
> > > There seem to be implementations (I
> > > include the operating system as part of the implementation) which would
> > > crash if they tried to run this, due to malloc returning non-NULL when
> > > the system doesn't actually have the memory available (which is
> > > non-conforming behaviour AFAICT).
> > > So: Is this UB, or is it merely a
> > > common implementation non-conformance?

> >
> > The latter.

>
> As usual, Richard giving both sides of the Möbius fence.
>
> Michael Wojcik once wrote:
>
> "Oh good, the lazy-allocation religious war has broken out again.
>
> "If no implementation is allowed to fail under the conditions "malloc
>
> returns non-null and later the program tries to use the returned
> memory", then no real implementations are not broken. Memory access
> can fail for reasons beyond the implementation's control.


Well yes but the opening poster specified that he considers
the operating system part of the implementation. Without
that condition perhaps Richard's answer would have been
different.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Enormous Input and Output Test n00m Python 32 05-30-2012 11:38 AM
Best way to query a enormous database table Lonifasiko Java 20 10-21-2006 09:16 AM



Advertisments