0

I'm trying to make a dynamic struct array using malloc.

I have a struct: 
typedef struct{
    char question[200];
    char answer1[30];
    char answer2[30];
    char answer3[30];
    char correctAnswer[2];
}Questions;

And the following code:

int length = 10;
Questions * qArray = malloc(length*sizeof(*qArray));

For some weird reason the sizeof(qArray) returns 4 even if I change length. How do I actually set the correct size of the array?

Haris
  • 12,120
  • 6
  • 43
  • 70
Jullix993
  • 105
  • 10

5 Answers5

3

The allocated size is in no way associated with the pointer to the first element.

In other words you cannot do that, sizeof(qArray) is statically computable from the type.

You need to track the allocated size at run-time separately yourself.

unwind
  • 391,730
  • 64
  • 469
  • 606
3

With these lines you are allocating memory for an array of 10 Questions

int length = 10;
Questions * qArray = malloc(length*sizeof(*qArray));

You could achieve the same result with :

Questions * qArray = malloc(length*sizeof(Questions));

The former syntax makes it easier to change the type in the future and is preferred by a lot of people.

After that :

sizeof(qArray[0]) // size of the first element of the array in bytes (292 probably)
sizeof(*qArray)   // size of the first element of the array in bytes (292 probably)
sizeof(Questions) // size of a struct of type Questions in bytes (292 probably)
sizeof(qArray)    // size of a pointer in your system in bytes (4 or 8 bytes usually)
length            // number of elements in qArray
length*sizeof(*qArray) // total size of the array in bytes
Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82
0

You are setting the size of the array correctly only. The problem is the way you are using to get back the size.


For some weird reason the sizeof(qArray) returns 4 even if I change length.

Because qArray is a pointer. So sizeof(qArray) is the size of a pointer, that is size of your address, which is 4 bytes in your case.


Doing sizeof(*qArray) will actually give you the size of the structure Questions, since *qArray is pointing to a structure Questions.


How do I actually set the correct size of the array?

To get the full size of the array, you need to keep length handy everytime. Since you are using pointers. Just like unwind said.

Haris
  • 12,120
  • 6
  • 43
  • 70
-1

You can't get the size of the array you allocated from the pointer. You already know the size since you had to compute it to call malloc():

Questions * qArray = malloc(length*sizeof(*qArray));

If you need to retain the size of the array:

size_t qArraySize = length * sizeof( *qArray );
qArray = malloc( qArraySize );
Andrew Henle
  • 32,625
  • 3
  • 24
  • 56
  • sizeof *qArray should be Questions – Robert Jacobs Dec 03 '15 at 16:11
  • @RobertJacobs The question *after* the code blocks is "For some weird reason the sizeof(qArray) returns 4 even if I change length. How do I actually set the correct size of the array?" Please read. – Andrew Henle Dec 03 '15 at 16:12
  • 1
    @RobertJacobs - *sizeof *qArray should be Questions* And no, it should **not** be. Because if someone were to change the definition of qArray, to something like `struct NewQuestions`, then one would also have to remember to change the size calculation code when there is no direct connection between the two other than the pointer used to hold the address of the allocated block. – Andrew Henle Dec 03 '15 at 16:17
-2

depending on what you want.

Number of elements in array

length 

Number of bytes in array. This should be the argument to malloc.

length*sizeof(Questions)
Robert Jacobs
  • 3,266
  • 1
  • 20
  • 30