I have this assignment to write my own malloc program and I am having some trouble. I am using a doubly linked list to manage free space and used space, and using next fit to allocate the memory. the problem that i am writing about is when i return the pointer to the program, it returns the wrong value here is the relevant code for this problem i think

if(found==0){
                                printf("INLOOP5 update\n");
                                newnode=(struct memory *)sbrk(sizemal+sizeof(struct memory));
                                printf("NEWNODE %X: \n", newnode);
                                newnode->next=NULL;
                                newnode->previous=tail;
                                tail=newnode;
                                tail->size=sizemal;
                                lastseen=head;
                                printf("NEW NODE %X: \n", newnode);
                                printf("TAIL: %X\n", tail);
                        }
                                
                }
void *returnsize=lastseen+sizeof(struct memory);
printf("LAST SEEN: %X  SIZEOFDLL: %X RETURNVAL: %X\n", lastseen, sizeof(struct memory), returnsize);
                                 
                                
printf("TAIL %X LASTSEEN %X\n", tail, lastseen);
printf("RETURN: %x: \n", lastseen+sizeof(struct memory));   
return (lastseen+sizeof(struct memory));
}

ALL VALUES BELOW ARE IN HEX

the number returned should be 502048 but it is returning 502240

502030 is the start of the tail and the size of the structure is 18
so the structure starts at 502030 and ends at 502047 so 502048 should be returned.... Any ideas as how to fix this problem?

USE CODE TAGS!!!

Having said that, is it possible you mean to use brk and not sbrk?

sorry, Idk what you mean by code tags, comments??? but i need to use sbrk because i am writing my own malloc, my problem is that i am trying to add 18 to a pointer which i know returns 502048h, but when adding the the 2 it returns 502240

See this for code tags.
If you feel you must use sbrk, then this line looks fishy to me: newnode=(struct memory *)sbrk(sizemal+sizeof(struct memory)); Should that possibly be: newnode=(struct memory *)sbrk(sizeof(struct memory)); It depends on what sizemal is. If it's the current size of the data segment, then you certainly don't want to pass that into sbrk since sbrk increments the size of the data segment by its parameter (whereas brk sets it to its parameter).

sbrk is working fine, this issue is i just want to increment the pointer by 18h. can you just add an int to a pointer and have it increment the address it is pointing to by 18h?

What does sizemal hold?
Is 18h the size of a struct memory ?

sizemal is the length i am recieving from the driver program telling how much space to allocate in memory, 18h is the size of the structure. neither is important, i just need to know why when i add 18 to the pointer at address 502048 it sends me to 502240. I also tried lastseen=lastseen+24(18h); and the same result occured

I've been on a wild goose chase! Sorry about that.

This just looks like pointer arithmetic. When you add a number to a pointer it increments the pointer by that number times the size of the thing pointed to. So you probably just want to add 1 to lastseen.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.