By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,945 Members | 1,307 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,945 IT Pros & Developers. It's quick & easy.

Global Pointers

P: n/a
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

Nov 22 '05 #1
Share this Question
Share on Google+
9 Replies


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

<fo******@gmail.com> ????
news:11**********************@f14g2000cwb.googlegr oups.com...
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

Nov 22 '05 #2

P: n/a
What can i do to fix it?
Are global pointers that use the heap possible?

Thanks

Nov 22 '05 #3

P: n/a
fo******@gmail.com wrote:
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]; // replace the two lines above with.
int * ptr( new int [3] );

// However, even better ,,,
#include <vector>
std::vector<int> ptr(3);
main(){
int * ptr;
ptr = new int [3]; ^^^ what's this ? You're leaking the initialization time 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;
}
// if you're using vector
void Test()
{
ptr.clear();
ptr.resize(4);
}

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

Nov 22 '05 #4

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

Nov 22 '05 #5

P: n/a
fo******@gmail.com wrote:
Hello..

Im trying to make a way to have a dynamic array....


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
Nov 22 '05 #6

P: n/a
John Harrison wrote:
fo******@gmail.com wrote:
Hello..

Im trying to make a way to have a dynamic array....

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


Ever ?

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

john

Nov 23 '05 #7

P: n/a

Gianni Mariani wrote in message ...
fo******@gmail.com wrote:
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];

// replace the two lines above with.
int * ptr( new int [3] );

// However, even better ,,,
#include <vector>
std::vector<int> ptr(3);

// >> main(){
int main(){ // as already stated in other post(s).
int * ptr;
ptr = new int [3];

^^^ what's this ? You're leaking the initialization time new int[3]
ptr[2] = 1;
Test();
cout << ptr[3] << endl;
return 0;
}

Add to Gianni's post:
void Test(){
delete[] ptr; 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').
int * ptr; This is another 'error'(see next comment), you are (re)declaring a pointer
with the same name as the global one.
ptr = new int [4]; 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'.
ptr[3] = 10;
delete[] ptr; // for the local pointer.
return; }


// if you're using vector
void Test(){
ptr.clear();
ptr.resize(4);
}


Hope I got that right, corrections are welcome.
--
Bob R
POVrookie
Nov 23 '05 #8

P: n/a

John Harrison wrote in message ...
fo******@gmail.com wrote:
Hello..
Im trying to make a way to have a dynamic array....


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


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
Nov 23 '05 #9

P: n/a

Gianni Mariani wrote:
John Harrison wrote:
fo******@gmail.com wrote:
Hello..

Im trying to make a way to have a dynamic array....

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


Ever ?


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,MyClass))

HTH,
Michiel Salters

Nov 24 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.