0

Can someone help me understand this kind of allocation

im more familiar with something like this:

result = array = malloc(sizeof(int *) * height); for (i = 0; i < height; i++) { array[i] = malloc(sizeof(int) * width); }

the other allocation is this:

  int len;
  len = (*numObjs) * (*numCoords);

  objects = (double **)malloc((*numObjs) * sizeof(double *));

  objects[0] = (double *)malloc(len * sizeof(double));      

  for (i = 1; i < (*numObjs); i++)
    objects[i] = objects[i - 1] + (*numCoords);

the values (*numObjs) && (*numCoords) are taken from a file read which is 100000*20 .

frcake
  • 229
  • 1
  • 3
  • 18
  • ah dammit you are right i miss pasted the link , ill change that – frcake Jan 30 '17 at 22:26
  • i changed it , as you stated correctly , it's the same as the second allocation of the link http://c-faq.com/aryptr/dynmuldimary.html , but i really don't get : "You can keep the array's contents contiguous, at the cost of making later reallocation of individual rows more difficult, with a bit of explicit pointer arithmetic" – frcake Jan 30 '17 at 22:30
  • Either allocate an array of pointers for a ragged array, or a block width * height * sizeof(element) for a contiguous array. The latter is what you want most often. – Malcolm McLean Jan 30 '17 at 22:38
  • @MalcolmMcLean i do understand the result , but i don't understand the whole mechanism behind it , i mean the 1st allocation i wrote is pretty straight forward , but this.. – frcake Jan 30 '17 at 22:40
  • @frcake Just stick with first allocation. Much more straightforward. – RoadRunner Jan 30 '17 at 23:13
  • @RoadRunner i agree, but it's not contiguous :S , i know other contiguous allocation techniques but never seen this one... – frcake Jan 30 '17 at 23:20
  • @frcake Have you read [this](http://stackoverflow.com/questions/13534966/how-to-dynamically-allocate-a-contiguous-block-of-memory-for-a-2d-array)? – RoadRunner Jan 31 '17 at 01:27

0 Answers0