# The stack and C++

## Recommended Posts

beebs1    398
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]

##### Share on other sites
ToohrVyk    1595
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.

##### Share on other sites
Antheus    2409
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.

##### Share on other sites
D_Tr    362
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.

##### Share on other sites
beebs1    398
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.

##### Share on other sites
ToohrVyk    1595
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.

##### Share on other sites
beebs1    398
Fair enough, thank you.

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

Thanks again for your replies :)

##### Share on other sites
Antheus    2409
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.

##### Share on other sites
beebs1    398
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.

##### Share on other sites
rip-off    10976
From here:
Quote:
 Calling member gcount after this function the total number of characters read can be obtained.

##### Share on other sites
beebs1    398
Thanks once again. Sure enough, in The C++ Library: A Tutorial and Reference, page 609, is gcount(). Slightly annoyed I didn't see that.

Cheers.