0

Hi all, when I declare an array:

datatype array[Index];

what's max value of Index ?

Edited by widapol: n/a

3
Contributors
10
Replies
12
Views
6 Years
Discussion Span
Last Post by gerard4143
0

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.

Edited by gerard4143: n/a

0

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

Edited by widapol: n/a

0

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.

Edited by gerard4143: n/a

0

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]
0

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.

0

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?

0

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?

Edited by gerard4143: n/a

0

>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.

Edited by Narue: n/a

0

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.

0

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..

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.