2

I'm pretty rust at my C still and I am just not figuring this out. What I am trying to do is to implement my own malloc so I can keep track of allocations and debug missing calls to free(). I have a header like this:

typedef struct MemoryInfo {
  mem_Kind kind;
  unsigned int id;
  struct MemoryInfo* prev;
  struct MemoryInfo* next;
} MemoryInfo;

And my custom malloc looks something like this:

void* my_malloc(mem_Kind kind, unsigned int size) {
  MemoryInfo* mem;

  allocCount++;
  mem = (MemoryInfo*)malloc(sizeof(MemoryInfo) + size);
  mem->id = id;
  mem->kind = kind;
  // set prev/next...

  return mem + sizeof(MemoryInfo); // return pointer to memory after header
}

But I'm clearly getting my pointer arithmetic wrong because it blows up pretty horribly very quickly. However if I add a void* memory to the end of my struct and do another malloc then it seems to do fine, the problem with that is that I can't really find the header in my_free if I do that. I'm trying to basically prepend the header so I can do some reverse pointer arithmetic to get the header in free.

void my_free(void* memory) {
  MemoryInfo* mem = memory - sizeof(MemoryInfo); // not correct either
  allocCount--;
  free(mem);
}

What am I doing wrong here?

3 Answers 3

1

I think you have a problem with adding to pointer. It has to be like this:

return (char*)mem + sizeof(MemoryInfo); // return pointer to memory after header

and

void my_free(void* memory) {
  MemoryInfo* mem = (MemoryInfo*)((char*)memory - sizeof(MemoryInfo)); // not correct either
  allocCount--;
  free(mem);
}

By the way. Look at this program.

#include <stdio.h>

typedef struct MemoryInfo {
  int kind;
  unsigned int id;
  struct MemoryInfo* prev;
  struct MemoryInfo* next;
} MemoryInfo;



int main()
{
  MemoryInfo* ptr = 0;

  printf("sizeof: %d\n",sizeof(MemoryInfo));
  printf("%d\n",ptr+3);
  return 0;
}

I have added 3 to pointer to MemoryInfo, but its value become 3*sizeof(MemoryInfo).

1
  • Interesting. Works like a charm! Can you explain why void* doesn't work and I have to cast it to char*? May 12, 2012 at 17:03
0

Your pointer arithmetic is wrong. in ptr+1 the +1 already uses the correct increment (sizeof *ptr). You would have to increment by sizeof *ptr if ptr were a char pointer, but it is not.

void *my_malloc(mem_Kind kind, unsigned int size) {
  MemoryInfo  mem;

  allocCount++;
  mem = malloc(sizeof *mem + size);
  mem->id = id;
  mem->kind = kind;
  // set prev/next...

  return mem + 1; // return pointer to memory after header
}


void my_free(void *memory) {
  MemoryInfo *mem = memory; // not correct either
  mem -= 1;
  allocCount--;
  free(mem);
}

Also, please take into consideration that malloc() and friends should return a pointer that is suitable to every object, and has to be aligned to the natural boundary for any object. (this could be 32 bits, 64 bits, or anything that your platform dictates) Your sizes could fail for sizeof (int)==2 and sizeof (void*) == 8 , for instance. (but which seems a very rare case, luckily)

0

good practice is define like this:

struct some_struct {
  size_t data_size;
  struct some_struct  *next, *prev;
  void * struct_data[];
}

and us it like that:

struct some_struct *get_some_struct ( void *buf,size_t buflen,struct some_struct **next){
  *next=0;
  struct some_struct *s=(struct some_struct *)buf;
  if ( s && buflen < sizof(*s) + s->data_size )
    *next = (char*)s + s->data_size;
    return s;
  else
    return 0;
}

So, if you need to iterate through structures, you can get properly the offset of next structure in buffer. I just want you to get the approach. Will be pleased if you'll get it

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.