473,773 Members | 2,277 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reassigning references (to std::map in this case)...

Hi,

Here's an example of something that feels like it should be OK but
does in fact produce a segfault on every compiler I've tried (VC2005, g
++ 4.1.2/Linux, g++ 3.4.4/Cygwin). The line marked // KABOOM is the
one that segfaults. If I change the references to pointers (and make
the appropriate dereferences) everything works just fine so is this
some finer point of references I'm not grasping or an issue with the
STL ?

Answers gratefully received..

--
Tobe

=============== ==========

#include <map>
#include <string>
#include <stdio.h>

class Entry;
typedef std::map<std::s tring, EntryEntryMap;

class Entry
{
public:
std::string iValue;
EntryMap iEntries;
Entry *iParentEntry;

Entry() { iParentEntry = NULL; }
};

int main(int argc, char* argv[])
{
EntryMap entries;

entries["1"].iEntries["2"].iEntries["3"].iValue = "three";

EntryMap &entriesRef1 = entries["1"].iEntries;
EntryMap &entriesRef2 = entries["1"].iEntries["2"].iEntries;

printf("%s\n", entriesRef2["3"].iValue.c_str() );

entriesRef1 = entriesRef2; // KABOOM
printf("%s\n", entriesRef1["3"].iValue.c_str() );

return 0;
}

Sep 6 '07 #1
6 1395
Tobe wrote:
Here's an example of something that feels like it should be OK but
does in fact produce a segfault on every compiler I've tried (VC2005,
g ++ 4.1.2/Linux, g++ 3.4.4/Cygwin). The line marked // KABOOM is the
one that segfaults. If I change the references to pointers (and make
the appropriate dereferences) everything works just fine so is this
some finer point of references I'm not grasping or an issue with the
STL ?

Answers gratefully received..
NEVER put anything beside your signature after the "-- " (the signature
separator). Now I have to manually drag your source code in here...
EntryMap &entriesRef1 = entries["1"].iEntries;
EntryMap &entriesRef2 = entries["1"].iEntries["2"].iEntries;
So, 'entriesRef2' is a reference to an entry in a member of the
'entriesRef1's referred object.
entriesRef1 = entriesRef2; // KABOOM
What happens? You start overriding 'entries["1"].iEntries' value by
means of assigning to a reference to it. And at the same time you
probably want to continue using it (since the right-hand side still
refers to it). It's not going to work. 'entriesRef2' becomes invalid
as soon as the assignment to 'entriesRef1' begins because assignment
cleans out the contents of 'entries["1"].iEntries' thus deleting what
is behind the 'entriesRef2' along with it.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 6 '07 #2
Victor Bazarov wrote:
Tobe wrote:
Here's an example of something that feels like it should be OK but
does in fact produce a segfault on every compiler I've tried
(VC2005, g ++ 4.1.2/Linux, g++ 3.4.4/Cygwin). The line marked //
KABOOM is the one that segfaults. If I change the references to
pointers (and make the appropriate dereferences) everything works
just fine so is this some finer point of references I'm not
grasping or an issue with the STL ?

Answers gratefully received..

NEVER put anything beside your signature after the "-- " (the
signature separator). Now I have to manually drag your source code
in here...
Actually, he didn't have "-- ", but "--". I assume your newsreader is
not strict on .sig separators.


Brian
Sep 6 '07 #3
On Sep 6, 8:46 pm, "Default User" <defaultuse...@ yahoo.comwrote:
Victor Bazarov wrote:
Tobe wrote:
Here's an example of something that feels like it should be OK but
does in fact produce a segfault on every compiler I've tried
(VC2005, g ++ 4.1.2/Linux, g++ 3.4.4/Cygwin). The line marked //
KABOOM is the one that segfaults. If I change the references to
pointers (and make the appropriate dereferences) everything works
just fine so is this some finer point of references I'm not
grasping or an issue with the STL ?
Answers gratefully received..
NEVER put anything beside your signature after the "-- " (the
signature separator). Now I have to manually drag your source code
in here...
Actually, he didn't have "-- ", but "--". I assume your newsreader is
not strict on .sig separators.
Or yours truncates trailing whitespace in some cases. I see
"-- " with Google news (and I'd be surprised if even Google adds
a trailing white space).

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 7 '07 #4
* James Kanze:
>
I see
"-- " with Google news (and I'd be surprised if even Google adds
a trailing white space).
The original message had only "--".

From earlier discussion of this, it seems that when posting, Google
Groups strips the space at the end of "-- ", because stripping it will
cause most harm and confusion, and from your comment it seems that when
reading, Google Groups adds a space, because that will cause most harm
and confusion. If Google Groups had done nothing, signature delimiters
would work perfectly. They're /actively/ messing them up.

