PDA

View Full Version : C++ Large Arrays


ghell
01-19-2006, 10:31 AM
Hi, a long time ago i wrote a small program which used something along the lines of

char bigarray[256][256][8192]

and it made the array on a computer with 768mb ram no problem .. but if i wanted to clear the contents to be 0 in each element i had to itterate through it..


so i thought i would give it another go on the same machine a year later but now if i try and make a char array bigger than 256*256*31 it gives me a send/dont send error and no try catch block seems to stop this crash and give me some text for the error that i can actually work on!

i may have compiled with borland before, i'm not sure, i'm compiling with mingw now..

really im trying to make something able to check if an int is used twice by using ~512mb ram to have one bit for every possibility (256^4).. i don't know if there is any better way of doing this though, i am currently using a java byte[256][256][8192] (as each byte has 8 bits in it this has 256^4 bits) but to get that i needed to change the max memory flag in javac.. i don't know if there is anything similar in mingw.. i made a c# version too but thats even slower than java hehe

aman
01-19-2006, 12:51 PM
Yea that's too much memory allocated on the stack and will most likely just crash. You can check into the compiler's docs to increase stack size (which I would consider poor coding because it isn't portable). Better yet, always use malloc (or operator new in C++) to allocate any significant amount of memory.

oracleguy
01-19-2006, 10:27 PM
Maybe you could explain better what you are trying to do, maybe there is a better way than having such a huge array.

ghell
01-20-2006, 12:58 AM
well, for example, if the int 0xFE043C02 was used then it would mark the correct bit (byte 0xFE043C would have a value of 0x02 if nothing else in that byte had already been added to the list .. i think) as a 1 instead of 0 (which requires it to default to 0) .. due to the number of possibilities of an int, this requires 4,294,967,296 bits (which is exactly 512 megabytes) and due to the smallest type being one byte long in memory, a single byte must be used for 8 bits.. this is not a problem using bitwise operators and shifting. I cant think of a smaller way to store whether a certain int has been used or not (appart from a list of used ints which i have used before but increases the program running time in java from approx 0.5s to 30mins+ (and this gets exponentially much longer as more ints are checked)

this is not an app i am going to distribute so it doesnt matter about lack of portability but i cannot find any documentation on increasing the amount of memory that the compiled program can use, it never used to need to be told either..it just hung when setting them to 0 if it couldn't.. i may have been using borlands compiler though but im pretty sure it did the same on mingw which is what i am using now..

i dont know any ways of using "new" to use this.. as you may have guessed i am pretty new to c++

edit: i found this article: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/express_25.asp and if i use it i see my memory usage spike from 200mb to 700 but im not entirely sure how to use the pointer for the array, i know how to itterate through the array with the pointer but dont know how to go straight to a certain point.. i could just start at the start of the array, move forward x places and use that then move back x places again i suppose.. i don't know if there is a different/better way?

edit again: nevermind, you can just use this char* as if it were a char[] apparently

Mhtml
01-20-2006, 01:25 AM
I dunno if I understand you correctly, are you just trying to find the range of an int?

ghell
01-20-2006, 01:30 AM
i have a list of say 1 million int values, i am checking to make sure that none are used twice. this is currently done by setting a bit to 1 once its used and checking if its 1 every time it gets a new int from the list. one bit represents one possibility of the int.

heres an example only using 4 ints (one of which is duplicate) and one byte

start:
0000 0000

int = 3: (0000 0000) --> no duplicate
0000 1000

int = 4: (0000 1000) --> no duplicate
0001 1000

int = 3: (0001 1000) --> duplicate

int = 7: (0001 1000) --> no duplicate
1001 1000

tcwlr
01-24-2006, 11:29 PM
Here's a project i did w/RSA where we had really large numbers and needed long strings..

__________________________________
Not Everyone Is Talented...
www.roguespyder.com

http://www.roguespyder.com/images/july14.JPG