I intend to allocate a large array of size 1073741824
(equivalent to 1GB eg) and then read from it randomly but i get segmentation fault (core dumped) for only defining the array as i checked which i do like:
unsigned int size = 1073741824;
short arr = malloc(size * sizeof(short));
i also tried casting it as follows but still the same issue:
unsigned int size = 1073741824;
short *arr = (short*) malloc(size * sizeof(short));
also the ulimit
command returns unlimited
so what am i doing wrong?
The likely cause of your problem is malloc()
fails to allocate close to 2GB of memory on your system. You must check the return value: if malloc()
fails, it returns a NULL
pointer which cannot be dereferenced.
Furthermore, your first code fragment defines arr
as a short
instead of a short *
.
Here is the typical code pattern you should be using:
unsigned int size = 1073741824;
short *arr = malloc(size * sizeof(short));
if (arr == NULL) {
fprintf("cannot allocate memory for %u shorts\n", size);
exit(1);
}
I think your problem is that the function malloc returns NULL due to the large size of the array. Using malloc directly is quite error prone. Therefor it's better to use a macro function like this:
#include <stdio.h>
#include <stdlib.h>
#define NEW_ARRAY(ptr, length) \
{ \
(ptr) = malloc((size_t) (length) * sizeof (ptr)[0]); \
if ((ptr) == NULL) { \
fputs("error: Memory exhausted\n", stderr); \
exit(EXIT_FAILURE); \
} \
}
int main(void)
{
unsigned int arrLen = 1073741824;
short *arr;
NEW_ARRAY(arr, arrLen);
/*...*/
free(arr);
return 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.