473,795 Members | 3,441 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_DA IA[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 1377
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_DA IA[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_t o_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_DA IA[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+SIZ E).

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
6569
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
2217
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 the process), I looked up the STL code. Erase certainly does not delete the memory associated with the element. However, it appears that the destructor on the element is invoked. I wonder why it has to be this way. In my opinion, this renders...
9
2318
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
26228
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 know it's danger to initialize NULL pointers this way. But how about floats? Zhang Le
5
1820
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 dynamically allocate the required memory). After the matrix is returned to main I want to pass it to a function, printMatrix() and have it displayed on screen. However, I do not want to send the rowSize and colSize arguments; instead I want to have...
8
2686
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 returning null pointers.) It looks pretty watertight to me, but my version of lint complains about use of deallocated pointers, etc. Is this code completely safe on all input, or have I missed something? /* Header files included in the program...
8
1595
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 through arrayName are all zeros. Note that arrayName is actually double*, and not double. When I print out the array to the screen using printf, the correct (non-zero) values are displayed. I'd rather not dump a bunch of
11
4720
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 some more research, that what I really need is known in C as a "dynamic array". Basically, you surpass the array notation and use pointers with memory obtained with malloc() or calloc(). I think this will do just what I needed. That has brought...
29
4257
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 a temp array and delete afterwards if necessary but since I'm actually working in a nested situation this could get a little messy. I guess I could set there values to null and remove them afterwards? Thanks, Jon
0
9672
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10437
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10214
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10164
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3723
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.