0
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    double *array;
    unsigned int size;

    printf("Choose size for your number array: ");
    scanf("%u", &size);

    array = malloc(sizeof(double) * size);

    return 0;
}

I memory allocated sizeof(double) * size, which I don't know if sizeof(double) is necessary, but sizeof(double) is not 1, so I don't know if I should either:

for (int i = 0; i < size; i++) {

} 

For loop through size without multiply it with sizeof(double), or:

for (int i = 0; i < sizeof(double) * size; i++) {

}

For loop and multiply size with sizeof(double) as well? The reason why I'm asking is because I really want to be careful and prevent going over size.

For loop without multiplying sizeof(double) or not?

14
  • How many doubles are there in your array? size or size * sizeof(double)? Commented Jul 17, 2020 at 20:21
  • 1
    When you increment the index of an array, it moves by a whole double, not a byte. Same for a pointer. Commented Jul 17, 2020 at 20:23
  • For example, sizeof(char) would be 1, but sizeof(double) won't be 1 (Idk if it is 8), so am I supposed to multiply by sizeof(double) or what? I'm starting to get confused on the purpose of multiplying sizeof(double). Commented Jul 17, 2020 at 20:29
  • Usually without multiplying, but to make sure, tell us what's inside the loop and how you use i. Commented Jul 17, 2020 at 20:43
  • 1
    Jack, consider arr = malloc(sizeof *arr * size);. It is the right size for an array size of whatever type arr points to. Commented Jul 17, 2020 at 20:45

3 Answers 3

3

You allocate sizeof(double) * size memory to have array of doubles, the number of elements is size. So multiply in malloc, don't multiply in for

Sign up to request clarification or add additional context in comments.

4 Comments

Do you multiply sizeof(double) when double array[sizeof(double) * size] or only for malloc?
@JackMurrow You only multiply sizeof(double) * size in the malloc. There is no need to multiply for anything else. It seems to me that what you really need is to first understand what malloc does.
"The malloc() function allocates size bytes ..." Note the "bytes". If you have a double that is 8 bytes and you have an array of 10 doubles, then you need 80 bytes allocated. Which in your case is size (e.g. 10) times sizeof(double) (8) gives you storage for 10 doubles.
Ohh, it allocates size bytes. Thanks for the help!
2

In this statement

array = malloc(sizeof(double) * size);

there is allocated memory for size elements with the size of objects of the type double.

You could rewrite the statement like

array = malloc( sizeof( double[size] ) );

An equivalent declaration with automatic storage duration would look like

double array[size];

if you will write (for the declaration of the array shown above with automatic or static storage duration)

printf( "sizeof( array ) = %zu\n", sizeof( array ) );

you will get a value equal to size * sizeof( double ).

So if you want to traverse all allocated elements to initialize them you should write for example

for ( int i = 0; i < size; i++ )
{
    *( array + i ) = 0.0;
}

that is equivalent to

for ( int i = 0; i < size; i++ )
{
    array[i] = 0.0;
}

This expression

array + i

where there is used the pointer arithmetic yields the value of pointer equal to

( double * )( ( char * )array + i * sizeof( double ) ) 

That is it points to the i-th element of the allocated array.

8 Comments

I don't exactly understand the part about when you said This expression where there is used the pointer arithmetic yields the value of pointer equal to That is it points to the i-th element of the allocated array. Can you explain a bit more in an easier way?
@JackMurrow When you write for example array[i] then you get the address that has the offset relative to the initial address of array equal to i * sizeof( double ).
Sorry, I still don't understand for array[i] then you get the address that has the offset relative to the initial address of array equal to i * sizeof( double ). I don't want to waste too much of your time.
@JackMurrow For example array[0] has the address (for simplicity) equal to 0. Then the address of the element a[1] is equal to 0 + 1 * sizeof( double ). That is to get the access to the second element of the array you have to move the pointer to the right by the value of the object of the type double.
@JackMurrow Imagine that you are in the beginning of a street that has several buildings of the same size. To come to the second building you have to walk as many meters as the size of the first building
|
1

malloc(X) returns X bytes, so if you want to store size doubles, call malloc(size * sizeof(double))

Comments

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.