malloc

Discussion in 'C Programming' started by cs, Nov 7, 2005.

  1. cs

    cs Guest

    in chapter 8.7 of K&R2(an example of memory allocator)
    if i write NALLOC==1024 (is it 1K?) in my pc (that has 40MegaBytes of
    memory) and i ask memory for many arrays of more than 1K there is the
    problem that Pc reads the disk (seems doesn't have memory).
    instead if i write
    NALLOC==1024*1024*2 (is it 2.048 MegaBytes?)
    all seems right.
    Where is the error?
    I have an error in that malloc function?
    I have an error in the function that give memory to malloc function?
    I have an error in the function that call malloc function?
    NALLOC is not right. What value is right for NALLOC macro value?
    Thank you if you can read in the crystal ball :)
    cs, Nov 7, 2005
    #1
    1. Advertising

  2. cs

    pete Guest

    pete, Nov 7, 2005
    #2
    1. Advertising

  3. cs

    cs Guest

    On Mon, 07 Nov 2005 13:04:42 +0100, cs <> wrote:
    >in chapter 8.7 of K&R2(an example of memory allocator)
    >if i write NALLOC==1024 (is it 1K?) in my pc (that has 40MegaBytes of
    >memory) and i ask memory for many arrays of more than 1K there is the
    >problem that Pc reads the disk (seems doesn't have memory).
    >instead if i write
    >NALLOC==1024*1024*2 (is it 2.048 MegaBytes?)
    >all seems right.
    >Where is the error?
    >I have an error in that malloc function?
    >I have an error in the function that give memory to malloc function?
    >I have an error in the function that call malloc function?
    >NALLOC is not right. What value is right for NALLOC macro value?
    >Thank you if you can read in the crystal ball :)


    the error seems in the function morecore() in
    "if(list_i_ <= list_m_ )"
    should be
    "if(list_i_ >= list_m_ )"
    program is difficult?
    Don't know if it is right...

    /* morecore: chiede al sistema memoria aggiuntiva */
    static Header* morecore(unsigned nu)
    {char *cp, **p;
    Header *up;
    unsigned len;
    /*-----------------------*/
    if(nu<NALLOC) nu=NALLOC;
    cp = (char*) malloc(nu * sizeof(Header));
    if( cp==0 ) /* non c'e' piu' spazio */
    return NULL;


    if(list_i_ <= list_m_ )
    {len = (list_m_==0 ? 128: 2*list_m_);
    p = (char**) realloc(list_, len * sizeof *p);
    if(p==0)
    {free(cp); return 0;}
    list_ = p;
    list_m_ =len;
    }
    list_[list_i_++]=cp;

    up=(Header*) cp;
    up->s.size=nu;
    inserisci((void*)(up+1), 1, 0);
    return freep;
    }
    cs, Nov 8, 2005
    #3
    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. John
    Replies:
    13
    Views:
    687
  2. ravi
    Replies:
    0
    Views:
    440
  3. Peter
    Replies:
    34
    Views:
    1,918
    Richard Tobin
    Oct 22, 2004
  4. porting non-malloc code to malloc

    , Feb 18, 2005, in forum: C Programming
    Replies:
    3
    Views:
    470
    Walter Roberson
    Feb 19, 2005
  5. Johs32

    to malloc or not to malloc??

    Johs32, Mar 30, 2006, in forum: C Programming
    Replies:
    4
    Views:
    314
    Captain Winston
    Mar 30, 2006
Loading...

Share This Page