The stack and C++

Started by
9 comments, last by beebs1 16 years, 9 months ago
Hiya, As the stack is limited in size, I guess for large data structures I need to allocate them on the heap instead. Does anyone know what the stack size actually is? I know standard types such as char, int, etc. are fine on the stack, but what about these: // 2 kb buffer std::vector<char> buffer(2048); // 1 Mb buffer std::vector<char> buffer(1048576); Thanks for any help [smile]
Advertisement
The size of an std::vector<char> instance is constant and equal to the compile-time constant sizeof(std::vector<char>).

The memory used by the vector to store its contents is allocated using the allocator provided as template argument. The default one allocates the memory on the heap.
It's defined by the platform/compiler you're using. On the order of 1Mb. While I didn't check the standard, I'd say that C++ assumes unlimited stack.

Don't specifically optimize for a particular size.
Five minutes ago after seeing your post i tried to determine this with visual studio.

char x[1048576];std::cout << x[4];


The above caused stack overflow with the default options in vs2005 in 32bit winxp, but i could allocate 100MB by adjusting the "stack reserved size" from the linker/system options. I guess if you want to use so much space you'd better allocate dynamically on the heap though.
Thanks for the quick replies.

If I can change the topic slightly, I have a queston about exceptions - I know I've posted before about this, but I'm having some trouble getting my head around it.

As an example, I have a function:
unsigned long CalculateCRC32FromFile( const std::wstring strFileName );

If the file can't be opened for some reason, it throws a std::runtime_error, with a descrition saying 'Unable to open file'.

I'm trying to find a good strategy for dealing with such exceptions once they're caught though - I don't want to display it to the user as I've read that what() shouldn't return anything suitable for general users. On the other hand, I need to display something. What do you guys do with exceptions?

Thanks again.
I usually try to recover. So, I could not compute a file CRC, is there something else I can do? Trying the file SHA1 or MD5 instead (although slower, it might work)? Try another file? Use a default value? Silently rewrite the file from a non-cached version or algorithm or attempt to download it? Ignore the CRC failure and load the file anyway?

If there's nothing I can do, I tell the user that the program has failed to perform some operation as part of loading, related to a certain file, and return the user to a previous working state.
Fair enough, thank you.

I guess the user only needs to know generally not went wrong, not specifically.

Thanks again for your replies :)
What does inability to calculate CRC32 from a file mean?

The problem expressed as Black Box problem is this:
[Filename] -> Black Box -> [CRC32 | Error]

How you handle the problem depends on what you're using the black box for.

Problem 1:
- Creating an archive, and storing checksum to make sure data isn't corrupted.
- Failure of any kind is fatal. Exception would be valid here

Problem 2:
- Reading file from archive, with supplied checksum of untampered file.
- Failure of any kind isn't fatal, it's expected. Reason for failure isn't important.

API for Problem 1:
uint32 generateFileCrc( Filename ); // throws exception

If anything whatsoever goes wrong, we're stuck. We can only add this file to archive if we have a valid checksum. While reading a file, many things in many places can go wrong. So exception is suitable.


API for Problem 2:
bool testFileCrc( Filename, Crc );

No exceptions are thrown. We don't care. We also don't care about how the Crc is calculated, as long as end result is valid.

Code re-use. In C++, some people will frown at this, although it's common sight in other languages. Most of the reasoning as to why such approach would be bad has to deal with certain implementation issues, not with language design itself.
bool testFileCrc( Filename, Crc ){  try {    if ( generateFileCrc( Filename ) == Crc ) {      return true;    }  } catch (Exception) { }  return false;}


Ultimately, API should be aimed at what its intended use is.
Thanks. Going back to the vector<char>, I'm using it to read in a file in 2Kb chunks, using the following code:

std::ifstream stream;stream.open( strFileName.c_str(), std::ifstream::in | std::ifstream::binary );std::vector<char> buffer(2048);stream.read( &buffer[0], 2048 );do {    // update the checksum value, using the size of the data read   UpdateChecksum( (char*)&buffer[0], buffer.size(), /* other parameters */ );    // read in the next chunk    stream.read( &buffer[0], 2048 );} while( !stream.eof() );


It doesn't work properly, as buffer.size() will always return 2048, even if less than 2Kb was read. Can anyone suggest a way of doing this?

Thanks.
From here:
Quote:
Calling member gcount after this function the total number of characters read can be obtained.

This topic is closed to new replies.

Advertisement