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

Home Posts Topics Members FAQ

Pointer to the penultimate element in a list

Hi All,

I want to set a pointer to the penultimate element in a std::list, what
is the best way of doing this? Also would setting a pointer to the end
of the list, and then adding another element, also result in a valid
pointer to the then penultimate element of the the list? (i.e. achieving
basically the same thing)

I ask this because I know there is problems with setting pointers with
std::vector then adding elements to the vector. From my experience this
scenario results in an invalid pointer, but I have in my mind that lists
do not suffer the same problem.

Adam
Jul 23 '05 #1
5 3035
Adam Hartshorne wrote:
I want to set a pointer to the penultimate element in a std::list, what
is the best way of doing this?
list<myelementt ype>::iterator r = mylist.rbegin() ;
myelementtype *ppen = &(*(++r));
Also would setting a pointer to the end
of the list
I presume you mean "to the last element of the list"...
, and then adding another element, also result in a valid
pointer to the then penultimate element of the the list? (i.e. achieving
basically the same thing)
Yes.
I ask this because I know there is problems with setting pointers with
std::vector then adding elements to the vector. From my experience this
scenario results in an invalid pointer, but I have in my mind that lists
do not suffer the same problem.


For a list the pointer remains valid. For a vector the pointer is
invalidated only if reallocation occurs, you cannot predict it, but you
can try to avoid it.

V
Jul 23 '05 #2

"Adam Hartshorne" <or********@yah oo.com> wrote in message
news:d4******** **@wisteria.csv .warwick.ac.uk. ..
| Hi All,
|
| I want to set a pointer to the penultimate element in a std::list, what
| is the best way of doing this? Also would setting a pointer to the end
| of the list, and then adding another element, also result in a valid
| pointer to the then penultimate element of the the list? (i.e. achieving
| basically the same thing)
|
| I ask this because I know there is problems with setting pointers with
| std::vector then adding elements to the vector. From my experience this
| scenario results in an invalid pointer, but I have in my mind that lists
| do not suffer the same problem.

How about something like this ?:

typedef std::list<int> IntList;
IntList::revers e_iterator GetPenultimate( IntList& L )
{
return (L.rbegin() != L.rend()) ? ++L.rbegin() : L.rend();
}

Also look up 'std::advance() '

Cheers,
Chris Val
Jul 23 '05 #3
> I want to set a pointer to the penultimate element in a std::list, what is
the best way of doing this?
If you actually want the last real element in a list (ie the one before
end()), then
back() returns a reference to it (vector and deque also have back() as a
member).
So with

list <int > l;

int * pi = &l.back();

assuming that l does have some elements in it.
Also would setting a pointer to the end of the list, and then adding
another element, also result in a valid pointer to the then penultimate
element of the the list? (i.e. achieving basically the same thing)
No.
I ask this because I know there is problems with setting pointers with
std::vector then adding elements to the vector. From my experience this
scenario results in an invalid pointer, but I have in my mind that lists
do not suffer the same problem.


Store indexes rather than pointers. At any time you can convert an index to
a pointer.
&v[index] is your pointer.
This way, even if vector reallocates internally, the computed pointer is
still valid (providing index is valid).

Stephen Howe

Jul 23 '05 #4
Victor Bazarov wrote:
Adam Hartshorne wrote:
I ask this because I know there is problems with setting
pointers with std::vector then adding elements to the vector.
From my experience this scenario results in an invalid pointer,
but I have in my mind that lists do not suffer the same problem.


For a list the pointer remains valid. For a vector the pointer is
invalidated only if reallocation occurs, you cannot predict it, but
you can try to avoid it.


Vector reallocation won't occur unless you add elements beyond
the capacity(), right? This seems predictable.

Jul 23 '05 #5
Old Wolf wrote:
Victor Bazarov wrote:
Adam Hartshorne wrote:
I ask this because I know there is problems with setting
pointers with std::vector then adding elements to the vector.
From my experience this scenario results in an invalid pointer,
but I have in my mind that lists do not suffer the same problem.


For a list the pointer remains valid. For a vector the pointer is
invalidated only if reallocation occurs, you cannot predict it, but
you can try to avoid it.


Vector reallocation won't occur unless you add elements beyond
the capacity(), right? This seems predictable.


True. So, if there were a mechanism allowing you to be warned when
allocation is about to happen, you'd know about it. Alternatively,
you can poll the vector's capacity every time you (or somebody else
in their code where you passed your vector) is about to insert a new
element...
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3605
by: JS | last post by:
I have a file called test.c. There I create a pointer to a pcb struct: struct pcb {   void *(*start_routine) (void *);   void *arg;   jmp_buf state;   int    stack; };   struct pcb *pcb_pointer;
2
1539
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ---------------------------------------------- //example 1: typedef int t_Array; int main(int argc, char* argv)
4
4037
by: Daniel Marques | last post by:
I would like to know how to get the address of a container of the STL without using iterators. I have a class with three STL lists and a index which points to an element of any ot the other lists, but i don't know how to access these elements addresses. Just as an example: class C { public: int a; }
10
2214
by: haomiao | last post by:
I want to implement a common list that can cantain any type of data, so I declare the list as (briefly) --------------------------------------- struct list { int data_size; int node_num; char nodes; //will be list_node1,list_node2... };
3
1753
by: Peter Oliphant | last post by:
Below are the definitions of two classes. VList creates two static integer arrays (v_0, v_1), creates an array of pointers to these arrays (vlist), and has a public method to return a pointer to the corresponding integer array based on its index into vlist (Element( index )) . MyClass contains an instance of VList, and then tries to call Element(1) and get a pointer to the appropriate integer list (v_1). But what comes out the debugger...
10
10510
by: Aditya | last post by:
Hi All, I would like to know how it is possible to insert a node in a linked list without using a temp_pointer. If the element is the first element then there is no problem but if it is in between then what is the best solution.
4
1823
by: Encrypted | last post by:
i hav coded a matrix class and its member functions (many of them are overloaded ones)...now in a function..for eg. function of matrix addition.. after the addition i want to return the local object "result" through a pointer (and not call by value)..however i aint getting d values of the result in main()...instead i am getting garbage values when i print it... here is my code : #include <iostream.h> #include <stdio.h> #include...
4
1695
by: ctx2002 | last post by:
hi guys: I am reading Sqlite code at moment, my c language skill not good enough , hope some one here can help me. In function listAdd(PagerLruList *pList, PagerLruLink *pLink, PgHdr *pPg); there is a line: int iOff = (char *)pLink - (char *)pPg;
9
1725
by: Richard | last post by:
I'm still battling with this causing UDB: while(e-- s); if s points to the start of a string and e becomes less than s then e is not really pointing to defined char. Fine. But UDB? Yes, e has an UDV (undefined value) but would this really cause a
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
10436
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...
1
10163
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,...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9040
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
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
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3722
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.