LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-13-2010, 08:39 PM   #1
mayankladoia
LQ Newbie
 
Registered: Jul 2010
Posts: 5

Rep: Reputation: 0
terminate called after throwing an instance of 'std::bad_alloc'


I have written a code in which i have used vectors:


vector< vector<double> > arr;
arr.resize(x,vector<double>(x,0));

x is a variable which is taken from a very beg text file > 64MB


first line of my code is
cout<<"\n\nWait Running...";


my code takes text file as an input, takes its data and generates an output text file....


Code is running fine for small data tried till x= 10

but while trying to run with large data ie x = 5000000 approx it is giving error
Even the first line of the code is not displayed.
NOTE: variable is declared global but its size is defined in main.

The error that i am getting after approx 2-3 minutes is:

terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Wait Running...Aborted (core dumped)


-----------------------------------------------------------------------
please help and thanks in advance.
 
Old 07-15-2010, 08:08 AM   #2
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
Quote:
Originally Posted by mayankladoia View Post
arr.resize(x,vector<double>(x,0));

but while trying to run with large data ie x = 5000000 approx it is giving error
I'll bet it is.



The long story:

What you're doing is making x vectors, and each one of them gets a different copy of a vector that has enough storage space for x doubles.

On my machine, a double is 8 bytes. 5,000,000 doubles is 8 * 5,000,000 = 40,000,000 bytes = 39,062.5 kB = 38.1 MB. That's the size (the absolute minimum size) of each vector<double>. Make 5,000,000 of those and that's 38.1 * 5,000,000 = 190,500,000 MB = 186,035.1 GB = 181.7 TB.

I'd bet it's actually impossible for you to have that much hard disk space, and I know it's physically impossible for you to have that much RAM.



The short story:

Use a smaller x.

Last edited by JohnGraham; 07-15-2010 at 08:11 AM.
 
1 members found this post helpful.
Old 07-15-2010, 08:29 AM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by JohnGraham View Post
...
The short story:

Use a smaller x.
The story to begin with: watch memory consumption in 'top' or similar.
 
Old 07-16-2010, 03:08 PM   #4
mayankladoia
LQ Newbie
 
Registered: Jul 2010
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Sergei Steshenko View Post
The story to begin with: watch memory consumption in 'top' or similar.
Thanks a lot for your reply it was truely very helpful....
Can you tell me 1 thing is it the RAM size or HDD size on which the size of dynamically allocated memory is directly proportional
 
Old 07-16-2010, 03:40 PM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by mayankladoia View Post
...
Can you tell me 1 thing is it the RAM size or HDD size on which the size of dynamically allocated memory is directly proportional
I don't understand your question.

What entity directly proportional to what other entity ?

Why did you mention HDD size ?
 
Old 07-16-2010, 03:42 PM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Did you read 'man top' and manpages of other utilities mentioned in the end of it ?
 
Old 07-16-2010, 07:54 PM   #7
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by Sergei Steshenko View Post
Why did you mention HDD size ?
If you were using a CPU architecture that allowed that much virtual address space, and you had that much HDD space and you had configured that HDD space as swap partitions, then in theory you could allocate a data structure that big (though not in a reasonable amount of time).

The simple answer could have been that what you were trying is impossible with current hardware. But JohnGraham apparently didn't want to just say "impossible" without considering extreme hardware that might bring it to borderline possible.

Certainly Cern and Google and several other such organizations have no trouble making 200 TB of disk space directly accessible to one computer. That still doesn't get you to the point of being able to take such a crude approach to working on nearly 200TB of data. Cern and Google and others certainly do attack problems that involve data sets that large, but not so crudely. For ordinary computer users that is still an impossible scale of problem.

Last edited by johnsfine; 07-16-2010 at 08:00 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
std::bad_alloc' what(): St9bad_alloc primenu Programming 3 01-29-2010 05:12 AM
terminate all sessions KFC123 Linux - Newbie 3 07-03-2009 02:16 AM
ps does not terminate Lotharster Linux - Newbie 1 02-18-2006 09:03 AM
How to terminate X application? drj000 Linux - Software 1 02-10-2005 07:40 PM
cant make instance of std::locale... azazil Programming 0 02-20-2002 05:39 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration