473,378 Members | 1,518 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,378 software developers and data experts.

Regarding STL-Map

Dew
Hi all,
I needed a clue to get rid of this problem. Code work fine for few run
but then fail with some segmentation fault error with next subsequent
run. I am using Visual Age C++ under AIX compiler , version 5

1 //************************************************** ****************8
2 //1. some other stuff goes here
3 //2. some other stuff goes here
4 //3. some other stuff goes here
5 // Now here is the Map initializaiton steps ..............
6 map<IString,IString> myMap ;
7 if (myMap.find((IString)name) != myMap.end()) // if Header Exist in
Map Table
8 {
9 IString s1;
10 for ( map< IString, IString >::const_iterator im = myMap.begin();
im != myMap.end(); ++im )
11 if (im->first==name) s1=(IString)im->second ;
12 }
13 else
14 myMap[(IString)name]=(IString)value;
//************************************************** ****************
Here value and name is type of const char*

for the first few run Map is being initialize properly but suddenly
some memory corruption error shown & it's stuck at line 4 only. any
suggestion or clue on this please ?

Apr 12 '06 #1
8 2564
Dew wrote:
Hi all,
I needed a clue to get rid of this problem. Code work fine for few run
but then fail with some segmentation fault error with next subsequent
run. I am using Visual Age C++ under AIX compiler , version 5

1 //************************************************** ****************8
2 //1. some other stuff goes here
3 //2. some other stuff goes here
4 //3. some other stuff goes here
5 // Now here is the Map initializaiton steps ..............
6 map<IString,IString> myMap ;
7 if (myMap.find((IString)name) != myMap.end()) // if Header Exist in
Map Table
8 {
9 IString s1;
10 for ( map< IString, IString >::const_iterator im = myMap.begin();
im != myMap.end(); ++im )
11 if (im->first==name) s1=(IString)im->second ;
What's the point of this additional loop? You've already found name
inside of myMap, why not store the result of your find() call and change
the value through that iterator?

Also, you should prefer to use C++-style casts to C-style casts.
12 }
13 else
14 myMap[(IString)name]=(IString)value;
//************************************************** ****************
Here value and name is type of const char*
Is there a constructor from const char * to IString? That would
eliminate a lot of your casts (some of which are already superfluous).
for the first few run Map is being initialize properly but suddenly
some memory corruption error shown & it's stuck at line 4 only. any
suggestion or clue on this please ?


And if it's stuck at line 4, how can we diagnose since line 4 is a
comment where "some other stuff" happens?

Apr 12 '06 #2
Dew
Hi Andre,
Sorry that above code snap is not very clear..
What I want to do is :

if the Header is found, I need to concatenate the values for this
header . But if the header is new for the Map, I need to simply assign
this Header and it's corresponding values. This is why I have loop
inside the if statement.
so the final map will be look like this...
key1:value1.1,value1.2,value1.3;
key2:value2.1,value2.2,value2.3;

Here's the more clear lines....

1 //************************************************** ****************8
2
3
4
5
6 map<IString,IString> myMap ;
7 if (myMap.find((IString)name) != myMap.end()) // if Header Exist in
Map Table
8 {
9 IString s1;
10 for ( map< IString, IString >::const_iterator im = myMap.begin();
im != myMap.end(); ++im )
11 if (im->first==name) s1=(IString)im->second ;

if(strstr(s1, (IString)value)==NULL)
IString s2=s1 + (IString)"," + (IString)value;
myMap[(IString)evv_name]=s2;
12 }
13 else
14 myMap[(IString)name]=(IString)value;
//************************************************** ****************

during the step 2 to 5 I am getting the value of "value" and "name" in
the const char * string. and that's why I have to convert this into
IString here. Now my prob is Map is being initialized good for few run
but not always and hanging just before the step 6.

