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; Originally Posted by whiteflags I don't change variable names that often but if I need to I would have my ...

  1. #16
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,709
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    >Well... I use C Style arrays only in analogy to pointers...
    >>What do you mean?

    Foo* foo = new Foo[n];
    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. #20
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    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 !



  7. #22
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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);
    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 !



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

  10. #25
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    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?
    jimblumberg likes this.
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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)
    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 !



  12. #27
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    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 !



  14. #29
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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 ?
    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 !



Page 2 of 3 FirstFirst 123 LastLast
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