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 7813213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:46:25+00:00 2026-06-02T04:46:25+00:00

To make this as quick and concise as possible, this is my code: char*

  • 0

To make this as quick and concise as possible, this is my code:

    char* aiMove = getAIMove();
    cout << aiMove;
    cout << "\n" << numMoves << ": " << aiMove << "\n\n";
    return aiMove;

And this is my output:

    a0 a1
    0: �����������������������7

So, the first line calls getAIMove() and assigns the return value (char*) to aiMove.

The second line prints aiMove (a0 a1).

The third line takes numMoves and aiMove into cout and prints it, but it’s printing some strange value instead.

The 4th line returns aiMove, which I’ve inspected to be the strange value printed.

Why has the value of aiMove changed? It seems to only happen when I pass an integer value into cout (in this case, numMoves).

Please help!
Thanks,
Patrick 🙂

edit: another thing that I forgot to mention is that this strange behaviour only happens when this block of code gets executed for the first time, every following time it gets run during the program it prints fine.

  • 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-06-02T04:46:26+00:00Added an answer on June 2, 2026 at 4:46 am

    This is a clear indication that getAIMove returned a pointer to memory that the system felt free to reuse. A subsequent allocation, from either the stack or the heap, overwrote the returned pointer.

    There are lots of ways this can happen, this is probably the most common:

    char *GetAIMove()
    {
        char buf[128];
        strcpy(buf, "a0");
        strcat(buf, " ");
        strcat(buf, "a1");
        return buf; // oops, buf won't exist after we return
    }
    

    Oops. This code returns a pointer to a buffer that ceases to exist as soon as it returns. A typical fix for this issue would be return strdup(buf);. Just remember that the caller of the function needs to free the string when it’s done with it.

    Here’s another way:

    std::string GetAIMove()
    {
     // ...
     return foo;
    }
    
    char* aiMov e= GetAIMove();
    // aiMove points to the contents of the returned string, no longer in scope.
    

    The fix for this is std::string aiMove = GetAIMove. Now aiMove keeps the string in scope.

    But the best fix is to use a string class specifically designed to hold strings all the way through:

    std::string GetAIMove()
    {
        std::string foo;
        foo = "a1";
        foo += " ";
        foo += "a2";
        return foo;
    }
    
    std::string aiMove = GetAIMove();
    

    Note that while this code appears to involve a lot of copying, in practice, modern compilers will make it efficient. So don’t feel bad about keeping your code simple, logical, and easy to understand and maintain.

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

Sidebar

Related Questions

quick question. Is there any way to make this possible? int array [] =
I will make this quick. I'm using Ruby/LDAP to search using my base_dn criteria.
I will make this quick and simple I have a query and I want
I'm fairly new to C# but I will try to make this quick! ;)
Quick question, how can I make this valid : if($this->datos->bathrooms == 1½){$select1 = JText::_(
To make this more clear, I'm going to put code samples: $file = fopen('filename.ext',
I make this program :: #include<stdio.h> char *raw_input(char *msg); main() { char *s; *s
I will try to make this simple and quick.. I have never attempted this
Ill try and make this quick and easy...I hope, Right now I have a
I wrote this quick jquery tab code that also updates the hash location (appended

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.