1

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 .

7
  • ah dammit you are right i miss pasted the link , ill change that Commented Jan 30, 2017 at 22:26
  • i changed it , as you stated correctly , it's the same as the second allocation of the link 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" Commented Jan 30, 2017 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. Commented Jan 30, 2017 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.. Commented Jan 30, 2017 at 22:40
  • @frcake Just stick with first allocation. Much more straightforward. Commented Jan 30, 2017 at 23:13

0

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.