Mail Archives: djgpp/2000/09/18/06:30:21
Incoerent results are given deppending on how memory is malloc or
realloc:
a)Using a straight foward malloc
b)Trying to malloc in 1MB step
c)Trying to realloc in 1 to 10 MB steps.
Using the programs listen at the bottom, i came the the following
results, using a computer with 128 MB ram and win98 1st edition:
Step Success Fail
a) 124 125
b) 95 96
c) 1 35 36
2 42 44
3 45 48
4 48 52
5 45 50
6 54 60
7 63 70
8 48 56
9 54 63
10 60 70
What on earth this means...???
Can someone help...?
Thanks.
//--------------------------------------------------------------------
// a) Straight malloc
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
void *ptr;
int step;
step=124; //change this value; recompile to find max. available
ptr = malloc(1048576*step);
if (!ptr) {
printf("Fail to allocate %d MB of memory!\n", step);
return 0;
}
else {
printf("Successfuly allocated %d MB\n", step);
free(ptr);
}
return 0;
}
//---------------------------------------------------------------------
// b) Trying to malloc in 1MB step
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
void *ptr;
int step, i;
step=1; //in 1MB step
for(i=1;i<257;i++) {
ptr = malloc(1048576*step*i);
if (!ptr) {
printf("Fail to malloc %d MB of memory!\n", step*i);
return 0;
}
else {
printf("Successfuly malloc %d MB\n", step*i);
free(ptr);
}
}
return 0;
}
//---------------------------------------------------------------------
// c)Trying to realloc in 1 to 10 MB step.
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
void *ptr;
int step, i;
ptr = malloc(1048576*1);
if (!ptr) {
printf("Fail to malloc 1 MB of memory!\n");
return 0;
}
else {
printf("Successfuly malloc 1 MB\n");
}
step=1; //change the setp from 1 to 10, and recompile
for(i=1;i<257;i++) {
ptr = realloc(ptr, 1048576*step*i);
if (!ptr) {
printf("Fail to realloc %d MB of memory!\n", step*i);
return 0;
}
else {
printf("Successfuly realloc %d MB\n", step*i);
}
}
return 0;
}
- Raw text -