To all:
I have observed an interesting quirk in the behavior of the STL
library map container when storing char* arrays. The quirk is that
the last line entered seems to be the only response given when making
requests into the map container with other map indexes. See the
output below.
I am curious to see if others can duplicate this behaviour which I
observed, and so I enclose the C++ code and the compile line, in order
to test this behavior.
Part of my code does provide specific information on my compile environement.
I suspect that temporary storage for char* arrays is at the heart of
the problem.
------- code below -------------------
#include <iostream>
#include <map>
// Compile line:
// g++ map_example.cpp -o map -Wall -g
// Executing:
// ./map
using namespace std;
#define LETTERS 4
enum letter { a, o, b, p };
const char* letter_char[] = { "a", "b", "c", "d" };
enum id_type { id_a, id_b, id_c, id_d };
const char* id_char[] = { "id_a", "id_b", "id_c", "id_d" };
const char* fruits[] = { "apple", "orange", "banana", "pear" };
struct fruit_type {
char* lc;
id_type i;
char* ic;
char* fruit;
fruit_type() {
lc = const_cast<char*>(letter_char[0]);
i = id_a;
ic = const_cast<char*>(id_char[0]);
fruit = const_cast<char*>(fruits[0]);
}
fruit_type(char* l, id_type id, char* idc, char* f) {
lc = l;
i = id;
ic = idc;
fruit = f;
}
};
struct compare
{
bool operator()(const letter l1, const letter l2) const
{
return l1 < l2;
}
};
typedef map<letter, fruit_type, compare> my_mapping_type;
my_mapping_type initialize_letters()
{
my_mapping_type mapping;
string root = "qualified/";
cout << endl;
cout << "Loading the STL::map<> object" << endl;
for ( int index = 0 ; index < LETTERS ; index++ ) {
string item(fruits[index]);
item = root + item;
// cout << "item[" << index << "] = " << item << endl;
id_type lc = (id_type) index;
fruit_type grocery(const_cast<char*>(letter_char[index]), lc,
const_cast<char*>(id_char[index]), const_cast<char*>(item.c_str()));
letter a_letter = (enum letter) index;
mapping[a_letter] = grocery;
cout << a_letter << " " << mapping[a_letter].lc << " " <<
mapping[a_letter].i << " " << mapping[a_letter].ic << " " <<
mapping[a_letter].fruit << endl;
}
return mapping;
}
int main()
{
my_mapping_type letters = initialize_letters();
cout << "Finished initializing the map object" << endl;
cout << endl;
cout << "Using iterator to dump the map" << endl;
my_mapping_type::iterator iter;
for ( iter = letters.begin(); iter != letters.end(); ++iter ) {
letter a_letter = iter->first;
fruit_type boo = iter->second;
cout << a_letter << " " << boo.lc << " " << boo.i << " " << boo.ic
<< " " << boo.fruit << endl;
}
cout << "Finished" << endl;
cout << endl;
cout << "Dumping the g++ compiler specifications:" << endl;
cout << endl;
cout << "My build environment:" << endl;
cout << " " << "COLLECT_GCC=g++" << endl;
cout << " " <<
"COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.6/lto-wrapper" <<
endl;
cout << " " << "Target: x86_64-linux-gnu" << endl;
cout << " " << "Configured with: ../src/configure -v
--with-pkgversion='Ubuntu/Linaro 4.6.3-1ubuntu5'
--with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs
--enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr
--program-suffix=-4.6 --enable-shared --enable-linker-build-id
--with-system-zlib --libexecdir=/usr/lib --without-included-gettext
--enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6
--libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu
--enable-libstdcxx-debug --enable-libstdcxx-time=yes
--enable-gnu-unique-object --enable-plugin --enable-objc-gc
--disable-werror --with-arch-32=i686 --with-tune=generic
--enable-checking=release --build=x86_64-linux-gnu
--host=x86_64-linux-gnu --target=x86_64-linux-gnu" << endl;
cout << " " << "Thread model: posix"<< endl;
cout << " " << "gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)" << endl;
cout << endl;
cout << "Dumping the Linux version:" << endl;
cout << endl;
cout << " " << "Linux version 3.2.0-58-generic (buildd@allspice)
(gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) ) #88-Ubuntu SMP Tue
Dec 3 17:37:58 UTC 2013" << endl;
cout << endl;
return 0;
}
-------------------------------- output ------------------------------
Loading the STL::map<> object
0 a 0 id_a qualified/apple
1 b 1 id_b qualified/orange
2 c 2 id_c qualified/banana
3 d 3 id_d qualified/pear
Finished initializing the map object
Using iterator to dump the map
0 a 0 id_a qualified/pear
1 b 1 id_b qualified/pear
2 c 2 id_c qualified/pear
3 d 3 id_d qualified/pear
Finished
Dumping the g++ compiler specifications:
My build environment:
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.6/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v
--with-pkgversion='Ubuntu/Linaro 4.6.3-1ubuntu5'
--with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs
--enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr
--program-suffix=-4.6 --enable-shared --enable-linker-build-id
--with-system-zlib --libexecdir=/usr/lib --without-included-gettext
--enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6
--libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu
--enable-libstdcxx-debug --enable-libstdcxx-time=yes
--enable-gnu-unique-object --enable-plugin --enable-objc-gc
--disable-werror --with-arch-32=i686 --with-tune=generic
--enable-checking=release --build=x86_64-linux-gnu
--host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)
Dumping the Linux version:
Linux version 3.2.0-58-generic (buildd@allspice) (gcc version 4.6.3
(Ubuntu/Linaro 4.6.3-1ubuntu5) ) #88-Ubuntu SMP Tue Dec 3 17:37:58 UTC
2013
_______________________________________________
PLUG mailing list
[email protected]
http://lists.pdxlinux.org/mailman/listinfo/plug