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

S

songfire

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;
// 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=TARGET)
ptr_to_elem_in_DAIA=&ptr_to_DAIA;

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

Clark S. Cox III

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;
// 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=TARGET)
ptr_to_elem_in_DAIA=&ptr_to_DAIA;

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 :)
 
R

Rolf Magnus

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;
// 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=TARGET)


This is an assignment, not a comparison, which seems to be what you really
wanted.
ptr_to_elem_in_DAIA=&ptr_to_DAIA;

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

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top