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

is it safe to point to an element in a dynamically allocated array?

P: n/a
Hi everybody!

Just wondering if it is possible to point to variables in the heap.
For example, is this okay?

int * ptr_to_DAIA; // pointer to dynamically allocated integer array
ptr_to_DAIA = new int [SIZE];
for(i=0; i<SIZE; i++) ptr_to_DAIA[i]=i;
// now say I want a pointer that points to the element that contains
value TARGET
int * ptr_to_elem_in_DAIA; // pointer to element in DAIA
for(i=0; i<SIZE; i++) if(ptr_to_DAIA[i]=TARGET)
ptr_to_elem_in_DAIA=&ptr_to_DAIA[i];

In other words, other than the whole bare pointer=bad issue, will this
code be stable? Will ptr_to_elem_in_DAIA always point to the right
place in the heap? Are there any subtle issues I should worry about
here?

Thanx.

Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
so******@gmail.com wrote:
Hi everybody!

Just wondering if it is possible to point to variables in the heap.
It seems that by "heap", you mean the free store. "heap" has a different
meaning in C++. And there are no variables on the free store, since a
variable has a name and dynamically allocated objects don't.
For example, is this okay?

int * ptr_to_DAIA; // pointer to dynamically allocated integer array
ptr_to_DAIA = new int [SIZE];
for(i=0; i<SIZE; i++) ptr_to_DAIA[i]=i;
// now say I want a pointer that points to the element that contains
value TARGET
int * ptr_to_elem_in_DAIA; // pointer to element in DAIA
for(i=0; i<SIZE; i++) if(ptr_to_DAIA[i]=TARGET)
This is an assignment, not a comparison, which seems to be what you really
wanted.
ptr_to_elem_in_DAIA=&ptr_to_DAIA[i];

In other words, other than the whole bare pointer=bad issue, will this
code be stable? Will ptr_to_elem_in_DAIA always point to the right
place in the heap?


No. If there is no element with the same value as TARGET in the array,
ptr_to_elem_in_DAIA will be uninitialized. But other than that, it's ok.
Btw, I'd use the standard find algorithm for this:

ptr_to_elem_in_DAIA = std::find(ptr_to_DAIA, ptr_to_DAIA + SIZE, TARGET);

Jul 23 '05 #2

P: n/a
On 2005-06-14 15:25:54 -0400, so******@gmail.com said:
Hi everybody!

Just wondering if it is possible to point to variables in the heap.
For example, is this okay?
int * ptr_to_DAIA; // pointer to dynamically allocated integer array
ptr_to_DAIA = new int [SIZE];
for(i=0; i<SIZE; i++) ptr_to_DAIA[i]=i;
// now say I want a pointer that points to the element that contains
value TARGET
int * ptr_to_elem_in_DAIA; // pointer to element in DAIA
for(i=0; i<SIZE; i++) if(ptr_to_DAIA[i]=TARGET)
ptr_to_elem_in_DAIA=&ptr_to_DAIA[i];

In other words, other than the whole bare pointer=bad issue, will this
code be stable?

Yes, at least until ptr_to_DAIA is delete'd (and assuming that TARGET
was actually found between ptr_to_DAIA and ptr_to_DAIA+SIZE).

Your code/question essentially boils down to:

int *ptr = new int[SIZE];
int *ptr2 = ptr + (some integer between zero and SIZE);

If that didn't work, the entire language would fall apart :)

--
Clark S. Cox, III
cl*******@gmail.com

Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.