-1

I wrote the post that this was closed for saying it was a duplicate question. It is not a duplicate question. While I really appreciate the time and effort responders have made their answers often don't explain what's going on. Please vote to reopen.

consider the following:

typedef struct node
{
    int number;
    struct node *left;
    struct node *right;
} node;



node  *test1 = malloc(511 * sizeof(node));
node  (*test2)[1] = malloc(511 * sizeof(node));

test1 is a pointer but we can reference test1[510], and test2 is a pointer to an array that contains 1 element but we can reference test2[510][0]

so in the first case I'm basically creating a 1 dimensional array and in the second a two dimensional array.

am I correct and is it correct to think of malloc in both of these cases is implicitly defining the number of elements?

DCR
  • 14,737
  • 12
  • 52
  • 115

2 Answers2

1

The number of the elements in the array you define here: node (*test2)[1]

node  (*test2)[1] = malloc(511 * sizeof(node));

Firstly you should not use types only objects in malloc

node  (*test2)[1] = malloc(511 * sizeof(*test2));

This line allocates N bytes of memory where N is 511 * (number of bytes occupied by the single element array of type node) and assigns it to the pointer test2. test2 is not an array and it does not have nay elements you may define/declare

so in the first case I'm basically creating a 1 dimensional array and in the second a two dimensional array.

To "create" the two dimensional array you need to node arr2d[x][y] and for single dimensional node arr1d[x]. Arrays are not pointers and pointers are not arrays

BTW what is the point of writing answers if you ignore them?

0___________
  • 60,014
  • 4
  • 34
  • 74
  • I'm not ignoring the answers I just don't understand them. I think of pointers as an address and an array as a block of memory is that not correct? AND when I do node (*test2)[1] = malloc(511 * sizeof(*test2)); I can access this as a two dimensional array. test2[200][0].number = 17; the first node->number in the 200th array – DCR Sep 02 '20 at 00:34
  • consider; node test[511][1]; here I can access test[200][0].number = 17; – DCR Sep 02 '20 at 00:50
0

It's implicitly defined as in as the programmer you need to understand what limits there are when using that allocated structure. malloc itself doesn't concern itself with anything other than the size you've requested.

There's also calloc which you can specify a number of elements and an element size to make your allocation code more easily understood and verifiable, but it too doesn't care what you do with the memory once allocated.

You can assign the result of malloc to whatever you want, but you should assign it to a pointer type that makes sense.

In C you can do a lot of things that don't make any sense, that are just undefined behaviour, so don't think that because it compiles it's valid code.

Remember that C does not do any bounds checking at all. This is entirely the responsibility of the programmer.

tadman
  • 208,517
  • 23
  • 234
  • 262