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

Started by
4 comments, last by dragonknightx 19 years, 4 months ago
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);	

    // add script to VM
    size_t Index = vm.AddScript(Test);

    // 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;
    }
}

--------------------------It's called a changeover. The movie goes on, and nobody in the audience has any idea.
Advertisement
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...
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)

http://www.silvermace.com/ -- My personal website
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.
--------------------------It's called a changeover. The movie goes on, and nobody in the audience has any idea.
Quote:Original post by dragonknightx
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."


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).
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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.
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]
--------------------------It's called a changeover. The movie goes on, and nobody in the audience has any idea.

This topic is closed to new replies.

Advertisement