Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Malloc

Status
Not open for further replies.

Cbug12

Programmer
Dec 13, 2005
3
GB
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
 
I personally don't understand what you're trying to do, but it's difficult to allocate memory without a single call to malloc.

Cheers,
Dian
 
Im trying to create an array but refrence it as an linked list to keep track of free regions of memory in the array.
The mymalloc function is meant to find unalloacted memory (indicated by a 0) and return its address.

malloc then loops through the array return the addresses of the array that are free.

hope that clears things up
 
Still don't fully understand but at first sight:

1.- You're not initializing i
2.- If the first if is false you just update i and exit
3.- I think you need a loop there (for, while)

What is this for? Maybe an assignment?

Cheers,
Dian
 
#include <stdio.h>
#include <stdlib.h>
#define SIZE (1000)
#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 counter = 0;
static int arraysize;
static count = 0;
int myfree(int * array, int * block);
int * mymalloc(int * array, int size);
int mydispose(int * array);
struct test {
int prev;
int next;
int size;


} ;
main(){

(void)myinit(block, SIZE);
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);
}
} else {
/* free a block */
int fp = rand()%valid_pointers;
(void)myfree(block, pointers[fp]);
while (++fp < valid_pointers)
pointers[fp-1] = pointers[fp];
valid_pointers--;
}
}
(void)mydispose(block);
}
int myinit(int * array, int size){

int i = 0;
// printf("size is = %d\n",size);
// if(size < 127)

// return !0;


struct test * tmp = (struct block *)(array);

array[0] = 0;
array[1] = -1;
array[2] = size;



return 0;
}

int * mymalloc(int * array, int size)
{ /* needs changing */int i;
//printf(" SIZE = %d\n" ,SIZE);

int p;
int np;
int s;
np = -1;
i= 0;

if((counter + size - 4) < SIZE){

while((counter + size - 4) < SIZE){
struct test * tmp = (struct block *)(array);
// printf("this is the array: = %d\n" ,tmp->);
if(tmp -> next == -1){
if(i == 1){
i= 0;

return &array[counter]; }
else if(i ==0)
array[counter +tmp->size ] = 1; // + 3 because each block stores size, next and prev, 1 signals in use
array[counter + tmp->size ] = -1;
array[counter + tmp->size ] = 0;
i= 1;


}
counter += 3;//increment counter
}
// printf("contents at = %d\n" ,counter);
// printf("contents of array = %d\n" ,array

return (int *)0;
}


}

Explaination
Yes this is an assignment thats why I don't want a solution just tips on how to make it more effiecient.


Main() this is just a test harness
myinit(...) initalises the array
mymalloc(...) allocates free region from the array passed
returns the address of the free unallocated region.

This seems to works however i feel the implementation could be improved significantly just not sure how to go about.

i hope that clears it up a bit!
 
Post your code again and please use the [tt][ignore]
Code:
[/ignore][/tt]
tags when posting code.

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top