Just wanted to mention that all the above stuff are inside the for loop
who is being executed for each Key (here it's "name") & Value ( here
it's "value")

Is there some problem in casting the char * with IString or I am
missing something else related to Map .. ?

Apr 13 '06 #3
Dew wrote:
Hi Andre,
Sorry that above code snap is not very clear..
What I want to do is :

if the Header is found, I need to concatenate the values for this
header . But if the header is new for the Map, I need to simply assign
this Header and it's corresponding values. This is why I have loop
inside the if statement.
so the final map will be look like this...
key1:value1.1,value1.2,value1.3;
key2:value2.1,value2.2,value2.3;

Here's the more clear lines....

1 //************************************************** ****************8
2
3
4
5
6 map<IString,IString> myMap ;
7 if (myMap.find((IString)name) != myMap.end()) // if Header Exist in
Map Table
8 {
9 IString s1;
10 for ( map< IString, IString >::const_iterator im = myMap.begin();
im != myMap.end(); ++im )
11 if (im->first==name) s1=(IString)im->second ;

if(strstr(s1, (IString)value)==NULL)
IString s2=s1 + (IString)"," + (IString)value;
myMap[(IString)evv_name]=s2;
12 }
13 else
14 myMap[(IString)name]=(IString)value;
//************************************************** ****************
Right, but inside your if statement, you're going and iterating through
the map again to find the location that you've already found by calling
find(). Why not store the iterator returned by find() and use that
instead of searching the map again?
during the step 2 to 5 I am getting the value of "value" and "name" in
the const char * string. and that's why I have to convert this into
IString here. Now my prob is Map is being initialized good for few run
but not always and hanging just before the step 6.
But you still haven't shown us what's before step 6! We can only guess
as to what's going on up there.
Just wanted to mention that all the above stuff are inside the for loop
who is being executed for each Key (here it's "name") & Value ( here
it's "value")

Is there some problem in casting the char * with IString or I am
missing something else related to Map .. ?


No clue what an IString actually is... so can't say much here...
Apr 13 '06 #4
Dew wrote:
Hi Andre,
Sorry that above code snap is not very clear..
What I want to do is :

if the Header is found, I need to concatenate the values for this
header . But if the header is new for the Map, I need to simply assign
this Header and it's corresponding values. This is why I have loop
inside the if statement.
so the final map will be look like this...
key1:value1.1,value1.2,value1.3;
key2:value2.1,value2.2,value2.3;

Here's the more clear lines....

1 //************************************************** ****************8
2
3
4
5
6 map<IString,IString> myMap ;
7 if (myMap.find((IString)name) != myMap.end()) // if Header Exist in
Map Table
8 {
9 IString s1;
10 for ( map< IString, IString >::const_iterator im = myMap.begin();
im != myMap.end(); ++im )
11 if (im->first==name) s1=(IString)im->second ;

if(strstr(s1, (IString)value)==NULL)
IString s2=s1 + (IString)"," + (IString)value;
myMap[(IString)evv_name]=s2;
12 }
13 else
14 myMap[(IString)name]=(IString)value;


I'll rewrite this using better style:

typedef map<IString,IString> map_t;
typedef map_t::iterator iterator_t;
map_t myMap;
iterator_t pos = myMap.find(name);
if (pos != myMap.end() && strstr(pos->second, value) == NULL)
{
pos->second += IString(",") + value;
}
else
{
myMap.insert(map_t::value_type(name, value));
}

Why are you using "IString" rather than std::string? Better than the
above is:
typedef map<string,string> map_t;
typedef map_t::iterator iterator_t;
map_t myMap;
iterator_t pos = myMap.find(name);
if (pos != myMap.end() && pos->second.find(value) == string::npos)
{
pos->second += string(",") + value;
}
else
{
myMap.insert(map_t::value_type(name, value));
}

As you can see, there is no need for a loop in either case.

Tom
Apr 13 '06 #5
In article <11*********************@t31g2000cwb.googlegroups. com>,
"Dew" <mr*********@gmail.com> wrote:
Hi Andre,
Sorry that above code snap is not very clear..
What I want to do is :

if the Header is found, I need to concatenate the values for this
header . But if the header is new for the Map, I need to simply assign
this Header and it's corresponding values. This is why I have loop
inside the if statement.
so the final map will be look like this...
key1:value1.1,value1.2,value1.3;
key2:value2.1,value2.2,value2.3;

Here's the more clear lines....

1 //************************************************** ****************8
2
3
4
5
6 map<IString,IString> myMap ;
If you just created myMap in line 6, there is no need for the find
because you know nothing is in the map.
7 if (myMap.find((IString)name) != myMap.end()) // if Header Exist in
Map Table
8 {
9 IString s1;
10 for ( map< IString, IString >::const_iterator im =
myMap.begin();
im != myMap.end(); ++im )
11 if (im->first==name) s1=(IString)im->second ;

if(strstr(s1, (IString)value)==NULL)
IString s2=s1 + (IString)"," + (IString)value;
myMap[(IString)evv_name]=s2;
12 }
13 else
14 myMap[(IString)name]=(IString)value;
//************************************************** ****************
It would help immeasurably if you could paste code that we can simply
copy and paste in a cpp file, run it and see the problem.

during the step 2 to 5 I am getting the value of "value" and "name" in
the const char * string. and that's why I have to convert this into
IString here. Now my prob is Map is being initialized good for few run
but not always and hanging just before the step 6.
I doubt the code you are posting is where the error is. I suspect it's
more like a memory overwrite somewhere else in your code.
Just wanted to mention that all the above stuff are inside the for loop
who is being executed for each Key (here it's "name") & Value ( here
it's "value")
Is line 6 in the for loop? If so, then there's a problem...
Is there some problem in casting the char * with IString or I am
missing something else related to Map .. ?


Who wrote IString? Are you null terminating your char*?
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Apr 13 '06 #6
In article <e1**********@emma.aioe.org>,
Tom Widmer <to********@hotmail.com> wrote:
I'll rewrite this using better style:

typedef map<IString,IString> map_t;
typedef map_t::iterator iterator_t;
map_t myMap;
iterator_t pos = myMap.find(name);
if (pos != myMap.end() && strstr(pos->second, value) == NULL)
{
pos->second += IString(",") + value;
}
else
{
myMap.insert(map_t::value_type(name, value));
}


In your code, if the value isn't already in the map, you still have to
make a second search. In the code below, only one search through the map
is necessary...

typedef string IString; // for those of us who don't have IString

void add_value( map<IString,IString>& myMap, IString key, IString value )
{
map<IString, IString>::iterator it = myMap.lower_bound( key );
if ( it->first == key )
it->second += IString(",") + value;
else
myMap.insert( it, make_pair( key, value ) );
}

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Apr 13 '06 #7
Daniel T. wrote:
In your code, if the value isn't already in the map, you still have to
make a second search.
True, it is suboptimal.

In the code below, only one search through the map is necessary...

typedef string IString; // for those of us who don't have IString

void add_value( map<IString,IString>& myMap, IString key, IString value )
{
map<IString, IString>::iterator it = myMap.lower_bound( key );
if ( it->first == key )
it->second += IString(",") + value;
else
myMap.insert( it, make_pair( key, value ) );
}


A slightly more compact alternative would be:

IString& ref = myMap[key];
if (ref.length() > 0)
ref += ',';
ref += value;

However, the OP appeared to want to check that the value wasn't already
present in the value string. So:

IString& ref = myMap[key];
if (!ref.includes(value)) //assuming this is IBM's IString
{
if (ref.length() > 0)
ref += ',';
ref += value;
}

Tom
Apr 13 '06 #8
Dew
Thanks all, I've updated the Map operation as you all suggested &
surely it's more efficient way.

Finally I could able to locate the prob. The prob was with IString
object. IString is Visual age C++ Class that provide String operation.

Just wanted to share the prob.
to Initialize the IString, i was doing something this way...
IString Name = NULL ;
or
IString Value= IString("") ;

It was okie for first few run but later on subsequent run it was
creating a problem , probably Stack/Heap memory assigned by IString
object was being corrupted bcoz improper initialization.

To solve the above, I did like...
IString Name= IString("",512);

[ "When you create an object of a Istring class, the actual characters
that make up the string are not stored in the string object. Instead,
the characters are stored in an object of a buffer class. Each IString
holds a pointer to an object of the OCL class IBuffer."]
Thank you all for good suggestion.

Apr 15 '06 #9

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

Similar topics

41
by: Allan Bruce | last post by:
Hi there, I am interested to know if there are any C++ programmers who do not use STL. I have been coding in C++ for a year or so now, although only recently have I started a large(ish)-scale...
0
by: rajd99 | last post by:
Hi, I am getting the following error while compiling with sun C++ 5.5 and using SGI STL. Has anyone run into this. The SGI headers are one at http://www.sgi.com/tech/stl/download.html Thanks, ...
20
by: Andrew Roberts | last post by:
Any more info on this? Anyone know when it will be released?
3
by: arnuld | last post by:
Hello, i posted this question more than 1 month ago but did not get any answers, so i am posting it again (1). with advice form archives @ alt.comp.lang.learn.c-c++, comp.lang.c++,...
13
by: prasadmpatil | last post by:
I am new STL programming. I have a query regarding vectors. If I am iterating over a vector using a iterator, but do some operations that modify the size of the vector. Will the iterator recognize...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.