This Pointer is legal or not?

M

morz

#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;


vector<int *> test;

void somefunction(int num)
{

int *p = new int(num);
test.push_back(p);


}

int main(int argc, char *argv[])
{


for(int i=0;i<10;i++)
somefunction(i);

for(int i=0;i<10;i++)
{
cout << *test << " ";
}

for(int i=0;i<test.size();i++)
delete test;


system("PAUSE");
return EXIT_SUCCESS;
}
 
W

W Marsh

#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;


vector<int *> test;

void somefunction(int num)
{

int *p = new int(num);
test.push_back(p);


}

int main(int argc, char *argv[])
{


for(int i=0;i<10;i++)
somefunction(i);

for(int i=0;i<10;i++)
{
cout << *test << " ";
}

for(int i=0;i<test.size();i++)
delete test;


system("PAUSE");
return EXIT_SUCCESS;
}


Test is not a pointer. It is a vector of pointers. Don't de-reference
it!
 
?

=?iso-8859-1?q?Stephan_Br=F6nnimann?=

morz said:
#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;


vector<int *> test;

void somefunction(int num)
{

int *p = new int(num);
test.push_back(p);


}

int main(int argc, char *argv[])
{


for(int i=0;i<10;i++)
somefunction(i);

for(int i=0;i<10;i++)
{
cout << *test << " ";
}

This loop relies on the fact that somefunction() adds an element to
test.
Better use test.size() as loop criteria (same as you did below).
for(int i=0;i<test.size();i++)
delete test;


system("PAUSE");
return EXIT_SUCCESS;
}


And what is your question. I can't see anything wrong in the program,
except the comment I've added above.

Regards, Stephan
 
M

morz

I'm not expert in pointer,mybe i'm wrong,but vector test hold pointer
to int .So,to get value inside it, i must dereferece vector test.

ok,i remove :

for(int i=0;i<10;i++)
{
cout << *test << " ";
}

Actually i just want to know this code below is valid or not because
i'm curious with vector test,is it still hold address of int * p ?
because pointer p is out of scope.
#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

vector<int *> test;

void somefunction(int num)
{

int *p = new int(num);
test.push_back(p);

}

int main(int argc, char *argv[])
{

for(int i=0;i<10;i++)
somefunction(i);

for(int i=0;i<test.size();i++)
delete test;

system("PAUSE");
return EXIT_SUCCESS;

P/S : sorry for my broken english
 
J

Jakob Bieling

W Marsh said:
#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;


vector<int *> test;

void somefunction(int num)
{

int *p = new int(num);
test.push_back(p);


}

int main(int argc, char *argv[])
{


for(int i=0;i<10;i++)
somefunction(i);

for(int i=0;i<10;i++)
{
cout << *test << " ";
}

for(int i=0;i<test.size();i++)
delete test;


system("PAUSE");
return EXIT_SUCCESS;
}


Test is not a pointer. It is a vector of pointers. Don't de-reference
it!


The OP is not derefencing test. *test will first call operator[],
then operator* (see operator precedence).

As for the code, I do not see anything wrong with it at all.

regards
 
W

W Marsh

W Marsh said:
#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;


vector<int *> test;

void somefunction(int num)
{

int *p = new int(num);
test.push_back(p);


}

int main(int argc, char *argv[])
{


for(int i=0;i<10;i++)
somefunction(i);

for(int i=0;i<10;i++)
{
cout << *test << " ";
}

for(int i=0;i<test.size();i++)
delete test;


system("PAUSE");
return EXIT_SUCCESS;
}


Test is not a pointer. It is a vector of pointers. Don't de-reference
it!


The OP is not derefencing test. *test will first call operator[],
then operator* (see operator precedence).

As for the code, I do not see anything wrong with it at all.

regards


Yep, you're entirely right. My eyes skipped the square brackets
entirely. Embarrassing!
 
B

Bo Persson

morz said:
I'm not expert in pointer,mybe i'm wrong,but vector test hold
pointer
to int .So,to get value inside it, i must dereferece vector test.

ok,i remove :

for(int i=0;i<10;i++)
{
cout << *test << " ";
}


Nothing wrong with that. It's just that operator precedence is
confusing.

The OP probably read the code as

(*test) // NOT correct

which is more common, but incorrect here.

Actually i just want to know this code below is valid or not because
i'm curious with vector test,is it still hold address of int * p ?
because pointer p is out of scope.

The vector copies the value of p, so that is not a problem. The
pointer p does disappear at the end of the function, but the copy
inside the vector remains.


Bo Persson
 
D

Daniel T.

"morz said:
#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;


vector<int *> test;

void somefunction(int num)
{

int *p = new int(num);
test.push_back(p);


}

int main(int argc, char *argv[])
{


for(int i=0;i<10;i++)
somefunction(i);

for(int i=0;i<10;i++)
{
cout << *test << " ";
}

for(int i=0;i<test.size();i++)
delete test;


system("PAUSE");
return EXIT_SUCCESS;
}


I do see a problem with the code above. If 'operator new' or
'test.push_back()' should throw, all the pointers loaded in the vector
up to that point will be leaked. You should either use a pointer
wrapper, or wrap 'somefunction' in a try catch block...
 
P

Pete Becker

Daniel said:
I do see a problem with the code above. If 'operator new' or
'test.push_back()' should throw, all the pointers loaded in the vector
up to that point will be leaked. You should either use a pointer
wrapper, or wrap 'somefunction' in a try catch block...

The consequences of the "leak" in this code are ... nothing. Granted, in
a different setting, this might matter. But that's some other code, not
this code.
 
D

Daniel T.

Pete Becker said:
The consequences of the "leak" in this code are ... nothing. Granted, in
a different setting, this might matter. But that's some other code, not
this code.

True, but I thought it was worth mentioning in case the OP was thinking
in a larger context.
 

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
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top