0

Having trouble with my task here. I need to create a global block of free memory and malloc it. Having some trouble initialising it due to typecast and handling errors.

Like arrays in C where the first array is actually a pointer to the first element, my memory block needs to be similar where i can use pointer arithmetic to locate blocks of memory.

//global variable
static byte *memory = NULL;

void allocator_init(u_int32_t size){

     *memory = (byte*) malloc(size);
}

The addresses/pointers to these memory addresses will be stored via structs/links as headers of the memory block.

typedef struct _header {
    int signiture;
    int size;
    header* next;
    header* prev;
} header;
ryantata
  • 137
  • 1
  • 3
  • 12
  • If you have included the header `` the cast is redundant; if you haven't the cast hides a nasty bug ... or you are better off without the cast whether you included the proper header :) – pmg Aug 17 '12 at 13:35
  • I did, its included at the top – ryantata Aug 17 '12 at 13:50

4 Answers4

5

Drop the *:

*memory = (byte*) malloc(size);
^

You might also want to drop the cast but that's your call.

cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • "You might also want to drop the cast but that's your call." - if it's C++, then not. If it's C, then also not: http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc –  Aug 17 '12 at 13:34
  • @H2CO3 I once read a post by an illustrious SO user who said there are pros and cons to casting the result of `malloc` in C. – cnicutar Aug 17 '12 at 13:35
  • @H2CO3 Definitely not. I can't remember who it was or I would have named him/her. – cnicutar Aug 17 '12 at 13:37
  • then I don't argue :) (Seriously, it only makes code unreadable and error prone in case you forgot to include stdlib.h...) –  Aug 17 '12 at 13:39
  • 1
    @H2CO3 I don't cast either but I also don't directly advise other people to do the same. – cnicutar Aug 17 '12 at 13:42
  • 1
    If you "need" the pointer casting, probably you are not compiling C (but C++) – pmg Aug 17 '12 at 13:54
1

You need to assign the return value of malloc to the pointer itself, not to the byte pointed to by the pointer. (Anyway, if you dereference the initially NULL pointer, your program will segfault.)

memory = malloc(size);

Also, don't caat the return value of malloc.

1

*memory = (byte*) malloc(size); - This statement means you are trying to assign the address of the heap memory block as a value to *memory. Here memory is having NULL, so it will crash.

You have to assign the addres to the variable like, memory = (byte*) malloc(size);

rashok
  • 12,790
  • 16
  • 88
  • 100
  • so will global static take care of assigning variable memory to be used as a pointer?? – ryantata Aug 17 '12 at 13:47
  • No. you have to assign the addess return by `malloc` only to the variable `memory` even if it is local variable also. – rashok Aug 17 '12 at 13:55
0

In your header struct, since the struct references itself, declare the internal header pointers as "struct _header * name;". I think other people have hit all the other points :)

Neil
  • 466
  • 1
  • 6
  • 15