malloc

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cs

    #1

    malloc

    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*10 24*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 :)
  • pete

    #2
    Re: malloc

    cs wrote:[color=blue]
    >
    > in chapter 8.7 of K&R2(an example of memory allocator)[/color]

    sbrk() means that's UNIX code, thus offtopic here.



    news:comp.unix. programmer

    --
    pete

    Comment

    • cs

      #3
      Re: malloc

      On Mon, 07 Nov 2005 13:04:42 +0100, cs <n@ss.g> wrote:[color=blue]
      >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*1 024*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 :)[/color]

      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(unsign ed 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;
      }

      Comment

      Working...