Global Pointers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • fonz2591@gmail.com

    Global Pointers

    Hello..

    Im trying to make a way to have a dynamic array....
    I was doing a test with global pointers and the heap. Would someone
    tell me thy this doesn't compile:

    #include <iostream>
    using namespace std;

    void Test();

    int * ptr;
    ptr = new int [3];
    main(){
    int * ptr;
    ptr = new int [3];
    ptr[2] = 1;
    Test();
    cout << ptr[3] << endl;
    return 0;
    }

    void Test(){
    delete[] ptr;
    int * ptr;
    ptr = new int [4];
    ptr[3] = 10;
    }

    I get the error:
    Test.cpp:7: error: expected constructor, destructor, or type conversion
    before '=' token


    Thanks alot

  • vietor.liu

    #2
    Re: Global Pointers

    int * ptr;
    ptr = new int [3];
    this is a execute code,and the c++ compiler can't create initialize code for
    this status

    <fonz2591@gmail .com> ????
    news:1132637450 .540630.277140@ f14g2000cwb.goo glegroups.com.. .[color=blue]
    > Hello..
    >
    > Im trying to make a way to have a dynamic array....
    > I was doing a test with global pointers and the heap. Would someone
    > tell me thy this doesn't compile:
    >
    > #include <iostream>
    > using namespace std;
    >
    > void Test();
    >
    > int * ptr;
    > ptr = new int [3];
    > main(){
    > int * ptr;
    > ptr = new int [3];
    > ptr[2] = 1;
    > Test();
    > cout << ptr[3] << endl;
    > return 0;
    > }
    >
    > void Test(){
    > delete[] ptr;
    > int * ptr;
    > ptr = new int [4];
    > ptr[3] = 10;
    > }
    >
    > I get the error:
    > Test.cpp:7: error: expected constructor, destructor, or type conversion
    > before '=' token
    >
    >
    > Thanks alot
    >[/color]


    Comment

    • fonz2591@gmail.com

      #3
      Re: Global Pointers

      What can i do to fix it?
      Are global pointers that use the heap possible?

      Thanks

      Comment

      • Gianni Mariani

        #4
        Re: Global Pointers

        fonz2591@gmail. com wrote:[color=blue]
        > Hello..
        >
        > Im trying to make a way to have a dynamic array....
        > I was doing a test with global pointers and the heap. Would someone
        > tell me thy this doesn't compile:
        >
        > #include <iostream>
        > using namespace std;
        >
        > void Test();
        >
        > int * ptr;
        > ptr = new int [3];[/color]
        // replace the two lines above with.
        int * ptr( new int [3] );

        // However, even better ,,,
        #include <vector>
        std::vector<int > ptr(3);
        [color=blue]
        > main(){
        > int * ptr;
        > ptr = new int [3];[/color]
        ^^^ what's this ? You're leaking the initialization time new int[3]
        [color=blue]
        > ptr[2] = 1;
        > Test();
        > cout << ptr[3] << endl;
        > return 0;
        > }
        >
        > void Test(){
        > delete[] ptr;
        > int * ptr;
        > ptr = new int [4];
        > ptr[3] = 10;
        > }[/color]

        // if you're using vector
        void Test()
        {
        ptr.clear();
        ptr.resize(4);
        }
        [color=blue]
        >
        > I get the error:
        > Test.cpp:7: error: expected constructor, destructor, or type conversion
        > before '=' token
        >
        >
        > Thanks alot
        >[/color]

        Comment

        • fonz2591@gmail.com

          #5
          Re: Global Pointers

          Wow..
          That worked...
          Thanks alot!
          Sry about that extra thing...
          I was doing some tests, and forgot to delete them...
          Thanks!

          Comment

          • John Harrison

            #6
            Re: Global Pointers

            fonz2591@gmail. com wrote:[color=blue]
            > Hello..
            >
            > Im trying to make a way to have a dynamic array....[/color]

            You shouldn't use pointers and you shouldn't use globals.

            One day you'll see the merit of this. On that day you'll be a better
            programmer.

            john

            Comment

            • Gianni Mariani

              #7
              Re: Global Pointers

              John Harrison wrote:[color=blue]
              > fonz2591@gmail. com wrote:
              >[color=green]
              >> Hello..
              >>
              >> Im trying to make a way to have a dynamic array....[/color]
              >
              >
              > You shouldn't use pointers and you shouldn't use globals.[/color]

              Ever ?
              [color=blue]
              >
              > One day you'll see the merit of this. On that day you'll be a better
              > programmer.
              >
              > john[/color]

              Comment

              • BobR

                #8
                Re: Global Pointers


                Gianni Mariani wrote in message ...[color=blue]
                >fonz2591@gmail .com wrote:[color=green]
                >> Hello..
                >> Im trying to make a way to have a dynamic array....
                >> I was doing a test with global pointers and the heap. Would someone
                >> tell me thy this doesn't compile:
                >>
                >> #include <iostream>
                >> using namespace std;
                >> void Test();
                >>
                >> int * ptr;
                >> ptr = new int [3];[/color]
                >// replace the two lines above with.
                >int * ptr( new int [3] );
                >
                >// However, even better ,,,
                >#include <vector>
                >std::vector<in t> ptr(3);
                >[/color]
                // >> main(){
                int main(){ // as already stated in other post(s).
                [color=blue][color=green]
                >> int * ptr;
                >> ptr = new int [3];[/color]
                >^^^ what's this ? You're leaking the initialization time new int[3]
                >[color=green]
                >> ptr[2] = 1;
                >> Test();
                >> cout << ptr[3] << endl;
                >> return 0;
                >> }
                >>[/color][/color]

                Add to Gianni's post:
                [color=blue][color=green]
                >> void Test(){
                >> delete[] ptr;[/color][/color]
                This deletes the array pointed to by the global 'ptr'. The global pointer is
                still there, and can now be re-assigned to (it wasn't 'const').
                [color=blue][color=green]
                >> int * ptr;[/color][/color]
                This is another 'error'(see next comment), you are (re)declaring a pointer
                with the same name as the global one.
                [color=blue][color=green]
                >> ptr = new int [4];[/color][/color]
                This assigns to the local you just declared, where you need '::ptr' here to
                get to the global. When you leave this function, you have no pointer to the
                memory you just allocated - a memory leak. Another vote *for* 'vector'.
                [color=blue][color=green]
                >> ptr[3] = 10;[/color][/color]

                delete[] ptr; // for the local pointer.
                return;[color=blue][color=green]
                >> }[/color]
                >
                >// if you're using vector
                >void Test(){
                > ptr.clear();
                > ptr.resize(4);
                >}[/color]

                Hope I got that right, corrections are welcome.
                --
                Bob R
                POVrookie


                Comment

                • BobR

                  #9
                  Re: Global Pointers


                  John Harrison wrote in message ...[color=blue]
                  >fonz2591@gmail .com wrote:[color=green]
                  >> Hello..
                  >> Im trying to make a way to have a dynamic array....[/color]
                  >
                  >You shouldn't use pointers and you shouldn't use globals.
                  >
                  >One day you'll see the merit of this. On that day you'll be a better
                  >programmer.
                  >john[/color]

                  So, you are saying we should:

                  int main(){
                  #include <iostream>

                  class MyThing{ /* guts go here*/ };
                  MyThing TheThing;

                  return 0;
                  }

                  Is that going to work in all cases? (Any cases?)
                  <G>
                  --
                  Bob R
                  POVrookie


                  Comment

                  • Michiel.Salters@tomtom.com

                    #10
                    Re: Global Pointers


                    Gianni Mariani wrote:[color=blue]
                    > John Harrison wrote:[color=green]
                    > > fonz2591@gmail. com wrote:
                    > >[color=darkred]
                    > >> Hello..
                    > >>
                    > >> Im trying to make a way to have a dynamic array....[/color]
                    > >
                    > >
                    > > You shouldn't use pointers and you shouldn't use globals.[/color]
                    >
                    > Ever ?[/color]

                    In the context of the question: yes.
                    If you need a dynamic array, use an STL collection. If you need global
                    access via a single instance, don't create a global object but use a
                    singleton
                    or dedicated access functions. (i.e. MyClass GetFoo(int)/void
                    SetFoo(int,MyCl ass))

                    HTH,
                    Michiel Salters

                    Comment

                    Working...