1

I'm stuck, maybe on a very simple question.

In university we have to make our own malloc-function in C. I only have a problem when saving the pointer Address on the dereferenced pointer. Im working on heap and there is enough memory left.

void *actual_pointer = sbrk(sizeof(Node));
*(char*)actual_pointer = 'O';
actual_pointer = actual_pointer+sizeof(char);
*(char*)actual_pointer = 'K';
actual_pointer = actual_pointer+sizeof(unsigned int);
*(unsigned int*)actual_pointer = size;
actual_pointer = actual_pointer+sizeof(unsigned int);
*(unsigned int*)actual_pointer = 0;
actual_pointer = actual_pointer+sizeof(unsigned int);
*actual_pointer = actual_pointer;

The last line doesn't work. I tried everything. Isn't it possible to store some pointer Address to the dereferenced pointer?

typedef struct _Node_ 
{
  char checkCorruption_[2];
  unsigned int size_;
  unsigned int status_;
  char *location_;
  struct Node *next_;
  struct Node *prev_;
} Node;

This is the structure of my double-linked list representing the momory structure.

My Idea was the following: We need to make a simple mallocfunction. From main function for example data[1] = malloc(100 * sizeof(int)) is called. Then I will create in the mallocfunction one Node and store the "checkCorruption"-Value 'OK' in it. After it the size, in my example "100 * sizeof(int)". After this I store a 0 for used or a 1 for free in it. Then I will store the location which is returned to data[0] - the storage gets reserved with sbrk(100*sizeof(int)) and begins at the location. Then i will store the Pointer to the next Node and the previous.

I always check the OK-value if some other malloc had an overflow and overwrited it - then i will exit with an error.

Is my Idea totally bullshit or is it ok?

Edit2:

When I will use now Node instead of void I can store also my location pointer to the node.

Node *actual_pointer = sbrk(sizeof(Node));


actual_pointer->checkCorruption_[1] = 'O';
printf("actual_pointer: %p\n", actual_pointer);
printf("actual_O: %c\n", actual_pointer->checkCorruption_[1]);
printf("actual_pointer_before: %p\n", actual_pointer);
actual_pointer = actual_pointer+sizeof(char);
printf("actual_pointer_after: %p\n", actual_pointer);

Output:

actual_pointer: 0x1ad4000
actual_O: O
actual_pointer_before: 0x1ad4000
actual_pointer_after: 0x1ad4028

But now I have some problems with actual_pointer = actual_pointer+sizeof(char);. This command should add the size of char to the actual_pointer but it increases the pointer with 40 bytes? I don't understand this?

Thanks in Advance,

Philipp

6
  • 2
    Even after you fix those trivial compilation errors, you will run into some hard runtime errors, as you will be attempting to store into unaligned memory address. I suggest that you explain exactly what you want to write where ("OK", size, address), as well as what is the Node structure. Then, someone here might point out exactly how it can be done safely. Commented Dec 14, 2014 at 12:37
  • I changed the informations and now I wrote how I would solve this malloc function. Commented Dec 14, 2014 at 12:53
  • 1
    So just use Node *actual_pointer instead of void *actual_pointer, and set the fields as you would normally do for any structure (for example, actual_pointer->size_ = size;). This should take care of both the compilation errors that you are experiencing and the runtime errors that you are likely to experience. Commented Dec 14, 2014 at 12:58
  • Thanks! Now I have some other problem or misunderstanding (Edit2). I thought I know C :P Commented Dec 14, 2014 at 13:47
  • 1
    With Type* x, the operation x = x+5 is equivalent to incrementing the pointer by 5*sizeof(Type), and not by 5. You can increment actual_pointer by 1 if you really want, but it would be very wrong (you'll most likely get runtime errors after changing fields in it). Please explain your purpose in performing this operation. Commented Dec 14, 2014 at 13:56

1 Answer 1

1

It is impossible to store value into a void...

Try replacing the last line with

*(unsigned int*)actual_pointer = (unsigned int*)actual_pointer
Sign up to request clarification or add additional context in comments.

2 Comments

OP is not "storing value into a void" (he or she are casting properly). The compilation errors are in fact due to the attempt to increment the void pointer. And in any case, those compilation errors are the least problem in this case.
OP is not "storing value into a void" (he or she are casting properly). - This statement is obviously false. The original post has the definition void *actual_pointer and the assignment *actual_pointer = actual_pointer, so the type of the left hand side is void.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.