Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: strangest error with std::cout  (Read 2946 times)

0 Members and 1 Guest are viewing this topic.

mvl

  • Newbie
  • *
  • Posts: 35
    • View Profile
    • Email
strangest error with std::cout
« on: February 17, 2014, 03:32:09 pm »
okey i am making an library where it is possible to bind a string to a key
take a look at the following function
void InputStates::createMap(const std::string& map,const KeyMapping& key)
    {
       
        std::cout << std::endl;
       
        std::unordered_map<std::string, KeyMapping>::iterator i= hasValue(key);
        //std::cout << "create map 2" << std::endl;
        if(i != mKeyMap.end())
        {
                //std::cout << "create map 4" << std::endl;
            KeyMapping& tmp = mKeyMap[map];
            //std::cout << "step 1" << std::endl;
            mKeyMap[map] = key;
            //std::cout << "step 2" << std::endl;
            (*i).second = tmp;
            //std::cout << "create map 5" << std::endl;
        }else
        {
                                       
                //std::cout << "create map 3" << std::endl;
            mKeyMap.insert(std::make_pair(map, key));
        }
    }
 

nothing looks wrong does it?
it works perfectly until i remove or comment out the std::cout << std::endl;
at the begining of the function it chrashes the moment the function is called
so please help this is really the strangest problem i have ever seen

amir ramezani

  • Jr. Member
  • **
  • Posts: 81
  • i'm a programmer who can't see well
    • View Profile
    • download useful software!
    • Email
Re: strangest error with std::cout
« Reply #1 on: February 17, 2014, 03:35:51 pm »
try using \n inside quotations instead of using std::endl and check if there is any error
and at last, you haven't say what error you've got
if you can't see well, you can't test your applications and operating system well
my game engine:
allegro game creator
my operating system:
AmirOS

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
AW: Re: strangest error with std::cout
« Reply #2 on: February 17, 2014, 05:45:32 pm »
try using n inside quotations instead of using std::endl
Not a good idea, since std::endl actually flushes the buffer.

The fact that it crashes when commentong something out, sounds like a stack corruption. You should debug your code and make sure no iterators/idices are stepping over the max size.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

mvl

  • Newbie
  • *
  • Posts: 35
    • View Profile
    • Email
Re: strangest error with std::cout
« Reply #4 on: February 18, 2014, 09:43:04 am »
ok i foud out it had nothing to do with the function i posted it was in the following function i called
the hasValue function

std::unordered_map<std::string, KeyMapping>::iterator InputStates::hasValue(KeyMapping key)
    {
        for(std::unordered_map<std::string,KeyMapping>::iterator it = mKeyMap.begin();it != mKeyMap.end();it++)
        {
            if((*it).second == key)
            {
                return it;
            }
        }
        return mKeyMap.end();
    }
 

i accedently put the "return mKeyMap.end();"
after the if instead of after the for loop

but can anyone explain why this error was evaded after i put a std::cout in front it is so strange

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: strangest error with std::cout
« Reply #5 on: February 18, 2014, 10:20:00 am »
Undefined behavior. As implied by the name, it can have arbitrary, non-intuitive effects that appear far from the place where the error initially occurred.

Increase the warning level of your compiler so that you notice when some return paths are not covered.

By the way, is there a reason why you don't use auto to declare the iterator? Or it->second instead of (*it).second?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

mvl

  • Newbie
  • *
  • Posts: 35
    • View Profile
    • Email
Re: strangest error with std::cout
« Reply #6 on: February 18, 2014, 11:22:49 am »
thanx I didn't use auto because I am not very used to that idea yet thanx vor the idea I am going to
use it now and I first had it->second but that gave some strange errors so

 

anything