And of course there is no obvious way to report problems with Google
Groups -- you're led on a wild goose tour leading nowhere.
Cheers,

- Alf

CC: Oh, I would CC to Google Groups technical support or customer
service or whatever, if any address was available (of course it isn't).
Sep 7 '07 #5
EntryMap &entriesRef1 = entries["1"].iEntries;
EntryMap &entriesRef2 = entries["1"].iEntries["2"].iEntries;

printf("%s\n", entriesRef2["3"].iValue.c_str() );

entriesRef1 = entriesRef2; // KABOOM
printf("%s\n", entriesRef1["3"].iValue.c_str() );
Your map entriesRef1 contains the map referenced by entriesRef2. When
you assign entriesRef2 to entriesRef1, entriesRef1 will be emptied
first, which will delete entriesRef2, because it is inside
entriesRef1. Then you will try to assign this dangling reference
entriesRef2 into entriesRef1, which fails.

Sep 7 '07 #6
On Sep 7, 11:55 am, "Alf P. Steinbach" <al...@start.no wrote:
* James Kanze:
I see
"-- " with Google news (and I'd be surprised if even Google adds
a trailing white space).
The original message had only "--".
From earlier discussion of this, it seems that when posting, Google
Groups strips the space at the end of "-- ", because stripping it will
cause most harm and confusion, and from your comment it seems that when
reading, Google Groups adds a space, because that will cause most harm
and confusion. If Google Groups had done nothing, signature delimiters
would work perfectly. They're /actively/ messing them up.
Maybe. There are so many intermediaries involved that I'm no
longer sure who's doing what. But it's true that back in the
old days, before Google and IE, things seemed to work better.

FWIW: making trailing spaces significant is a major error in the
specification, since some more exotic systems don't have any
means of maintaining them. (The existance of such systems is
why C and C++ don't guarantee them in text mode.)
And of course there is no obvious way to report problems with Google
Groups -- you're led on a wild goose tour leading nowhere.
I reported a problem once to them, and got a response. I don't
remember where I sent it, but I do know that it took some effort
to find the address, and if I remember correctly, you need a
Google account to access it.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 8 '07 #7

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

Similar topics

10
9958
by: ios | last post by:
Hi Can someone tell me what is different between below case? strcpy(eventname, "MDCX_RSP"); and sprintf(eventname, "MDCX_RSP"); Thanks, Leon
20
5604
by: cylin | last post by:
Dear all, I open a binary file and want to write 0x00040700 to this file. how can I set write buffer? --------------------------------------------------- typedef unsigned char UCHAR; int iFD=open(szFileName,O_CREAT|O_BINARY|O_TRUNC|O_WRONLY,S_IREAD|S_IWRITE); UCHAR buffer; //??????????? write(iFD,buffer,5); ---------------------------------------------------
9
1710
by: | last post by:
Hi, quick question: I have a function which takes a reference to an object as an argument. void foo( vect3 & v ); This works fine: vect3 v1(0.0, 0.0, 0.0); foo(v1);
1
2259
by: Pratchaya | last post by:
Hi, All Can i write php code to connect 2 MySQL DB. like this case. ? My Environment : Server < ---- > PC Client Server =
7
25155
by: gyan | last post by:
follwing code gives error: 1 #include<iostream.h> 2 int main() 3 { 4 int a=5,b; 5 switch(a){ 6 case 1: 7 {b=5; 8 break; 9 }
18
1664
by: howa | last post by:
a simple singleton class (PHP4) which way is preffered? // 1. class Foo { function getFoo() { static $instace; if (!isset($instace) ) {
14
2196
by: George2 | last post by:
Hello everyone, Why visual studio does not optimize constructor in this case? I do not understand what the MSDN mentioned, if use different named object, compiler can not optimize. Why? http://msdn2.microsoft.com/en-us/library/ms364057(vs.80).aspx
7
3951
by: * Tong * | last post by:
Hi, I couldn't figure out how to properly type cast in this case: $ cat -n type_cast.c 1 #include <stdio.h> 2 3 typedef unsigned char Byte; 4 typedef signed char Small_Int; 5
5
1482
by: Juha Nieminen | last post by:
Let's assume we have a class like this: //--------------------------------------------------------- #include <iostream> class MyClass { public: MyClass() { std::cout << "constructor\n"; } ~MyClass() { std::cout << "destructor\n"; }
0
9621
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
10264
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
10039
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
8937
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
7463
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
6717
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();...
1
4012
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3610
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2852
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.