473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Max size of an array in c#?

I have code like so:

FileStream fs = new FileStream(locationTextBox.Text, FileMode.Open,
FileAccess.Write, FileShare.Read);
long size = fs.Length;
byte[] buffer = new byte[size];

// Get file to byte array
fs.Position = 0;
fs.Write(buffer, 0, buffer.Length);
fs.Close();

which works just fine for small/medium sized files. However, if I have
large files (2GB) this fails. I get an "arithmetic overflow" error on
the line

byte[] buffer = new byte[size];

if "size" is large, like 2678614016 bytes.

Is there a better way to read in large files?

Thanks,

Christian

Nov 17 '06 #1
2 17521
The index of an array is an integer (that is it can't be greater than
Integer.MaxValue). Strickly spekaing the problem is not that you don"t have
enough memory but ratehr taht you use a long value that is beyond the last
possible index for an array and its conversion to integer fails.

Generally files are read as multiple chunks of data into a small buffer to
avoid suing up the whole memmory for huge files (even worse if this is an
ASP.NET where several people could perhaps read huge files consuming the
whole memory).

You may want to elaborate a bit about whihc kind of process you are applying
to this file...

--
Patrice

<ka******@hotmail.coma écrit dans le message de news:
11**********************@j44g2000cwa.googlegroups. com...
>I have code like so:

FileStream fs = new FileStream(locationTextBox.Text, FileMode.Open,
FileAccess.Write, FileShare.Read);
long size = fs.Length;
byte[] buffer = new byte[size];

// Get file to byte array
fs.Position = 0;
fs.Write(buffer, 0, buffer.Length);
fs.Close();

which works just fine for small/medium sized files. However, if I have
large files (2GB) this fails. I get an "arithmetic overflow" error on
the line

byte[] buffer = new byte[size];

if "size" is large, like 2678614016 bytes.

Is there a better way to read in large files?

Thanks,

Christian

Nov 17 '06 #2
if you are on a 32bit machine, you only have 2gb of addressess space. you
will also need a matching paging file, etc. you should not read large files
into memory, just the parts you need.

-- bruce (sqlwork.com)

<ka******@hotmail.comwrote in message
news:11**********************@j44g2000cwa.googlegr oups.com...
>I have code like so:

FileStream fs = new FileStream(locationTextBox.Text, FileMode.Open,
FileAccess.Write, FileShare.Read);
long size = fs.Length;
byte[] buffer = new byte[size];

// Get file to byte array
fs.Position = 0;
fs.Write(buffer, 0, buffer.Length);
fs.Close();

which works just fine for small/medium sized files. However, if I have
large files (2GB) this fails. I get an "arithmetic overflow" error on
the line

byte[] buffer = new byte[size];

if "size" is large, like 2678614016 bytes.

Is there a better way to read in large files?

Thanks,

Christian

Nov 17 '06 #3

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

Similar topics

2
by: p175 | last post by:
Hi folks, I've tried reading just about every post I can on raid stiping / extent size etc and I'm just getting myself more confused than ever. Here's my situation. On a Windows Server 2000...
5
by: sugaray | last post by:
Hi, my problem with calculating the size of an array is when I pass an array as a parameter to a function which perform the calculation, the result never comes right, like below: int...
22
by: Wynand Winterbach | last post by:
I think every C programmer can relate to the frustrations that malloc allocated arrays bring. In particular, I've always found the fact that the size of an array must be stored separately to be a...
40
by: madireddy | last post by:
Hi, Inside a program how do i find out what size has been allocated to pointer using malloc. eg... int *p; p=(int*)malloc(1024*sizeof(int)); now how do find how much has been allocated to p?...
12
by: manochavishal | last post by:
Hi, I have a question. How can i know the size of array when it is passed to a function. For Example i have this code: #include <stdio.h> #include <stdlib.h>
8
by: redefined.horizons | last post by:
I would like to have an array declaration where the size of the array is dependent on a variable. Something like this: /* Store the desired size of the array in a variable named "array_size". */...
2
by: Harry | last post by:
Good Day To all, When i am declaring a array for e.g char ....it means i am declaring array of 45 characters each of which has a maximum,minimum value limit or range...for example in...
7
by: bowlderster | last post by:
Hello,all. I want to get the array size in a function, and the array is an argument of the function. I try the following code. /*************************************** */ #include<stdio.h>...
5
by: desktop | last post by:
I have a function that takes two pointers to an array. The first point to the first element while the other points to the last element. int nums = { 1, 2, 3, 4, 5, 7, 8, 9}; int* result; int...
33
by: Adam Chapman | last post by:
Hi, Im trying to migrate from programming in Matlab over to C. Im trying to make a simple function to multiply one matrix by the other. I've realised that C can't determine the size of a 2d...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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...

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.