Hi all, when I declare an array:

datatype array[Index];

what's max value of Index ?

Recommended Answers

All 10 Replies

Do you mean what is the last element?

The last element is array[Index - 1].

Or do you mean how big can an array be? How many elements can it have....

C defines a type called size_t which accommodates the largest arrays.

No, I mean the max value that Index can have.

No, I mean the max value that Index can have.

do you mean the max value of an array element or do you mean the largest element an array can have?

If you mean the latter then it depends what Index is? Is Index a char, int...size_t.

do you mean the max value of an array element or do you mean the largest element an array can have?

If you mean the latter then it depends what Index is? Is Index a char, int...size_t.

I mean the largest number of elements an array can have. Because of my program, when I declare

pid_t cpid[999999]

the program run well. But when I change the size of array to 9,999,999; my program gets segmentation fault.

pid_t cpid[9999999]

I mean the largest number of elements an array can have. Because of my program, when I declare

pid_t cpid[999999]

the program run well. But when I change the size of array to 9,999,999; my program gets segmentation fault.

pid_t cpid[9999999]

The largest number of elements an array can hold in C is whatever your implementation defines size_t as...Most implementation define it as unsigned int.

So it really depends on your implementation. Try doing a sizeof(size_t) to see how bytes it is.

Actually I don't know how to define size_t like you wrote. For example if I want to declare an array that have the number of elements larger than 2^32. How can I implement? Could you give me some sample codes?

The easiest way...Get on a 64 bit machine with a 64 bit C compiler.

So let me get this straight, you want to make an array greater than the virtual memory address space that can hold it? Is that correct?

>The largest number of elements an array can hold in C is whatever your implementation defines size_t as...
65535 bytes is the minimum size for an object, so you're guaranteed to have an array of sizeof(array)/sizeof(array[0]) <= 65535. Beyond that you enter the realm of non-portable where hitting runtime stack limitations becomes a possibility. You're not guaranteed to have an array of size_t elements, and in practice that's very unlikely to be a successful declaration.

If you want a large array, my recommendation is to do it with dynamic memory rather than actual arrays.

The easiest way...Get on a 64 bit machine with a 64 bit C compiler.

So let me get this straight, you want to make an array greater than the virtual memory address space that can hold it? Is that correct?

Yes, it is.

Yes, it is.

Then your going to have to figure out a scheme of swapping data to and from a file because you can't have an array bigger than your address space..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.