I need some help with a malloc function i have to write, I don't want a solution just tips on how to find a solution.
#include <stdio.h>
#include <stdlib.h>
#define SIZE (1000000)
#define PSIZE (SIZE/50)
/* You are not told this value */
static int block[SIZE];
static int * pointers[PSIZE];
static int valid_pointers=0;
static int arraysize;
static count = 0;
int* mymalloc(int * array, int size);
int initial(int * array, int size);
struct test {
int prev;
int next;
int size;
} ;
main(){
(void)myinit(block, SIZE);
int i;
while(1) {
if (valid_pointers==0 || ((valid_pointers < PSIZE) && ((rand()&7) <5))) {
/* Allocate a new block */
int size = rand() % (1<<(rand()%12));
int * fp = mymalloc(block, size);
if (fp) {
pointers[valid_pointers++] = fp;
count++;
} else {
printf("Successful mymallocs = %d\n",count);
system("PAUSE");
exit(0);
}
}
}
}
/* It will be passed a pointer to the start of an int array of extent size.initial will return 0 if the array is too small to use; otherwise it will return (!0). It must not return 0 if passed an array of extent greater than 127.*/
int initial(int * array, int size){
if(size < 127){
struct test * tmp = (struct block *)(array);
array[0] = 0; //prev
array[1] = -1;//next
array[2] = 0;//size
return 0;
}
return !0;
}
//return !0;
}
/*returns a pointer to the start of an int array drawn from an unallocated free region of array, which will previously have been initialised by initial() int *
*/
mymalloc(int * array, int size)
{ /* needs changing */
int counter;
int i;
if(( i + size) -3 < SIZE){
struct test * tmp = (struct block *)(array);
if(tmp -> next == -1){
array[0 + i] = size;
array[1 + (i +1)] = -1;
array[2 + (i + 1)] = 0;
}
return &array;
}
else{ i = i + 3 ;
}
return (int *)0;
}
i dont achieve any mallocs for some reason anyone got any ideas
#include <stdio.h>
#include <stdlib.h>
#define SIZE (1000000)
#define PSIZE (SIZE/50)
/* You are not told this value */
static int block[SIZE];
static int * pointers[PSIZE];
static int valid_pointers=0;
static int arraysize;
static count = 0;
int* mymalloc(int * array, int size);
int initial(int * array, int size);
struct test {
int prev;
int next;
int size;
} ;
main(){
(void)myinit(block, SIZE);
int i;
while(1) {
if (valid_pointers==0 || ((valid_pointers < PSIZE) && ((rand()&7) <5))) {
/* Allocate a new block */
int size = rand() % (1<<(rand()%12));
int * fp = mymalloc(block, size);
if (fp) {
pointers[valid_pointers++] = fp;
count++;
} else {
printf("Successful mymallocs = %d\n",count);
system("PAUSE");
exit(0);
}
}
}
}
/* It will be passed a pointer to the start of an int array of extent size.initial will return 0 if the array is too small to use; otherwise it will return (!0). It must not return 0 if passed an array of extent greater than 127.*/
int initial(int * array, int size){
if(size < 127){
struct test * tmp = (struct block *)(array);
array[0] = 0; //prev
array[1] = -1;//next
array[2] = 0;//size
return 0;
}
return !0;
}
//return !0;
}
/*returns a pointer to the start of an int array drawn from an unallocated free region of array, which will previously have been initialised by initial() int *
*/
mymalloc(int * array, int size)
{ /* needs changing */
int counter;
int i;
if(( i + size) -3 < SIZE){
struct test * tmp = (struct block *)(array);
if(tmp -> next == -1){
array[0 + i] = size;
array[1 + (i +1)] = -1;
array[2 + (i + 1)] = 0;
}
return &array;
}
else{ i = i + 3 ;
}
return (int *)0;
}
i dont achieve any mallocs for some reason anyone got any ideas