will memory be reserved for this object?

Discussion in 'C++' started by Good Guy, Oct 20, 2010.

  1. Good Guy

    Good Guy Guest

    I have following two classes:

    template <size_t size>
    class Cont{
    public:
    char charArray[size];
    };
    template <size_t size>
    class ArrayToUse{
    public:
    Cont<size> container;
    inline ArrayToUse(const Cont<size+1> &
    input):container(reinterpret_cast<const Cont<size> &>(input)){}
    };

    I have two following lines of code at global scope:

    const Cont<12> container={"hello world"};
    ArrayToUse<11> temp(container);
    char (&charArray)[11]=temp.container.charArray;

    In totality of my code The only usage of "container" object is for
    initialization of an object of "ArrayToUse" class as mentioned and
    after initialization of "charArray" reference to
    "temp.container.charArray" I'll use that reference in rest of my code,
    now I'm wondering whether after building the code memory will be
    reserved for "container" object or not since that's got a temporary
    usage.
     
    Good Guy, Oct 20, 2010
    #1
    1. Advertising

  2. Good Guy

    Helge Kruse Guest

    "Good Guy" <> wrote in message
    news:...
    >
    > I have two following lines of code at global scope:
    >
    > const Cont<12> container={"hello world"};
    > ArrayToUse<11> temp(container);
    > char (&charArray)[11]=temp.container.charArray;
    >
    > In totality of my code The only usage of "container" object is for
    > initialization of an object of "ArrayToUse" class as mentioned and
    > after initialization of "charArray" reference to
    > "temp.container.charArray" I'll use that reference in rest of my code,
    > now I'm wondering whether after building the code memory will be
    > reserved for "container" object or not since that's got a temporary
    > usage.


    Since it's a global object, the object's life time is not limited.

    You can define in any other compilation unit "extern const Cont<12>
    container;" and reference this object. The compiler cannot know this while
    compiling the compilation unit where you define container, temp and
    charArray.

    Helge
     
    Helge Kruse, Oct 20, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jose luis fernandez diaz

    Amount of heap memory reserved by a procces

    jose luis fernandez diaz, Feb 9, 2004, in forum: C++
    Replies:
    2
    Views:
    392
    Michael Groys
    Feb 9, 2004
  2. metaperl
    Replies:
    32
    Views:
    937
    NickC
    Sep 15, 2006
  3. Delaney, Timothy (Tim)
    Replies:
    10
    Views:
    688
    Jason
    Sep 14, 2006
  4. Steve Holden
    Replies:
    0
    Views:
    403
    Steve Holden
    Sep 13, 2006
  5. Replies:
    3
    Views:
    188
    osmium
    Nov 6, 2013
Loading...

Share This Page