# std::cout can't handle an int???

## Recommended Posts

In spite of the fact that I've done it many, many times before (even in the same program), std::cout breaks ("unhandled exception" in malloc.c) when I pass it an integer variable.
/* Main.cpp */
#include "VirtualMachine.h"
#include <iostream>

using std::cout;
using std::cin;

void main()
{   CVirtualMachine vm;
CScript::InstrList iList; // typedef for std::vector<char>

// ...
// Fill iList with instructions
// ...

// As a test, I added these two lines (uncommented), and THAT error vanished,
// only to be replaced with one reading "DAMAGE: after Normal block (#127)
// at 0x002F13F0.", which breaks in dbgheap.c as the program exits
// int tmp = 0;
// cout << tmp;

// turn the list into a script
CScript Test(iList, 4);

// run script
vm.Execute(Index);

// dump data to std::cout for debugging
vm.ExposeVariableState(); // This function never returns
}

//////////////////////////////////////
/*  VirtualMachine.cpp */
void CVirtualMachine::ExposeVariableState() const
{   CScriptState::VarList_cIter cIter;
int n = 0;  // used to denote indexed position of value
for (cIter = _CurrentState.DataArray().begin(); cIter != _CurrentState.DataArray().end(); cIter++, n++)
{   cout << n; // Seems to cause the problem
cout << ": ";
cout << static_cast<int>(*cIter);   // cast for numeric value
cout << endl;
}
}



##### Share on other sites
tried casting to an int* then dereferencing? iterators can play nasty tricks on you.

int* temp = static_cast<int*>( &(*cIter) );std::cout << *temp << std::endl;

just a stab in the dark...

##### Share on other sites
silvermace:
I tried your suggestion, but then it returns "error C2440: 'static_cast' : cannot convert from 'const char *__w64 ' to 'int *'". I know what you mean about iterators, but the abilities of the STL are worth it.

I did a bit more searching and noticed this error from the VS.NET Output Window: "HEAP[ScriptTest.exe]: HEAP: Free Heap block 2f3a18 modified at 2f3a54 after it was freed." It doesn't mean a whole lot to me, but it sounds like it might be causing the problem. Perhaps someone could be kind enough to at least translate it for me? I don't mind fixing my own problems, but this message doesn't tell me much.

Any help would be appreciated.

##### Share on other sites
Quote:
 Original post by dragonknightxI did a bit more searching and noticed this error from the VS.NET Output Window: "HEAP[ScriptTest.exe]: HEAP: Free Heap block 2f3a18 modified at 2f3a54 after it was freed."

Once you've freed (or deleted) a block of memory, you are not allowed to access it, much less modify it.

Quote:
 "DAMAGE: after Normal block (#127) at 0x002F13F0.", which breaks in dbgheap.c as the program exits

When you dynamically allocate an array, your compiler adds a small header before the pointer it returns, containing information about the block (e.g. the number of objects it contains, so that delete[] can find them all). If you overwrite it, Bad Things™ happen when you finally delete the block.

I bet you are writing past the bounds of the array you allocated immediately before the array that causes the error (and thus is likely to be located before it in memory).

##### Share on other sites
Might want to check the behaviour of the _CurrentState.DataArray() method. If this function returns a copy of a container, instead of a reference, then your interators wont behave properly. The container they were iterating over wont exist any more.

##### Share on other sites
I'm not really sure what I did, but it seems as though those errors are gone (at least for now). All I can do is venture a guess that rebuilding the project (deleting the intermediate files) solved the problem. Now std::vector is throwing me an "out-of-bounds" exception to deal with, but at least I know where to start tracking this one, so it shouldn't be 'too' hard.

In spite of the rather anti-climatic 'solution', I still appreciate the help. Thanks again! [smile]

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628275
• Total Posts
2981743

• 10
• 11
• 17
• 10
• 9