Array size

In a C program is there any limit on the size of an array?

I will say Yes, there is limitation of array size.
However, this will base on O.S and memory.

Let's say you have 4Gb Virtual Mem; it does not mean you can use all of them (i.e. some of them reserved for thread frames...)

If you need to know more details, you may want to go through some O.S internal books.

hope this help

:wink:

When a new process is created it is not given unlimited memory. Much of the memory requirements come from symbols created during compile time based on memory allocation requirements. Examples in C include the malloc system call, memory allocation.

So, the size of any array is contrained by how much space for that array was malloc'ed for example, when the binary was compiled and process executed.

Hope this helps.

The answer is No and Yes. Theoretically,
there is no limit. However, the kernel may be
configured to allow only a limited amount
of memory per process and/or user. You can find
out what the system resource limits are on your
particular UNIX system using the system
call "getrlimit()" (man section 2). The following
C program can be compiled and run as any ordinary
user...

================== SNIP ========================
/*

  • Filename - getlimits.c
    *
  • Description - display system resource limits and
  •           test allocating large blocks of memory.
    

*
*/

#include <sys/time.h>
#include <sys/resource.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

main(int argc, char **argv)
{
struct rlimit rlim;
void *p;

getrlimit\(RLIMIT_CPU, &rlim\);
printf\("CPU:     cur - %ld   max - %ld\\n", rlim.rlim_cur, rlim.rlim_max\);
getrlimit\(RLIMIT_FSIZE, &rlim\);
printf\("FSIZE:   cur - %ld   max - %ld\\n", rlim.rlim_cur, rlim.rlim_max\);
getrlimit\(RLIMIT_DATA, &rlim\);
printf\("DATA:    cur - %ld   max - %ld\\n", rlim.rlim_cur, rlim.rlim_max\);
getrlimit\(RLIMIT_STACK, &rlim\);
printf\("STACK:   cur - %ld   max - %ld\\n", rlim.rlim_cur, rlim.rlim_max\);
getrlimit\(RLIMIT_CORE, &rlim\);
printf\("CORE:    cur - %ld   max - %ld\\n", rlim.rlim_cur, rlim.rlim_max\);

#ifdef LINUX
getrlimit(RLIMIT_RSS, &rlim);
printf("RSS: cur - %ld max - %ld\n", rlim.rlim_cur, rlim.rlim_max);
getrlimit(RLIMIT_NPROC, &rlim);
printf("NPROC: cur - %ld max - %ld\n", rlim.rlim_cur, rlim.rlim_max);
#else
getrlimit(RLIMIT_VMEM, &rlim);
printf("VMEM: cur - %ld max - %ld\n", rlim.rlim_cur, rlim.rlim_max);
getrlimit(RLIMIT_AS, &rlim);
printf("AS: cur - %ld max - %ld\n", rlim.rlim_cur, rlim.rlim_max);
#endif
getrlimit(RLIMIT_NOFILE, &rlim);
printf("NOFILE: cur - %ld max - %ld\n", rlim.rlim_cur, rlim.rlim_max);
#ifdef LINUX
getrlimit(RLIMIT_MEMLOCK, &rlim);
printf("MEMLOCK: cur - %ld max - %ld\n", rlim.rlim_cur, rlim.rlim_max);
#endif

if\(\(p = malloc\(100000000\)\) == NULL\)
\{
    printf\("malloc 100M failed\\n"\);
\}
else
\{
    printf\("malloc 100M succeeded\\n"\);
\}
if\(p\) free\(p\);
if\(\(p = malloc\(500000000\)\) == NULL\)
\{
    printf\("malloc 500M failed\\n"\);
\}
else
\{
    printf\("malloc 500M succeeded\\n"\);
\}
if\(p\) free\(p\);
return 0;

}

================== SNIP ========================

The output (on Linux) should look somthing like...

$ getlimits
CPU: cur - 2147483647 max - 2147483647
FSIZE: cur - 2147483647 max - 2147483647
DATA: cur - 2147483647 max - 2147483647
STACK: cur - 8388608 max - 2147483647
CORE: cur - 1024000000 max - 2147483647
RSS: cur - 2147483647 max - 2147483647
NPROC: cur - 2048 max - 2048
NOFILE: cur - 1024 max - 1024
MEMLOCK: cur - 2147483647 max - 2147483647
malloc 100M succeeded
malloc 500M succeeded

For some reason, the include files didn't
come out in the code in the previous posting.
the files are...

sys/time.h
sys/resource.h
unistd.h
stdio.h
stdlib.h