Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1043891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:44:22+00:00 2026-05-16T15:44:22+00:00

I have a function that creates and insert some numbers in a vector. if(Enemy2.dEnemy==true)

  • 0

I have a function that creates and insert some numbers in a vector.

    if(Enemy2.dEnemy==true)
    {
        pt.y=4;
        pt.x=90;
        pt2.y=4;
        pt2.x=125;
        for(int i=0; i<6; i++)
        {
            Enemy2.vS1Enemy.push_back(pt);
            Enemy2.vS2Enemy.push_back(pt2);
            y-=70;
            pt.y=y;
            pt2.y=y;
        }
        Enemy2.dEnemy=false;
        Enemy3.cEnemy=0;
    }

It should insert 6 numbers in two vectors, the only problem is that it doesn’t – it actually inserts more.

I don’t think the snippet will run unless Enemy2.dEnemy == true, and it won’t stay true for ever.

The first time the snippet runs, then Enemy2.dEnemy is set to false and it shouldn’t run again.

I don’t set Enemy2.dEnemy to true anywhere except when the window is created.

If I insert a break point any where in the snippet, the program will work fine – it will insert ONLY 6 numbers in the two vectors.

Any ideas what’s wrong here?


ok so i did some debugging.
i found that Enemy2.dEnemy=false; is being skipped for some reason.
i tried to do this to see if it was.

 if(Enemy2.dEnemy)
    {
        pt.y=4;
        pt.x=90;
        pt2.y=4;
        pt2.x=125;
        for(int i=0; i<6; i++)
        {
            Enemy2.vS1Enemy.push_back(pt);
            Enemy2.vS2Enemy.push_back(pt2);
            y-=70;
            pt.y=y;
            pt2.y=y;
        }
        TCHAR s[244];
        Enemy2.dEnemy=false;
        if(Enemy2.dEnemy)
        {
          MessageBox(hWnd, _T("0"), _T(""), MB_OK);
        }
        else
        {
            MessageBox(hWnd, _T("1"), _T(""), MB_OK);
        }
        Enemy3.cEnemy=0;
    }

well the message box popped saying 1 and my code worked fine. it seems that Enemy2.dEnemy=false; doesn’t have time to run ;/
blahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblah!
ok i found where is the real problem which was causing to insert more than 6 numbers..
it was where i was asigning Enemy2.dEnemy=true;

if(Enemy2.e1)
{
Enemy2.now=time(NULL);
Enemy2.tEnemy=Enemy2.now+4;
Enemy2.e1=false;
}
if(Enemy2.tEnemy==time(NULL))
{
check=1;
Enemy2.aEnemy=0;
Enemy2.dEnemy=true;
}

the problem seems that the second if runs more than one time, which is weird!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T15:44:23+00:00Added an answer on May 16, 2026 at 3:44 pm

    First things first: get rid of that abominable if (Enemy2.dEnemy == true) – it should be:

    if (Enemy2.dEnemy)
    

    (I also prefer to name my booleans as a readable sentence segments like Enemy2.isABerserker or Enemy3.hasHadLeftLegCutOffThreeInchesBelowTheKnee but that’s just personal preference).

    Other than that, the only thing I can suggest is a threading problem. There’s nothing wrong with that code per se, but there is a window in which two threads could enter the if statement and both start pushing values into your vector.

    In other words, if thread 1 is doing the pushing when thread 2 encounters the if statement, thread 2 will also start pushing values, since thread 1 has yet to set dEnemy to true. And don’t think you can just move the assignment to the top of the if block – that will reduce but not remove the window.

    My advice is to print out the contents of the vectors in the situation where they have more than six entries and that may give a clue as to what’s happened (post the output here if you wish).


    Re your update that the second if below is running twice:

    if(Enemy2.e1)
    {
    Enemy2.now=time(NULL);
    Enemy2.tEnemy=Enemy2.now+4;
    Enemy2.e1=false;
    }
    if(Enemy2.tEnemy==time(NULL))
    {
    check=1;
    Enemy2.aEnemy=0;
    Enemy2.dEnemy=true;
    }
    

    If this code is executed twice in the same second (and that’s not beyond the bounds of possibility), the second if statement will run twice.

    That’s because time(NULL) give you the number of seconds since the epoch so, until that second is over, you may well be executing the contents of that if thousands of times (or more).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.