Re: Why using the new keyword at all for memory allocation?

Discussion in 'C++' started by PencoOdStip@gmail.com, May 23, 2007.

  1. Guest

    By the way.

    Let's say i want this variable to be acessible in all functions.To be
    global.

    By alocating it like that in memory i will have acess to it from all
    functions right? Like that it will be global???

    Is there other way of making variables global?
     
    , May 23, 2007
    #1
    1. Advertising

  2. Jim Langston Guest

    <> wrote in message
    news:...
    > By the way.
    >
    > Let's say i want this variable to be acessible in all functions.To be
    > global.
    >
    > By alocating it like that in memory i will have acess to it from all
    > functions right? Like that it will be global???
    >
    > Is there other way of making variables global?


    To make a variable, any variable, global to all functions in a .cpp file,
    simply declare it ouside of any function.

    To make a variable, any variable, global to all compiliation units (other
    ..cpp files as well) in a header file, or other means, declare the varaible
    as extern and then in one, and only one, .cpp file declare the variable
    normally.

    (Untested code)

    MyProg.cpp:

    int proba[5]
    int* probb;

    void Foo()
    {
    // Foo can "see" proba and probb
    }

    int main()
    {
    probb = new int[10];

    // main can "see" proba and probb
    }

    -----------------------------------------

    MyOtherFile.cpp:

    extern* probb;

    void Bar()
    {
    // Bar can "see" probb, but not proba.
    }
     
    Jim Langston, May 24, 2007
    #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. Gaurav Jain

    Memory allocation using New

    Gaurav Jain, Jun 10, 2005, in forum: C++
    Replies:
    7
    Views:
    579
    Peter Koch Larsen
    Jun 11, 2005
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,072
    Smokey Grindel
    Dec 2, 2006
  3. Ken
    Replies:
    24
    Views:
    3,896
    Ben Bacarisse
    Nov 30, 2006
  4. chris
    Replies:
    6
    Views:
    1,007
    chris
    Oct 28, 2005
  5. Replies:
    6
    Views:
    294
    Stuart Redmann
    May 25, 2007
Loading...

Share This Page