Thread: How can I let a function to read an array that is defined in my main function?

  1. #31
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Are you asking if you can push a map into a vector?
    A simple code example might help. It's difficult interpreting what you are asking.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #32
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Elysia View Post
    Are you asking if you can push a map into a vector?
    A simple code example might help. It's difficult interpreting what you are asking.
    Consider the 'scope' class from here:
    Confusing Template error

    I have some global objects of that type.
    Wouldn't it be bad to do the following and expect the map at the back() to be valid at a different scope?
    Code:
    std::map<std::string,std::string > m = {{"foo","bar"},{"xip","bas"}};
    global_object.new_local(m);

  3. #33
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No.
    Why? Because a vector follows an invariant. Whatever you push into that vector must be valid until that element is destroyed or the vector is destroyed.
    Since you push a map into it, it will remain valid until one of those conditions are met.
    This is implemented by a container copying whatever you push into its own storage (hence you need proper copy semantics).
    This is no different from when pushing integers or strings into a vector.

    Beware of pointers, though. Since the value is copied, only the pointer's value persists.
    If you delete what it points to, the pointer will point to an invalid object. But that is a user error, not an error in the vector invariant. It does its job as it should.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #34
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Thanks.. that makes my work a bit simpler.
    (Any special kind of copy/move bug I've to be aware about when changing dynamically containers to 'normal' ones ...in this case ?)

  5. #35
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    As in ... changing vector * myvec to vector myvec or std::vector<T*> to std::vector<T>?
    If the later, just be aware that copies of your objects will be made. To gain efficiency, use move semantics.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #36
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by manasij7479 View Post
    Are you sure about that ?
    Yes I am.

    Then why is it said that C++ does not automatically manage memory ?
    (I've almost never delete `d a container....and AFAIK..that is a common practice)
    Because it still does not manage memory. STL uses an allocator: there are a few standard ones such as alloc.

    The only exception that I know about is std::array, which can be implemented in such a way that it has a do-nothing destructor.
    Last edited by whiteflags; 12-01-2011 at 08:42 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 07-21-2011, 03:26 AM
  2. Passing array back to main function
    By greg677 in forum C Programming
    Replies: 11
    Last Post: 05-01-2010, 04:27 PM
  3. Replies: 14
    Last Post: 03-02-2008, 01:27 PM
  4. dynamically defined array size in a function
    By earth_angel in forum C Programming
    Replies: 21
    Last Post: 05-28-2005, 01:44 AM
  5. Passing a double array from main to another function
    By Loctan in forum C++ Programming
    Replies: 2
    Last Post: 01-04-2005, 05:19 PM