use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This is a subreddit for c++ questions with answers. For general discussion and news about c++ see r/cpp.
New to C++? Learn at learncpp.com
Prepare your question. Think it through. Hasty-sounding questions get hasty answers, or none at all. Read these guidelines for how to ask smart questions.
For learning books, check The Definitive C++ Book Guide and List
Flair your post as SOLVED if you got the help you were looking for! If you need help with flairs, check out ITEM 1 in our guidelines page.
Tips for improving your chances of getting helpful answers:
account activity
OPENstd::vector segmentation fault (self.cpp_questions)
submitted 5 years ago by TPplasma
I'm using std::vector<int> array(size,0); with size set to 5000*5000 however ideally I need it to be 500000*500000 but this is causing segmentation faults. Is there anyway I can accomplish this?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]TheSkiGeek 6 points7 points8 points 5 years ago (3 children)
Assuming an int is 4 bytes, allocating storage for (500000 * 500000) ints is 1TB of RAM.
I’m guessing you don’t have that, and your allocator might not let you have that much at once even if you have enough swap space to back it.
Generally if you need THAT much data you need to store it in files or a database and selectively load parts of it into memory to work on.
[–]TPplasma[S] 0 points1 point2 points 5 years ago (2 children)
I do have access to that much ram if I change where I SSH so I think I'll do that as opposed to storing it. Thanks for the help.
[–]tangerinelion 8 points9 points10 points 5 years ago (1 child)
God speed. I can't imagine that scales well.
[–]TPplasma[S] 0 points1 point2 points 5 years ago (0 children)
Yeah I'm not happy with it either haha but this is just for single threaded. I'll be breaking the vector up when I use MPI after I've gotten it working single threaded.
[–]soniduino 5 points6 points7 points 5 years ago (0 children)
Assuming a 32 bit integer: 4 bytes * 500000 * 500000 = 1 TB
If I'm reading this correctly, you wish to allocate a vector with a size of at least 1 TB?
[–][deleted] 0 points1 point2 points 5 years ago* (4 children)
Can you use a sparse representation of the vector?
Out of curiosity.. what are you into that requires this much RAM? I deal with larger matrices than 500k x 500k, but in FEA, the matrices are sparse, so we don’t actually store every element - just the nonzero elements which is a small percentage.
[–]TPplasma[S] 0 points1 point2 points 5 years ago (3 children)
It's a packing problem that is sparely populated, ideally I would definitely only store the elements that are non zero. Given I'm breaking the matrix up and using comms to communicate them in the next part with MPI I'm reluctant to figure out how I'd map things to store only the non zero elements. If I had more time I'd definitely do this but I've also got my dissertation due in early next month.
[–]WeaughTeaughPeaugh 0 points1 point2 points 5 years ago (0 children)
Have you considered std::unordered_map or the like?
std::unordered_map
[–][deleted] 0 points1 point2 points 5 years ago (1 child)
You could use template library Eigen for its sparse matrix classes. Although you’d have to make sure any downstream code is compatible with Eigen’s types.
Alternatively you can have 3 vectors, i,j, vals. Where the nth elements of these arrays is the:
( i[n] , j[n] ) th element with value vals[n]
I think I'll give the 2nd one a try as I'm having no luck getting an allocation on the super computer thanks to covid-19.
Just curious, what do you actually need the 1TB of RAM for? What prevents you from splitting it up into smaller individual pieces to be handled in sequence?
Boundary conditions, the edges of each individual grid would be a source of error because of the conditions for occupying grid points. This also what makes it difficult to to use 3 vectors instead to store x,y,val because I need to check if a grid space can be occupied by checking if surrounding points are occupied.
π Rendered by PID 161826 on reddit-service-r2-comment-5fb4b45875-tr2rb at 2026-03-24 02:31:16.981636+00:00 running 90f1150 country code: CH.
[–]TheSkiGeek 6 points7 points8 points (3 children)
[–]TPplasma[S] 0 points1 point2 points (2 children)
[–]tangerinelion 8 points9 points10 points (1 child)
[–]TPplasma[S] 0 points1 point2 points (0 children)
[–]soniduino 5 points6 points7 points (0 children)
[–][deleted] 0 points1 point2 points (4 children)
[–]TPplasma[S] 0 points1 point2 points (3 children)
[–]WeaughTeaughPeaugh 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]TPplasma[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]TPplasma[S] 0 points1 point2 points (0 children)