To allocate enough space for an array of 10 integers (on a 64bit, Linux machine), I can use:malloc(64 * 10)malloc(10 * int)malloc(10 * sizeof(int))
Question
To allocate enough space for an array of 10 integers (on a 64bit, Linux machine), I can use:
malloc(64 * 10)
malloc(10 * int)
malloc(10 * sizeof(int))
Solution
The correct way to allocate enough space for an array of 10 integers on a 64-bit Linux machine is to use malloc(10 * sizeof(int))
.
Here's why:
-
malloc(64 * 10)
is incorrect because it allocates 640 bytes. This might be more than you need, or it might not be enough, depending on the size of an integer on your system. On a 64-bit system, an integer is typically 4 bytes, so you would only need 40 bytes to store 10 integers. -
malloc(10 * int)
is incorrect becauseint
is a type, not a value. You can't multiply by a type in C. -
malloc(10 * sizeof(int))
is correct. Thesizeof
operator gives you the size of a type in bytes. On a 64-bit Linux system,sizeof(int)
is typically 4, so10 * sizeof(int)
gives you exactly the 40 bytes you need to store 10 integers.
Similar Questions
The memory space reserved when calling malloc is on:The stackThe heapI don't know
blank is used to allocate memory to array variable in Javamalloc calloc new alloc
Which operator is used to allocate memory dynamically in C++?mallocallocnewcreate
How is memory allocated for an array dynamically in C++?int* arr = new int[10];int arr = new int[10];int arr[10] = new int;int* arr = alloc int[10];
The statement char arr [10]; is an example of a type of ____________ memory allocation.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.