memory usage

A

Alan

I have a program that bombs with a Windows-generated error
"application has encountered an error"). Is there any easy way to tell
whether or not I`m consuming all the memory, or further diagnose this?

I put in some print statements, and it dies at different points
at different times.

I`m using several vectors, which I have reserve-ed for 1000
elements.

Thanks in advance, Alan
 
I

Ian Collins

Alan said:
I have a program that bombs with a Windows-generated error
"application has encountered an error"). Is there any easy way to tell
whether or not I`m consuming all the memory, or further diagnose this?
Run the program in your debugger.
 
D

Dan Bloomquist

Alan said:
I have a program that bombs with a Windows-generated error
"application has encountered an error"). Is there any easy way to tell
whether or not I`m consuming all the memory, or further diagnose this?

I put in some print statements, and it dies at different points
at different times.

I`m using several vectors, which I have reserve-ed for 1000
elements.

Is it that it doesn't bomb in a debug build?

Best, Dan.
 
A

Alan

No, it`s just my inexperience. I`m using DevC++ and will try to
track down some documentation on the debugger it uses.

Thanks, Alan
 
B

BobR

Alan wrote in message
No, it`s just my inexperience. I`m using DevC++ and will try to
track down some documentation on the debugger it uses.

Thanks, Alan

If you installed the default (MinGW), the debugger is 'gdb'.
Get docs at www.gnu.org
(be sure you get the ones for your version (...../bin/gdb.exe --version).)
 
S

Salt_Peter

Alan said:
I have a program that bombs with a Windows-generated error
"application has encountered an error"). Is there any easy way to tell
whether or not I`m consuming all the memory, or further diagnose this?

I put in some print statements, and it dies at different points
at different times.

I`m using several vectors, which I have reserve-ed for 1000
elements.

Thanks in advance, Alan

Then use a try/catch block to catch any exception that is generated. If
your catch block catches a bad_allocation, then you know that you've
run out of memory. And so on.

#include <iostream>
#include <vector>
#include <stdexcept>

class A { double d; };

int main()
{
try
{
std::vector< A > va(1000);
// throw std::bad_alloc(); // testing
// throw std::runtime_error("testing runtime_error");
}
catch( const std::exception& e )
{
std::cerr << "error: ";
std::cerr << e.what() << std::endl;
}
catch( ... )
{
std::cerr << "unexpected exception caught!\n";
}
return 0;
}

Incidentally, don't use new/delete. Use boost smart_ptr instead.
 
E

eriwik

Alan skrev:
I have a program that bombs with a Windows-generated error
"application has encountered an error"). Is there any easy way to tell
whether or not I`m consuming all the memory, or further diagnose this?

I put in some print statements, and it dies at different points
at different times.

I`m using several vectors, which I have reserve-ed for 1000
elements.

You'll have to work quite hard to consume all your memory, consider
this, if you have a vector with 1000 ints you've only wasted 4KB. Say
you store some instanses of a class instead and I'd say you still
haven't reached 10MB. But you can always check, the easiest way for you
would be to right-click the taskbar and select Task Manager, go to the
Processes-tab and start your app and find it in the list, there you'll
see how much memory it consumes (if it does not crash too fast).

Anyway, debugging is your friend, and as you are using DevC++ and your
debugger is gdb your out of friendliness. I'd suggest switching to MS
Visual C++ Express, it has exelent debugging facilities.
 
A

Alan

I ran this past where I had the original problem (smaller file) but
on a different machine, and it did not crash. I have some additional
things to try now.

Thanks for the advice. Alan
 
A

Alan

This seems like a silly question, but . . . I suppose the try . . .
catch should go around any push_back and erase operations, right?
Should I bother putting it around clear and size methods?
Thanks again, Alan
 
A

Alf P. Steinbach

* Alan:
This seems like a silly question, but . . . I suppose the try . . .
catch should go around any push_back and erase operations, right?
Should I bother putting it around clear and size methods?

No, you're applying try-catch at a much too detailed level of operations.
 
A

Alan

Do you mean I should be placing it around larger blocks of code
rather than isolating single, memory allocation-related statements?
Thanks, Alan
 
B

BobR

Alan wrote in message ...

[ please do not top-post. ] [re-orderef]
Do you mean I should be placing it around larger blocks of code
rather than isolating single, memory allocation-related statements?
Thanks, Alan

If you are looking for an unknown problem, you could put it in main().

#include <stdexcept>
#include <iostream> // etc.
#include <vector>

class BumFaddle{
void funcy(){
try{
throw std::runtime_error("from funcy");
}
catch( std::runtime_error &Re){
std::cerr<<"BumFaddle caught: "<<Re.what()<<std::endl;
throw; // send it on
}
} // funcy()
};

int main(){
try{
// .... code ....
BumFaddle Bf;
Bf.funcy();
std::vector<int> Vint(2);
int num = Vint.at(3); // out_of_range
} // try

// catch( [1] ){
// }

catch( const std::eek:ut_of_range &Oor ){
std::cout<<"caught "<<Oor.what()<<std::endl;
}

catch( const std::exception &e ){ // Salt's example
std::cerr << "error: ";
std::cerr << e.what() << std::endl;
}

// if you put "catch( out_of_range )" here, it will never get
// to it due to the 'higher-up' above.

// catch( [1] ){
// }

catch( ... ){ // catch anything not caught above.
std::cout<<"caught something (maybe the flu!!)"<<std::endl;
} // the 3 fots are really 3 dots, not a place holder.
return 0;
}
// --- sample output ---
// BumFaddle caught: from funcy
// error: from funcy // note how 'exception' caught 'runtime_error'


[1] one of (all std::) :
exception
Base class for all the exceptions thrown by C++ Standard library.
You can call what() and retrieve the optional string with which the
exception was initialized.
logic_error [ Derived from exception. ]
Reports program logic errors.
runtime_error [ Derived from exception. ]
Reports runtime errors.

[ Exception classes derived from logic_error ]
domain_error
invalid_argument
length_error
out_of_range
bad_cast
bad_typeid
[ Exception classes derived from runtime_error ]
range_error
Reports violation of a postcondition.
overflow_error
Reports an arithmetic overflow.
bad_alloc
Reports a failure to allocate storage.

// OR almost anything (including class/struct):

try{
if( true ){ throw "Help Me!!";}
}
catch( char const *r_e ){
std::cout<<"error: " << r_e << std::endl;
}
 
S

Salt_Peter

Alan said:
This seems like a silly question, but . . . I suppose the try . . .
catch should go around any push_back and erase operations, right?
Should I bother putting it around clear and size methods?
Thanks again, Alan

Do what the others here suggested, instead of wrapping every operation
you do on a container in a try-cactch block, keep a universal guarded
block in main(). Simply check for errors occasionally where appropriate
(null pointers, out_of_range situations, etc) by throwing an exception
with meaning.
Example:
checking that a pointer is not null:
if(0 == ptr)
throw std::runtime_error("nullptr detected in ClassA::foo()\n");

You can fairly confident about how the standard library containers
behave if thats what you are using. They are rock solid as long as you
follow the basic rules and the intent of each container. In the case of
vectors, using at() which will throw when out of range, that means you
need to do nothing else but provide try/catch in main().
 
A

Alan

Just for closure, I put in try . . . catch (as I should have
originally) and, of course, had no errors on a different machine. I
suspect that something is amiss with the original PC where I had the
problem, so I`ll run it there to see what happens.


Thanks for all the advice. Alan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top