-1

I just wanted to know of the scope of the STL containers.

For eg. //Have a function which creates an unordered_map and passes to set_map to fill values in it.

int foo() {
  unorderd_map<char,int>mymap;
  set_map(mymap);
}

set_map (unorderd_map<char,int> mmap){
//...setting values of map
}

In this case will the scope of mymap in foo be limited to the function foo() only or mymap is passed by reference to set_map() and whatever changes done in set_map will be reflected to mymap in foo() ?

I also wanted to know how are still containers passed as function parameters, i.e. are they passed by value or they are passed by reference.

Thank You

Harsh M. Shah
  • 613
  • 1
  • 6
  • 6
  • Are you aware, that c++ doesn't have separate reference and value types? So it's not a question of the type, but how you declare the parameters. – MikeMB Mar 20 '15 at 23:27

2 Answers2

2

mymap is passed as a copy to set_map, so set_map only sees its own copy of the map, not the original mymap. Changes made in set_map will be only applied to the copy, and will not affect the original mymap.

To pass the map by reference you need to declare it explicitly:

set_map (unordered_map<char,int>& mmap)
                             // ^ will be passed by reference now.

Now the changes in set_map will alter the original object that was passed as the parameter.

Emil Laine
  • 41,598
  • 9
  • 101
  • 157
  • Thank You for your reply zenith. So when I create unordered_map<>mymap in any of the functions its scope is local to that function? Also the could you elaborate on how memory is allocated to that container when I create it. Because I thought that its memory allocation in dynamic and so its scope would be at other functions also? – Harsh M. Shah Mar 20 '15 at 23:32
  • 1
    Yes, objects declared inside a function are scoped to that function. When that function returns, the object gets destroyed (unless the object is returned). Even if you pass the object around by reference inside the function. – Emil Laine Mar 20 '15 at 23:36
  • 1
    `mymap` will be allocated on the [stack](http://stackoverflow.com/q/79923/3425536), so it's not allocated dynamically as you thought. – Emil Laine Mar 20 '15 at 23:37
  • Thanx a lot zenith. I got what you trying to explain. Thanks. – Harsh M. Shah Mar 21 '15 at 01:04
1

You have defined the argument of set_map() as passed by value. So this function will work on its own copy. What is done in it will be lost for the caller (mymap unchanged).

Comments: The container uses dynamic allocation, with the following principles:

  • When you declare a container such as unordered_map in the scope of a function, it's local to that function.
  • When you leave this local scope, all its local objects are destroyed and so is the unordered_map.
  • The allocator will allocate the dynamic elements from the free store. But when the container is destroyed, it will destroy all its elements (and release ther memory).
  • Unless your elements are pointers, or use move, the container works by value.

In C++ you should not worry about how the standard container are implemented, but about the semantic they implement. So regardless of the implementation of allocation, a local object remains local. But you can easily copy it, or pass it by reference, as Zenith already showed.

Christophe
  • 68,716
  • 7
  • 72
  • 138