malloc

C

cs

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 :)
 
C

cs

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;
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Malloc question 9
malloc 40
malloc and maximum size 56
malloc 11
Correct use of malloc 9
Weird malloc behaviour 12
Alternative to Malloc in C 0
malloc() size problems 15

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top