Malloc stuff


#1

Hi! Just a little question about the use of malloc:

If we have to hold a couple of floats in an array, what’s the difference between this malloc(2) this malloc(sizeof(float)) and this malloc(2 * sizeof(float))???

When I print the size of each one of this examples I got the same answer: 8 bytes… so… what’s the good practice here? What should I get used to?

Thanks in advance.


#2

[quote]malloc(2)[/quote]Allocates room for only two bytes.

[quote]malloc(sizeof(float))[/quote]Allocates room for only one float.

[quote]malloc(2 * sizeof(float))[/quote]Allocates room for only two floats.

[quote]When I print the size of each one of this examples I got the same answer: 8 bytes… so… what’s the good practice here? What should I get used to?[/quote]You are probably printing the size of a pointer, not the size of the actual object.


#3

Thanks ibex10, just to be clear about what I’m printing, this is a more clean example:

[code]//Allocate memory for each one

float *gradeBook1 = malloc(3 * sizeof(float));
float *gradeBook2 = malloc(2 * sizeof(float));

//Assigning values

gradeBook1[0] = 60.2;
gradeBook1[1] = 94.5;
gradeBook1[2] = 81.1;

gradeBook2[0] = 80.8;
gradeBook2[1] = 92.3;[/code]

//Printing

printf("%zu\n", sizeof(gradeBook1)); printf("%zu\n", sizeof(gradeBook2));

And still getting this:

Size of gradeBook1: 8 Size of gradeBook2: 8 Program ended with exit code: 0

What I’m doing wrong?


#4

[quote][code]// Allocate memory for each one

float *gradeBook1 = malloc (3 * sizeof (float));
float *gradeBook2 = malloc (2 * sizeof (float));

printf ("%zu\n", sizeof (gradeBook1));
printf ("%zu\n", sizeof (gradeBook2));
[/code]
[/quote]You are not doing anything wrong, but you are printing the sizes of pointers.

What are you trying to print? The number elements in each array or their sizes in bytes?

The sizeof operator gives you the size (in bytes) of the object to which you apply it. In your code that object is a pointer variable. So sizeof (gradeBook1) gives you the size of the gradeBook1 pointer variable.
You can not get the sizes of the arrays in your code by using the sizeof operator; you have to compute them.

[code]// Allocate memory for each one
const unsigned long N1 = 3; // three elements
const unsigned long N2 = 2; // two elements

float *gradeBook1 = malloc (N1 * sizeof (float));
float *gradeBook2 = malloc (N2 * sizeof (float));

printf (“gradeBook1 array takes %zu bytes\n”, N1 * sizeof (float));
printf (“gradeBook2 array takes %zu bytes\n”, N2 * sizeof (float));
[/code]
But there is a quirk in the language when it comes to compile-time arrays. Although the gradeBook is a pointer in the example below, the sizeof operator yields the size of the array, not the size of the pointer!

// Allocate a compile-time array with three elements const unsigned long N = 3; float gradeBook [N]; ... printf ("gradeBook array takes %zu bytes\n", sizeof (gradeBook)); printf ("gradeBook array takes %zu bytes\n", N * sizeof (float));


#5

Thank you so much ibex10, my purpose was to see if there were any differences in the size of the arrays created in that different ways I did (allocating) just to check how much space was using each one in memory, but I got your point, and is good to notice about that “quirk” stuff.

See you later!