473,396 Members | 1,724 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,396 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 1345
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Noah Spitzer-Williams | last post by:
Hello guys, I'm itinerating through my array using pointers in this fashion: image is unsigned char image do { cout << "image byte is: " << *image << endl;
8
by: Generic Usenet Account | last post by:
To settle the dispute regarding what happens when an "erase" method is invoked on an STL container (i.e. whether the element is merely removed from the container or whether it also gets deleted in...
9
by: Luke Wu | last post by:
Hello, I'm having some problems understanding 2 dimensional arrays. My problem relates to the following code: #include <stdio.h> #define M 3 #define N 3
26
by: 69dbb24b2db3daad932c457cccfd6 | last post by:
Hello, I have to initialize all elements of a very big float point array to zero. It seems memset(a, 0, len) is faster than a simple loop. I just want to know whether it is safe to do so, since I...
5
by: tricard | last post by:
Good day all, I have created a two dimensional array (matrix for my purposes) whose size is dynamically allocated. (i.e. rowSize and colSize are both taken as input, then malloc() is used to...
8
by: ais523 | last post by:
I use this function that I wrote for inputting strings. It's meant to return a pointer to mallocated memory holding one input string, or 0 on error. (Personally, I prefer to use 0 to NULL when...
8
by: Jared.Holsopple | last post by:
Hi all, I have a dynamically allocated array of doubles in VC++ .NET. When I view the array in the watch window with "arrayName, 10", it displays the correct value for arrayName, but arrayName...
11
by: redefined.horizons | last post by:
First, I would thank all of those that took the time to answer my question about creating an array based on a numeric value stored in a variable. I realize after reading the responses and doing...
29
by: Jon Slaughter | last post by:
Is it safe to remove elements from an array that foreach is working on? (normally this is not the case but not sure in php) If so is there an efficient way to handle it? (I could add the indexes to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.