Malloc struct

codenstuff via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jul 6 11:15:55 PDT 2015


I need to create following struct using malloc

struct Map {
int **entries;
int rows;
int cols;
}
Map *map_create(int rows, int cols) {
   Map *that = cast(Map*)malloc(Map.sizeof);
   that.entries = cast(int**)malloc(rows * int.sizeof);
   foreach(row; 0..rows) {
     that.entries[row] = cast(int*)malloc(cols * int.sizeof);
   }
   that.rows = rows;
   that.cols = cols;
   return that;
}

I find that memory is collected when pointer is returned (from 
some of the entries). How do I prevent that? The code should 
behave as in C.


More information about the Digitalmars-d-learn mailing list