Are enormous malloc()s conforming?

Discussion in 'C Programming' started by ais523, Dec 13, 2006.

  1. ais523

    ais523 Guest

    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=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
     
    ais523, Dec 13, 2006
    #1
    1. Advertising

  2. 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=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.
     
    Richard Heathfield, Dec 13, 2006
    #2
    1. Advertising

  3. 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=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
     
    Peter Nilsson, Dec 13, 2006
    #3
  4. 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=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.
     
    Spiros Bousbouras, Dec 13, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Rootham
    Replies:
    2
    Views:
    629
    Richard Tobin
    Aug 28, 2003
  2. Peter Rooney

    tool to fix non-conforming xml?

    Peter Rooney, Feb 5, 2006, in forum: XML
    Replies:
    2
    Views:
    593
    Henry S. Thompson
    Feb 6, 2006
  3. ik
    Replies:
    6
    Views:
    825
    Rich Grise
    Sep 11, 2004
  4. Lonifasiko
    Replies:
    20
    Views:
    921
    Patrick May
    Oct 21, 2006
  5. n00m
    Replies:
    32
    Views:
    1,449
    rishimukherjee
    May 30, 2012
Loading...

Share This Page