Global Pointers

F

fonz2591

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
 
V

vietor.liu

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

Gianni Mariani

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 said:
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);
}
 
F

fonz2591

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

John Harrison

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
 
B

BobR

Gianni Mariani wrote in message ...
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').
This is another 'error'(see next comment), you are (re)declaring a pointer
with the same name as the global one.
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'.

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.
 
B

BobR

John Harrison wrote in message ...
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>
 
M

Michiel.Salters

Gianni said:

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top