Like Tree1Likes

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

This is a discussion on How can I let a function to read an array that is defined in my main function? within the C++ Programming forums, part of the General Programming Boards category; Are you asking if you can push a map into a vector? A simple code example might help. It's difficult ...

  1. #31
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,771
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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);
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #33
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,771
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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 ?)
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #35
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,771
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    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 07:42 PM.

Page 3 of 3 FirstFirst 123
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, 12: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, 04:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21