473,320 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

This Pointer is legal or not?

#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[i] << " ";
}

for(int i=0;i<test.size();i++)
delete test[i];
system("PAUSE");
return EXIT_SUCCESS;
}

Feb 5 '06 #1
11 1592
On 5 Feb 2006 03:36:42 -0800, "morz" <bl*********@gmail.com> wrote:
#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[i] << " ";
}

for(int i=0;i<test.size();i++)
delete test[i];
system("PAUSE");
return EXIT_SUCCESS;
}


Test is not a pointer. It is a vector of pointers. Don't de-reference
it!
Feb 5 '06 #2
morz wrote:
#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[i] << " ";
} 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[i];
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

Feb 5 '06 #3
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[i] << " ";
}

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[i];

system("PAUSE");
return EXIT_SUCCESS;

P/S : sorry for my broken english

Feb 5 '06 #4
W Marsh <wa*********@gmail.com> wrote:
On 5 Feb 2006 03:36:42 -0800, "morz" <bl*********@gmail.com> wrote:
#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[i] << " ";
}

for(int i=0;i<test.size();i++)
delete test[i];
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[i] will first call operator[],
then operator* (see operator precedence).

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

regards
--
jb

(reply address in rot13, unscramble first)
Feb 5 '06 #5
Thank you!

Feb 5 '06 #6
On Sun, 5 Feb 2006 13:38:33 +0100, "Jakob Bieling"
<ar****************@rot13.com> wrote:
W Marsh <wa*********@gmail.com> wrote:
On 5 Feb 2006 03:36:42 -0800, "morz" <bl*********@gmail.com> wrote:
#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[i] << " ";
}

for(int i=0;i<test.size();i++)
delete test[i];
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[i] 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!
Feb 5 '06 #7

"morz" <bl*********@gmail.com> skrev i meddelandet
news:11**********************@o13g2000cwo.googlegr oups.com...
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[i] << " ";
}
Nothing wrong with that. It's just that operator precedence is
confusing.

The OP probably read the code as

(*test)[i] // 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

#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);

}

Feb 5 '06 #8
Thank you Bo Persson,you answered my question.

Feb 5 '06 #9
In article <11*********************@g14g2000cwa.googlegroups. com>,
"morz" <bl*********@gmail.com> wrote:
#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[i] << " ";
}

for(int i=0;i<test.size();i++)
delete test[i];
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...
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Feb 6 '06 #10
Daniel T. wrote:

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.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Feb 7 '06 #11
In article <_a********************@giganews.com>,
Pete Becker <pe********@acm.org> wrote:
Daniel T. wrote:

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.


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

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Feb 7 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Christopher Benson-Manica | last post by:
From FAQ 6.13: int arr; int (*a)=arr; /* legal, but useless */ printf( "%d\n", a ); /* error, bounds of a unspecified */ Are there non-contrived situations where an array of unspecified...
40
by: Ike Naar | last post by:
In K&R "The C++ programming language (2nd ANSI C edition), the reference manual states (paragraphs 7.9 and 7.10) that pointer comparison is undefined for pointers that do not point to the same...
24
by: s.subbarayan | last post by:
Dear all, According to standards is this valid: char TmpPtrWriteBuffer; void* PtrWriteBuffer =(void*) TmpPtrWriteBuffer; I had a debate with my colleagues that anything cant be typecasted to...
21
by: Roman Werpachowski | last post by:
I can't understand this: double * x = new double; const double * p = x; delete p; works. Why am I allowed to delete a const pointer? On the same note: why am I allowed to do this: class...
19
by: Sharath A.V | last post by:
I had an argument with someone on wheather this piece of code can invoke undefined bahaviour. I think it does not invoke any undefined behaviour since there is sufficient memory space of 9...
15
by: khan | last post by:
Hi, I read that pointer representation can non-zero bit pattern, machine specific.Compiler when comes accross value '0' in pointer context, converts it to machine specific null pointer...
6
by: Dave Vandervies | last post by:
I'm writing an abstraction layer over an OS's ability to do non-blocking reads and writes on various things. The basic idea is that the caller will be able to let the I/O happen in the background,...
5
by: Vols | last post by:
class A{ public: int x; }; class B : public A{ public: int y; }; void foo()
3
by: Victor Bazarov | last post by:
jl_post@hotmail.com wrote: Yes, certainly. Actually 'delete' is an operator, not a function. The parens are superfluous. There is really no need for that.
5
by: ramu | last post by:
Hi, Can you please let me know the output of the below code. Regards #include<stdio.h> int main() { int *ptr=0; ptr++;
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.