Re: Wikipedia goes wrong: is this possible?

Discussion in 'C Programming' started by James Kuyper, Jun 13, 2013.

  1. James Kuyper

    James Kuyper Guest

    On 06/13/2013 08:27 AM, paskali wrote:
    > I do not know you but i consider Wikipedia one of the more ugly things
    > on internet and in general in the humanity history. I detest anything is
    > Wiki and like Wiki, i consider a similar approach about the various
    > argument mistaken, useless and dangerous.


    Your poor English leaves me less than certain what the basis for your
    complaint against Wikipedia is; all I can say is that I've found that is
    has very broad coverage, has a lot of useful information, and is not a
    whole lot less reliable than the typical conventional encyclopedia. YMMV.

    > Some time ago i have read (i do not remember the item) a thing like the
    > following:
    >
    > int funtion(int var) {
    >
    > int arr[var];


    Note: this is a variable-length array (VLA). VLAs were first introduced
    in C99. They were made optional in C2011; __STDC_NO_VLA__ will be
    pre-#defined by any conforming implementation of C2011 that chooses not
    to support VLAs

    > ......
    >
    > }
    >
    > All this should be in the place of malloc function:
    >
    > int function(int var) {
    >
    > int *arr;
    >
    > arr = malloc(var * sizeof(int));


    better:
    int *arr = malloc(var * sizeof *arr);

    > if(arr != NULL) {
    > .......
    > } else {
    > puts("OUT OF MEMORY");
    > exit();
    > }
    >
    > arr = NULL;


    Since arr is going out of scope immediately after the call to free(),
    there's no need to null it. In a program where arr was going to continue
    being used after the free(), it should be nulled - but you should NOT
    null it until after you call free().

    > free(arr);


    That call is pointless, because free(NULL) does nothing. You've already
    erased the only pointer you had that pointed at the memory allocated by
    the call to malloc(), so now you can neither use that memory, nor
    free() it (at least, not until your program terminates). That's called a
    memory leak. You should have called free(arr) before nulling it.

    > }


    The second way of handling arr is more complicated than the first, but
    it achieves much the same goals. In general, I would favor the simpler
    approach, but there's three key reasons why it might be better to use
    the more complicated one:

    1. The first way doesn't work with implementations that don't support
    VLAs, either because they are C90 implementations, or because they are
    less than fully conforming implementations of C99, or because they are
    C2011 implementations that pre#define __STDC_NO_VLA__.

    2. On many systems, the VLA in the first version of function() will be
    allocated from a different memory pool than the one controlled by
    malloc() - that pool may be significantly smaller than the other, which
    can be a good reason to avoid using it more than you need to.

    3. If your system doesn't have enough memory to allocate space for the
    VLA, the behavior of the first version of function() is undefined. If
    malloc() can't find the requested amount of memory, the second version
    of function() has well-defined behavior that includes printing an error
    message, which is much safer.

    > I am not a very C expert, i have studied some things, read some item,
    > but i have never seen a thing like this and that my compilers,
    > obviously, refuse.


    The first version of function() will obviously be refused by any
    implementation of C that does not support VLAs, but it's not hard to
    find one which does; gcc -std=c99, for instance. A conforming
    implementation of C can't use the existence of a memory leak to reject a
    program, and I didn't notice any other reason for rejecting the second
    version of that code, but I might have missed something. Which version
    of the above code do your compilers refuse? Why do your compilers refuse
    to do so - what error message do they give?
     
    James Kuyper, Jun 13, 2013
    #1
    1. Advertising

  2. James Kuyper

    Siri Cruise Guest

    In article <>,
    James Kuyper <> wrote:

    > 2. On many systems, the VLA in the first version of function() will be
    > allocated from a different memory pool than the one controlled by
    > malloc() - that pool may be significantly smaller than the other, which
    > can be a good reason to avoid using it more than you need to.


    Not a problem on a modern general purpose machine with a real operating system.
    They can leave as much space for each thread's stack as the heap. On 64 bit VM
    you can have four million threads with four trillion bytes each. That should
    satisfy most programs for at a few years.

    It's only a concern for embedded systems, which is its own special set of
    problems, or people using brain dead systems.
    --
    Mommy is giving the world some kind of bird.
    :-<> Siri Seal of Disavowal #000-001. Disavowed. Denied. Deleted.
    NSA CIA bomb Constitution patriot terrorism freedom Snowden
     
    Siri Cruise, Jun 13, 2013
    #2
    1. Advertising

  3. Siri Cruise <> writes:
    > In article <>,
    > James Kuyper <> wrote:
    >
    >> 2. On many systems, the VLA in the first version of function() will be
    >> allocated from a different memory pool than the one controlled by
    >> malloc() - that pool may be significantly smaller than the other, which
    >> can be a good reason to avoid using it more than you need to.

    >
    > Not a problem on a modern general purpose machine with a real
    > operating system. They can leave as much space for each thread's
    > stack as the heap. On 64 bit VM you can have four million threads with
    > four trillion bytes each. That should satisfy most programs for at a
    > few years.
    >
    > It's only a concern for embedded systems, which is its own special set of
    > problems, or people using brain dead systems.


    Many systems, including the 64-bit desktop system on which I'm
    typing this, impose limits on stack size. For example, the command
    "ulimit -s" indicates that the stack size is limited to 8192 kbytes.
    The data segment size, which I believe is where heap allocations come
    from, is unlimited, as is the virtual memory size. ("Stack" and
    "heap" are implementation-specific terms referring, in this case,
    to the resources from which objects with automatic and allocated
    storage duration, respectively, are created. "Unlimited" means that
    the system doesn't impose specific limits, not that the resource
    is actually infinite.)

    I suspect the rationale behind this policy is that it causes
    infinite recursion bugs to kill offending programs fairly quickly
    rather than allowing them to consume significant resources needed
    by other concurrently executing programs. It's generally advisable
    to allocate very large chunks of memory via malloc() or equivalent
    rather than as objects with automatic storage duration, even though
    the latter might be more convenient.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Working, but not speaking, for JetHead Development, Inc.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jun 13, 2013
    #3
  4. On 13-Jun-13 10:11, James Kuyper wrote:
    > ... Wikipedia ... has very broad coverage, has a lot of useful
    > information, and is not a whole lot less reliable than the typical
    > conventional encyclopedia. YMMV.


    A study showed that, with the exception of current events*, Wikipedia
    has a similar error rate to conventional encyclopedias, eg. Britannica.

    * which, of course, conventional encyclopedias don't cover at all.

    S

    --
    Stephen Sprunk "God does not play dice." --Albert Einstein
    CCIE #3723 "God is an inveterate gambler, and He throws the
    K5SSS dice at every possible opportunity." --Stephen Hawking
     
    Stephen Sprunk, Jun 15, 2013
    #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. Siri Cruise

    Re: Wikipedia goes wrong: is this possible?

    Siri Cruise, Jun 13, 2013, in forum: C Programming
    Replies:
    4
    Views:
    198
    zebra9forC
    Jun 15, 2013
  2. Eric Sosman

    Re: Wikipedia goes wrong: is this possible?

    Eric Sosman, Jun 13, 2013, in forum: C Programming
    Replies:
    10
    Views:
    285
    Kenny McCormack
    Jun 19, 2013
  3. Noob
    Replies:
    0
    Views:
    193
  4. Stephen Sprunk

    Re: Wikipedia goes wrong: is this possible?

    Stephen Sprunk, Jun 13, 2013, in forum: C Programming
    Replies:
    9
    Views:
    179
    Tim Rentsch
    Jun 18, 2013
  5. Stephen Sprunk

    Re: Wikipedia goes wrong: is this possible?

    Stephen Sprunk, Jun 13, 2013, in forum: C Programming
    Replies:
    22
    Views:
    482
    Phil Carmody
    Jun 30, 2013
Loading...

Share This Page