472,122 Members | 1,521 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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

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
2 1278
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
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.

Similar topics

10 posts views Thread by Noah Spitzer-Williams | last post: by
26 posts views Thread by 69dbb24b2db3daad932c457cccfd6 | last post: by
8 posts views Thread by ais523 | last post: by
11 posts views Thread by redefined.horizons | last post: by
29 posts views Thread by Jon Slaughter | last post: by
reply views Thread by leo001 | last post: by

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.