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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:41:36+00:00 2026-05-29T07:41:36+00:00

Start with the code: #include <iostream> #include <string> #include <map> #include <boost/asio.hpp> typedef std::map<boost::asio::ip::address,

  • 0

Start with the code:

#include <iostream>
#include <string>
#include <map>

#include <boost/asio.hpp>

typedef std::map<boost::asio::ip::address, int> Ip2Int;
Ip2Int ip2int;

void
func1()
{
    boost::asio::ip::address addr4 = boost::asio::ip::address::from_string("192.168.2.1");
    boost::asio::ip::address addr6 = boost::asio::ip::address::from_string("de::ad");

    ip2int.insert(std::pair<boost::asio::ip::address, int>(addr4, 1));
    ip2int.insert(std::pair<boost::asio::ip::address, int>(addr6, 2));
}

int
main()
{
    func1();

    Ip2Int::iterator iter = ip2int.begin();
    do {
        std::cout << iter->first << " -> " << iter->second << std::endl;
    } while (++iter != ip2int.end());

    return 0;
}

I am learning C++ and the above snippet of code has me confused. In func1 the allocation of addr4 and addr6 are stack allocations (right?). When func1 exists they should be gone(-ish, the memory will hold the value until something else uses it). This originally made me think that my walk of the ip2int map could print garbage. I was never able to make this happen though.

Since I still new to C++ I am not ruling out that I missing something. Does a copy happen somewhere that I am unaware of? I thought both the pair and the map insert calls are just making references. Which should mean they could refer to garbage at some point.

Ok, enough rambling. Is the above code somehow valid or am I just getting lucky and nothing else is coming along to use the memory that was storing addr4 and addr6?

thanks in advance for any and all help

  • 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-29T07:41:37+00:00Added an answer on May 29, 2026 at 7:41 am

    Yes, a copy is made when you do ip2int.insert<..>(..).

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

Sidebar

Related Questions

I have this simple code: #include <iostream> #include <fstream> using namespace std; int main(void)
The sample I'm looking at in full is: #include <boost/asio.hpp> #include <boost/bind.hpp> #include <boost/enable_shared_from_this.hpp>
I have some pretty basic C++ code: #include <iostream> #include <string.h> #include <msclr\/marshal_cppstd.h> #using
I think I'll get right into it and start with the code: #include <iostream>
I have the following code #include <iostream> #include <cstddef> #include <string> #include <memory> class
void main() { int xyz = 123; // original value { // code block
I recently updated to the new version of XCode and the session start code
I would like to start making code patches to Rails. Are there any good
I would like to know how to start and code a thread manager for
I found this via google: http://www.mvps.org/access/api/api0008.htm '******************** Code Start ************************** ' This code was

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.