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

  • SEARCH
  • Home
  • 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 8218811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:55:45+00:00 2026-06-07T12:55:45+00:00

The output of the program below is 1: foo strlen: 3 2: strlen: 0

  • 0

The output of the program below is

1: foo strlen: 3
2:  strlen: 0
3: foo strlen: 3
4: foo strlen: 3
5:  strlen: 0
6:  strlen: 0

I don’t understand

  • why 1 prints the string, but 2 does not and
  • what the difference between the three loops is

The source:

#include "stdafx.h"
#include <map>
#include <string>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    map<string, string> m;
    m["foo"] = "bar";

    const char * s;

    for(map<string, string>::iterator it = m.begin(); it != m.end(); it++)
    {
        pair<string, string> kvPair = *it;
        s = kvPair.first.c_str();
        printf("1: %s strlen: %d\n", s, strlen(s));
        break;
    }
    printf("2: %s strlen: %d\n", s, strlen(s));

    for(map<string, string>::iterator it = m.begin(); it != m.end(); it++)
    {
        s = (*it).first.c_str();
        printf("3: %s strlen: %d\n", s, strlen(s));
        break;
    }
    printf("4: %s strlen: %d\n", s, strlen(s));

    for(map<string, string>::iterator it = m.begin(); it != m.end(); it++)
    {
        s = ((pair<string, string>) (*it)).first.c_str();
        printf("5: %s strlen: %d\n", s, strlen(s));
        break;
    }
    printf("6: %s strlen: %d\n", s, strlen(s));

    return 0;
}

Update An explanation for programmers with little C++ background would be appreciated.

  • 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-07T12:55:49+00:00Added an answer on June 7, 2026 at 12:55 pm

    Partially by chance.

    In 1/2 you create a local variable within the loop, copying the value
    out of the map into kvPair. You set s to point to data in this
    copy. The copy is destroyed (destructor called) when you exit the
    block. By any means: break, goto, an exception, or simply finishing
    the loop body and going through it again—each time through the
    loop, you get a new kvPair, which is destructed at the end of the loop
    body. s points to data inside kvPair.first, and any use of s
    (even simply copying it) after kvPair has been destructed is undefined
    behavior. Anything can happen, and what happens is likely to be
    different depending on the level of debug checking and optimization, or
    even depending on totally unrelated aspects of the program. (If you
    consistently get an empty string, there is probably some poorly designed
    debug checking going on. Well designed debug checking would cause an
    immediate crash, so you would see the error.)

    In 2/3, you initialize s with the actual contents of the map, so it is
    valid until the map is destructed, or the element is removed from the
    map.

    In 4/5, you create a temporary: T( initialization ) constructs a
    temporary of type T, using the initialization given, for any type T,
    including type std::pair<std::string, std::string>. (This isn’t quite
    true; if T is a reference, for example, the behavior is different.)
    And you initialize s to point to data within this temporary. The
    lifetime of a temporary is only to the end of the full expression which
    contains it, so the contents of s become invalid at the semicolon
    ending the statement (which in this case is the end of the full
    expression). As in 1/2, undefined behavior ensues when you use s
    after this.

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

Sidebar

Related Questions

The output of the below program is 24. But I could not understand the
I don't understand the output of this program: int arr[]={1,7,4,2,5,8}; int x=(&(arr[arr[1]-arr[4]])-arr); printf(%d ,x);
In the C program below, I don't understand why buf[0] = 'A' after I
In below program i am getting output as false .But as per my understanding
I tried the below program . INPUT-: i want help Desired OUTPUT-:Words=3 Characters=9 But
I am new to Java. I executed the below program successfully but I don't
I am trying to understand the output of the program printed below. When I
I've a simple but confusing doubt about whether the program below runs in exponential
I am doing rnd for JEXL but i got exception for the below program;
I'm using Netbeans. When I run the program below, I get this as output

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.