473,763 Members | 1,373 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Array size limits

How do I determine the maximum array size?

For example, int a[10000] works, but a[10000000] does not (run time
error).

Thank you.

Nov 14 '05 #1
37 48746
On Tue, 31 Aug 2004 02:08:08 GMT, Carol Depore <no****@nowhere .com>
wrote in comp.lang.c:
How do I determine the maximum array size?
You can't
For example, int a[10000] works, but a[10000000] does not (run time
error).

Thank you.


The original C standard (ANSI 1989/ISO 1990) required that a compiler
successfully translate at least one program containing at least one
example of a set of environmental limits. One of those limits was
being able to create an object of at least 32,767 bytes.

This minimum limit was raised in the 1999 update to the C standard to
be at least 65,535 bytes.

No C implementation is required to provide for objects greater than
that size, which means that they don't need to allow for an array of
ints greater than (int)(65535 / sizeof(int)).

In very practical terms, on modern computers, it is not possible to
say in advance how large an array can be created. It can depend on
things like the amount of physical memory installed in the computer,
the amount of virtual memory provided by the OS, the number of other
tasks, drivers, and programs already running and how much memory that
are using. So your program may be able to use more or less memory
running today than it could use yesterday or it will be able to use
tomorrow.

Many platforms place their strictest limits on automatic objects, that
is those defined inside of a function without the use of the 'static'
keyword. On some platforms you can create larger arrays if they are
static or by dynamic allocation.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #2
Carol Depore <no****@nowhere .com> writes:
How do I determine the maximum array size?


There is no portable way. You are better off using dynamic
allocation, because then you can try different sizes at runtime.
--
"Am I missing something?"
--Dan Pop
Nov 14 '05 #3
On Mon, 30 Aug 2004 21:48:07 -0500, Jack Klein <ja*******@spam cop.net>
wrote:
On Tue, 31 Aug 2004 02:08:08 GMT, Carol Depore <no****@nowhere .com>
wrote in comp.lang.c:
How do I determine the maximum array size?
You can't
For example, int a[10000] works, but a[10000000] does not (run time
error).

Thank you.


The original C standard (ANSI 1989/ISO 1990) required that a compiler
successfully translate at least one program containing at least one
example of a set of environmental limits. One of those limits was
being able to create an object of at least 32,767 bytes.

This minimum limit was raised in the 1999 update to the C standard to
be at least 65,535 bytes.

No C implementation is required to provide for objects greater than
that size, which means that they don't need to allow for an array of
ints greater than (int)(65535 / sizeof(int)).


So...are you saying that I am guaranteed at least int a[65535], but no
guarantees beyond that?



In very practical terms, on modern computers, it is not possible to
say in advance how large an array can be created. It can depend on
things like the amount of physical memory installed in the computer,
the amount of virtual memory provided by the OS, the number of other
tasks, drivers, and programs already running and how much memory that
are using. So your program may be able to use more or less memory
running today than it could use yesterday or it will be able to use
tomorrow.

Many platforms place their strictest limits on automatic objects, that
is those defined inside of a function without the use of the 'static'
keyword. On some platforms you can create larger arrays if they are
static or by dynamic allocation.


Nov 14 '05 #4
Carol Depore <no****@nowhere .com> writes:
On Mon, 30 Aug 2004 21:48:07 -0500, Jack Klein <ja*******@spam cop.net>
wrote:
No C implementation is required to provide for objects greater than
that size, which means that they don't need to allow for an array of
ints greater than (int)(65535 / sizeof(int)).


So...are you saying that I am guaranteed at least int a[65535], but no
guarantees beyond that?


No. To start with, sizeof(int) may be, and usually is, greater
than 1.
--
"...what folly I commit, I dedicate to you."
--William Shakespeare, _Troilus and Cressida_
Nov 14 '05 #5
On Mon, 30 Aug 2004 20:48:45 -0700, Ben Pfaff <bl*@cs.stanfor d.edu>
wrote:
Carol Depore <no****@nowhere .com> writes:
On Mon, 30 Aug 2004 21:48:07 -0500, Jack Klein <ja*******@spam cop.net>
wrote:
No C implementation is required to provide for objects greater than
that size, which means that they don't need to allow for an array of
ints greater than (int)(65535 / sizeof(int)).


So...are you saying that I am guaranteed at least int a[65535], but no
guarantees beyond that?


No. To start with, sizeof(int) may be, and usually is, greater
than 1.

Oh, I see...so, if sizeof(int) is 2 bytes, then I am guaranteed at
least int a[32767], but no larger sized arrays are guaranteed.

Thanks for the help!

Nov 14 '05 #6

