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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:06:01+00:00 2026-06-14T04:06:01+00:00

I had a weird behavior of a piece code today under windows std::vector<std::string> getMACs()

  • 0

I had a weird behavior of a piece code today under windows

std::vector<std::string> getMACs() {
  std::vector<std::string> macs;
  for(/*something*/) {
    char buffer[100];
    sprintf_s(buffer, size, "get the mac address here");
    std::string s = "";
    s.append(buffer);
    printf("mac=%s\n", s.c_str();  //print the mac address correctly
    macs.push_back(s);
  }
  return macs;
}

int main(int, char**) {
  std::vector<std::string> macs = getMACs();
  for (size_t i = 0; i < mac.size(); i++) {
    printf("mac=%s\n", macs[i]);   //prints garbage
  }
}

although the mac address inside the function has been printed correctly, in the main it prints garbage, the only explanation I have, that the macs vector is full of garbage strings, but how can this happen; a call to string.append(const char*), though passes by reference, the push_back() function should call the copy constructor of string, and so it should not point any more to a string reference that will turn into garbage after leaving the scope, right?

  • 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-14T04:06:03+00:00Added an answer on June 14, 2026 at 4:06 am
    printf("mac=%s\n", macs[i]);   //prints garbage
    

    Because macs[i] is of type std::string, and printf doesn’t know how to handle that. Try this:

    printf("mac=%s\n", macs[i].c_str());
    

    Or this:

    std::cout << "mac=" << macs[i] << '\n';
    

    Type safety, FTW

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

Sidebar

Related Questions

I just had some weird behavior from a version of g++ for Windows that
I had a weird issue arise after using CodeMaid to clean up my code.
I had a weird problem, where I opened up a source code project for
I had a weird problem here. Please check the code below. I do not
Having a weird issue. I'm new to Macs and have a windows VM that
Had this weird bug in my code and I was not able to solve
Few weeks ago, I switched from Java to C#. Today, I had a weird
I had this weird problem a little while ago and haven't been able to
I've always had a weird feeling in my gut about actually deleting rows from
Ok this is a weird problem because I had it working before. So I

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.