Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing array declared by malloc

Tags:

c

pointers

I was working on an assignment (details in another question). As part of it, I was increasing the size of the array. And found that when I try to initialize an array:

int arr[2097152]; // 8MB

I got segmentation fault ... I think its because I am trying to declare an array that too large? Then I found the way to get around this is to use malloc. But being new to C (mainly use JavaScript/Python/Java ...). I get very confused with pointers and stuff ...

I've declared an array 8MB:

int *arr = malloc (MBs * 1024 * 1024 / sizeof(int)); // MBs = 8

But now ... how do I access it, or write to it? When I use it like arr am I getting the address, and if I use *arr I get the 1st element?

like image 712
Jiew Meng Avatar asked Sep 26 '12 09:09

Jiew Meng


People also ask

Can malloc be used for arrays?

The malloc() function is used in dynamic memory allocation and it can also be used to store values in the array. In this write-up, we have discussed how to declare and use the array using the malloc() function.

What does malloc () return?

malloc returns a void pointer to the allocated space, or NULL if there's insufficient memory available. To return a pointer to a type other than void , use a type cast on the return value.

How do I malloc a dynamic array?

To dynamically allocate space, use calls to malloc passing in the total number of bytes to allocate (always use the sizeof to get the size of a specific type). A single call to malloc allocates a contiguous chunk of heap space of the passed size.

Is memory allocated for an array when it is declared?

Statically declared arrays are allocated memory at compile time and their size is fixed, i.e., cannot be changed later. They can be initialized in a manner similar to Java. For example two int arrays are declared, one initialized, one not. Static multi-dimensional arrays are declared with multiple dimensions.


2 Answers

Use it like arr[index], just as if it were declared as an array. In C, the notation x[y] is exactly equivalent to *(x + y). This works in the case of an array because the array name is converted to a pointer to its first element.

int *arr = malloc (MBs * 1024 * 1024 / sizeof(int));

This is not a good approach (and doesn't make it the size you want), because you don't have the number of elements handy. You should declare it based on the number of elements, e.g.,

#define ARR_LENGTH 2097152
int *arr = malloc (ARR_LENGTH * sizeof *arr);

You need to multiply by the element size because malloc's argument is a byte count.

like image 102
Jim Balter Avatar answered Sep 28 '22 06:09

Jim Balter


I think its because I am trying to declare an array that too large?

Yes, Indeed. You are most likely doing this in a function. Wherein the array is created on local storage on stack and usually stack will not be big enough to cater to demands of this big size.
The usual solution is to use dynamic memory.

How do I access it, or write to it? When I use it like arr I am getting the address, and if I use *arr I get the 1st element?

You can access it in similar way as you use an array by using the [] operator.

Note that,

arr[i] == i[arr] == *(arr + i) == *(i + arr)

So,

arr[0] ---> Gives you first element
arr[1] ---> Gives you Second element
and so on...

Note that the name of the array decays as an pointer to its first element sometimes but arrays and pointers are not the same!

Good Read:

How do I use arrays in C++?

like image 23
Alok Save Avatar answered Sep 28 '22 07:09

Alok Save