"Carol Depore" <no****@nowhere .com> wrote in message
news:iu******** *************** *********@4ax.c om...
On Mon, 30 Aug 2004 20:48:45 -0700, Ben Pfaff <bl*@cs.stanfor d.edu>
wrote:
Carol Depore <no****@nowhere .com> writes:
On Mon, 30 Aug 2004 21:48:07 -0500, Jack Klein <ja*******@spam cop.net>
wrote:
No C implementation is required to provide for objects greater than
that size, which means that they don't need to allow for an array of
ints greater than (int)(65535 / sizeof(int)).

So...are you saying that I am guaranteed at least int a[65535], but no
guarantees beyond that?
No. To start with, sizeof(int) may be, and usually is, greater
than 1.

Oh, I see...so, if sizeof(int) is 2 bytes, then I am guaranteed at
least int a[32767], but no larger sized arrays are guaranteed.

No, You have to check INT_MAX in your limits.h file to know the exact
maximum
value that an integer can hold on your implementation.

Thanks for the help!

Nov 14 '05 #7
"Ravi Uday" <ra******@gmail .com> writes:
"Carol Depore" <no****@nowhere .com> wrote in message
news:iu******** *************** *********@4ax.c om...

[...]
Oh, I see...so, if sizeof(int) is 2 bytes, then I am guaranteed at
least int a[32767], but no larger sized arrays are guaranteed.

No, You have to check INT_MAX in your limits.h file to know the exact
maximum
value that an integer can hold on your implementation.


INT_MAX isn't relevant. The limit in question is the maximum size of
an object, which is at least 65535 bytes (in a hosted environment
only). If sizeof(int) is 2, you're guaranteed at least int a[32767];
if sizeof(int) is 4; you're only guaranteed at least int a[16383].

But actually the guarantee is even weaker than that. The standard
only requires that the implementation must be able to translate and
execute at least one program containing an object of 65535 bytes
(along with a number of other limits, such as 127 arguments in one
function call). It's not required to handle *your* program containing
an object of 65535 bytes.

Practically speaking, though, the easiest way to satisfy the
translation limits is generally to impose no explicit limits, but to
support whatever will fit in memory, either at compile time or at run
time. A (non-binding) footnote in C99 5.2.4.1 says, "Implementation s
should avoid imposing fixed translation limits whenever possible."

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #8
Keith Thompson wrote:

"Ravi Uday" <ra******@gmail .com> writes:
"Carol Depore" <no****@nowhere .com> wrote in message
news:iu******** *************** *********@4ax.c om...

[...]
Oh, I see...so, if sizeof(int) is 2 bytes, then I am guaranteed at
least int a[32767], but no larger sized arrays are guaranteed.

No, You have to check INT_MAX in your limits.h file to know the exact
maximum
value that an integer can hold on your implementation.


INT_MAX isn't relevant. The limit in question is the maximum size of
an object, which is at least 65535 bytes (in a hosted environment
only). If sizeof(int) is 2, you're guaranteed at least int a[32767];
if sizeof(int) is 4; you're only guaranteed at least int a[16383].

But actually the guarantee is even weaker than that. The standard
only requires that the implementation must be able to translate and
execute at least one program containing an object of 65535 bytes
(along with a number of other limits, such as 127 arguments in one
function call). It's not required to handle *your* program containing
an object of 65535 bytes.


I think what they meant by the "at least one" part,
is closer to saying
"A C implementation is something which can translate and execute
a C program, and anything that can't translate and execute
a C program, isn't a C implementation"

I don't think that they meant to suggest that an implementation
which doesn't self destruct after program translation and execution,
"exceeds ANSI standards".

--
pete
Nov 14 '05 #9

Everyone, thank you for your help. I feel a little like I'm asking
Einstein to explain relativity to me. I'm way out of my league.
So, thanks for your patience.

Anyway, I'm still confused about how large a simple int array can be.
I understand what Jack and Ben said about the Standard guaranteeing
at least int a[32767], but I don't understand why I can't have an
array of int a[600000], especially since I have 311MB of unused memory
on my machine, and I thought the machine would allow programs up to
4GB.

Here's my little test program, which works for 500000, but fails for
600000.

All help to relieve my confusion is appreciated. I think I'm not
understanding something very fundamental.
#include <stdio.h>

//#define NNN 600000
#define NNN 500000

int main() {
long i;
int a[NNN];
for (i=0;i<NNN;i++) {
printf("%i\n",i );
a[i] = 1;
}
}

Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
2225
by: mike | last post by:
hi, i am running a very big code on my pc using redhat linux. when i try to increase my array size, compile and run, i get segmentation fault. i go into the debugger, run it, it crashes right away. i can't even trace where it happens. i have no idea what is the problem. any idea ???
0
9563
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10145
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9998
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9938
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9822
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8822
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3523
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.