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

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by whiteflags View Post
    I don't change variable names that often but if I need to I would have my editor help me. I don't agree with bringing in a complex feature when there is something easier to use available. To wit, I'm not sure how much help it is when you have to explicitly instantiate the template. So really, saying that you've moved the problem to the template is the way to look at it.
    Explicitly instantiate?
    The above example I posted clearly shows how to use it and how it's more fool-proof than passing in size.

    Quote Originally Posted by manasij7479 View Post
    Well... I use C Style arrays only in analogy to pointers...
    What do you mean?
    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. #17
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by Elysia View Post
    Explicitly instantiate?
    The above example I posted clearly shows how to use it and how it's more fool-proof than passing in size.
    Mm, well I'm not sure it still works if what you pass is an alias.

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, obviously, this kind of thing won't work:
    Code:
    #include <iostream>
     
    template<size_t N>
    void foo(int (&bar)[N])
    {
        std::cout << N << std::endl;
    }
     
    int main()
    {
        int bla[50];
        int* r = bla;
        foo(*r);
    }
    But as long as you keep the type information through proper references, it will work.

    Though, I am not entirely sure of what scenario you had in mind.
    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. #19
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    >Well... I use C Style arrays only in analogy to pointers...
    >>What do you mean?

    Foo* foo = new Foo[n];

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But what was your point? How do you relate it to the ongoing discussion?
    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. #21
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Elysia View Post
    How do you relate it to the ongoing discussion?
    You can't use templates in this scenario...and need some stuff created on the heap...(So.. using containers would be a bit kludgy here)


    I stated it before the ongoing discussion about instantiating the function explicitly.

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But you can use std::vector. So why don't you?
    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.

  8. #23
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Elysia View Post
    But you can use std::vector. So why don't you?
    Eyesore ...your logic !
    Code:
    Foo* foo = new Foo[n];
    vs
    Code:
    std::vector<Foo>* vfoo = new std::vector<Foo>();
    vfoo->reserve(n);

  9. #24
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    vector as well as most STL containers (except array in fact) use the heap.

  10. #25
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by manasij7479 View Post
    Eyesore ...your logic !
    You want an array of n elements, so why don't you just do

    std::vector v(n);

    Or just push n items into a vector?
    Why the need for new at all?
    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.

  11. #26
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by whiteflags View Post
    vector as well as most STL containers (except array in fact) use the heap.
    Are you sure about that ?
    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)

  12. #27
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    C++ does not manage memory in the sense that what you new, you must delete (though there are tools that help you do that, such as smart pointers).
    If you use containers, they manage that memory for you. There is almost never any need to allocate a container on the heap.
    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.

  13. #28
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Elysia View Post
    You want an array of n elements, so why don't you just do

    std::vector v(n);

    Or just push n items into a vector?
    Why the need for new at all?
    I didn't know...and am still sceptical ..that std::vectors are created on the heap.

  14. #29
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    std::vector and almost all other containers need to dynamically allocate memory. Hence, the data they store is often allocated on the heap. The vector itself is up to you to place (ie, on the stack or the heap).
    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.

  15. #30
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Elysia View Post
    There is almost never any need to allocate a container on the heap.
    When there is?
    Suppose...
    As of my current project: I have a class that privately stores a std::vector of std::map `s ...of which 3 Global Objects are declared to be used from all the other modules somewhat like environment variables.
    Whenever I am adding a map to one of those, I'm allocating ..and then passing its value to the relevant function to 'push' it.
    I'm not sure.. but would it work if I just create a map and pass it ?

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