0

If I take the 2 following snipped codes below (which are equivalent) :

double ***x;
x = malloc(N * sizeof(*x));
for (i = 0; i < size_y; i++) {
   x[i] = malloc(N * sizeof(**x));

and

double ***x;
x = malloc(N * sizeof(double*));
for (i = 0; i < size_y; i++) {
   x[i] = malloc(N * sizeof(double**));

Does the syntax for the first one above should be rather :

double ***x;
x = malloc(N * sizeof(*x));
for (i = 0; i < size_y; i++) {
   x[i] = malloc(N * sizeof(*x));

I mean, for x, x[i] and x[i][j] malloc allocation, syntax will stay "sizeof(*x)", is this right ?

??

and does the second one should be :

double ***x;
x = malloc(N * sizeof(double**));
for (i = 0; i < size_y; i++) {
   x[i] = malloc(N * sizeof(double*));

??

I saw this syntax from this link

It is safer becuse you don't have to mention the type name twice and don't have to build the proper spelling for "dereferenced" version of the type. For example, you don't have to "count the stars" in

int *****p = malloc(100 * sizeof *p);

Compare that to the type-based sizeof in

int *****p = malloc(100 * sizeof(int ****));

where you have too make sure you used the right number of * under sizeof.

regards

UPDATE 1 :

I have got in one answer below the syntax as a pattern :

p = malloc(N * sizeof *p);

Does spaces (between N, sizeof and *p) and the abscence of parenthsesis for sizeof *p (instead of sizeof(*p)) have to be strictly applied for syntax of this allocation ?

In my code, I did :

 /* Arrays */
  double**  x;
  double**  x0;

/* Allocation of 2D arrays */
  x =  malloc(size_tot_y*sizeof(*x));
  x0 =  malloc(size_tot_y*sizeof(*x0));

  for(i=0;i<=size_tot_y-1;i++)
  {
    x[i] = malloc(size_tot_x*sizeof(**x));
    x0[i] = malloc(size_tot_x*sizeof(**x0));
  }

As you can see, I used parenthesis for sizeof and didn't use spaces between size_tot_*, sizeof and *p.

Does my syntax change anything to respect the pattern mentioned above ?

It may be a silly question but I would like to get a confirmation.

Thanks

Community
  • 1
  • 1
  • 3
    The first snippet is correct, the second snippet is not, and the third snippet is not. The sizes of `double*` and `double**` are not guaranteed to be the same in C (even though they usually are the same) and it’s confusing to use the wrong type in `sizeof` anyway. – Ry- Feb 01 '17 at 04:27
  • 1
    The argument about not having to count stars doesn’t hold much weight, either, because if you’re going beyond `**` too often you’re probably doing something wrong. – Ry- Feb 01 '17 at 04:27
  • Also `i < size_y;` --> `i < N;` or `x = malloc(N * sizeof(*x));` --> `x = malloc(size_y * sizeof(*x));` – BLUEPIXY Feb 01 '17 at 04:32
  • ok thanks. and what about the right syntax for the second snippet ? –  Feb 01 '17 at 04:33
  • @Ryan, so the fourth sinppet is right compared to the second one ? –  Feb 01 '17 at 04:43
  • 1
    @youpilat13: Yes. – Ry- Feb 01 '17 at 04:52

1 Answers1

1

The pattern is:

p = malloc(N * sizeof *p);

Replace p with the same thing in both cases. For example:

x[i] = malloc(N * sizeof *x[i]);

Sometimes people abbreviate *x[i] to **x using the knowledge that **x means *x[0], and x[0] and x[i] have the same size because all elements of an array must have the same type and therefore the same size.

But if you are not confident about all this then you can stick to the basic pattern and use *x[i].

M.M
  • 138,810
  • 21
  • 208
  • 365