public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/31825]  New: bug in g++ a.out for stl::map
@ 2007-05-04 22:00 lbana at hotmail dot com
  2007-05-04 22:06 ` [Bug libstdc++/31825] " pinskia at gcc dot gnu dot org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: lbana at hotmail dot com @ 2007-05-04 22:00 UTC (permalink / raw)
  To: gcc-bugs

# gcc -v
Reading specs from /usr/local/lib/gcc/i686-pc-linux-gnu/3.4.4/specs
Configured with: ./configure
Thread model: posix
gcc version 3.4.4


I have compiled a simple test program in C++ using stl::map. 

#g++ -DDOESNT_WORK test.cpp
#./a.out
Added value
TYpe=0name = varnum0value =199
Added value
TYpe=1name = varnum1value =200
Added value
TYpe=2name = varnum2value =201
Added value
TYpe=3name = varnum3value =202
Added value
TYpe=4name = varnum4value =203
Added value
TYpe=5name = varnum5value =204
Added value
TYpe=6name = varnum6value =205
Added value
TYpe=7name = varnum7value =206
Added value
TYpe=8name = varnum8value =207
Added value
TYpe=9name = varnum9value =208
Size = 1
Type = 0Name = varnum0value= 199

#g++ -DTHIS_WORKS test.cpp
# ./a.out
Added value
TYpe=0name = varnum0value =199
Added value
TYpe=1name = varnum1value =199
Added value
TYpe=2name = varnum2value =100
Added value
TYpe=3name = varnum3value =100
Added value
TYpe=4name = varnum4value =100
Added value
TYpe=5name = varnum5value =100
Added value
TYpe=6name = varnum6value =100
Size = 7
Type = 0Name = varnum0value= 199
Type = 1Name = varnum1value= 199
Type = 2Name = varnum2value= 100
Type = 3Name = varnum3value= 100
Type = 4Name = varnum4value= 100
Type = 5Name = varnum5value= 100
Type = 6Name = varnum6value= 100

File : test.cpp
#include "uv_type.h"

int main(const int argc, const char **argv)
{
    UserVariable uv;
    char name[10];
    char var[20];

#if defined (DOESNT_WORK)
    for (int i = 0; i < 10; i++)
    {
        sprintf(name, "varnum%d", i);
        sprintf(var, "%d", i + 199);

        uv.AddUserVar(i, name, var);
    }
#endif
#if defined (THIS_WORKS)
    uv.AddUserVar(0, "varnum0", "199");
    uv.AddUserVar(1, "varnum1", "199");
    uv.AddUserVar(2, "varnum2", "100");
    uv.AddUserVar(3, "varnum3", "100");
    uv.AddUserVar(4, "varnum4", "100");
    uv.AddUserVar(5, "varnum5", "100");
    uv.AddUserVar(6, "varnum6", "100");
#endif
    uv.Print();
    return 0;
}

file: uv_type.h
#ifndef UV_TYPE_H
#define UV_TYPE_H

#include <map>
#include <iostream>

typedef struct
{
    int  type;
    char name[40];
    char data[80];
}
UserVar;

typedef std::map<char *, UserVar>UVTable;
typedef UVTable::value_type UVEntry;

class UserVariable
{
    public:
        UserVariable() {}
        ~UserVariable() {}
        int AddUserVar(int type, char *name, char *value)
        {
            UserVar var;
            int ret = 0;
            UVTable::iterator iter;

            var.type = type;
            if (strlen(name) < 40)
            {
                strcpy(var.name, name);
            }
            else
            {
                return 1;
            }

            if (strlen(value) < 80)
            {
                strcpy(var.data, value);
            }
            else
            {
                return 1;
            }

            UVEntry entry(name, var);

            tbl.insert(UVEntry(name, var));
            std::cout << "Added value" << std::endl;
            std::cout << "TYpe=" << type << "name = "<< name << "value ="
                      << value << std::endl;

            return 0;
        }

        int DelUserVar(int type, char *name)
        {
        }

        void Print()
        {
            UVTable::iterator iter;

            std::cout << "Size = " << tbl.size() << std::endl;

            for (iter = tbl.begin(); iter != tbl.end(); iter++)
            {
                UserVar &var = iter->second;
                std::cout << "Type = " << var.type
                          << "Name = " << var.name
                          << "value= " << var.data << std::endl;
            }
        }
    protected:
        UVTable tbl;
};

#endif


-- 
           Summary: bug in g++ a.out for stl::map
           Product: gcc
           Version: 3.4.4
            Status: UNCONFIRMED
          Severity: major
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: lbana at hotmail dot com
  GCC host triplet: Linux 2.6.19-1.2911.6.5.fc6 #1 SMP i686 i686 i386
                    GNU/Linux


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31825


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug libstdc++/31825] bug in g++ a.out for stl::map
  2007-05-04 22:00 [Bug c++/31825] New: bug in g++ a.out for stl::map lbana at hotmail dot com
@ 2007-05-04 22:06 ` pinskia at gcc dot gnu dot org
  2007-05-04 23:11 ` pcarlini at suse dot de
  2007-05-05  4:01 ` lbana at hotmail dot com
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2007-05-04 22:06 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from pinskia at gcc dot gnu dot org  2007-05-04 23:05 -------
I don't think this is a bug as you changing the key from underneath of
std::map.


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|c++                         |libstdc++


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31825


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug libstdc++/31825] bug in g++ a.out for stl::map
  2007-05-04 22:00 [Bug c++/31825] New: bug in g++ a.out for stl::map lbana at hotmail dot com
  2007-05-04 22:06 ` [Bug libstdc++/31825] " pinskia at gcc dot gnu dot org
@ 2007-05-04 23:11 ` pcarlini at suse dot de
  2007-05-05  4:01 ` lbana at hotmail dot com
  2 siblings, 0 replies; 4+ messages in thread
From: pcarlini at suse dot de @ 2007-05-04 23:11 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from pcarlini at suse dot de  2007-05-05 00:11 -------
You are only passing around a pointer to name and not allocating memory for the
various "C" strings, instead overwriting each time in the loop the same char
array. Well, there are also other problems in the code, like not passing to
std::map as third template argument an appropriate comparison object:

struct ltstr
{
  bool operator()(const char* s1, const char* s2) const
  {
    return std::strcmp(s1, s2) < 0;
  }
};

I would suggest always using std::string, instead of "C" strings, at least
while you are still learning std::map.


-- 

pcarlini at suse dot de changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31825


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug libstdc++/31825] bug in g++ a.out for stl::map
  2007-05-04 22:00 [Bug c++/31825] New: bug in g++ a.out for stl::map lbana at hotmail dot com
  2007-05-04 22:06 ` [Bug libstdc++/31825] " pinskia at gcc dot gnu dot org
  2007-05-04 23:11 ` pcarlini at suse dot de
@ 2007-05-05  4:01 ` lbana at hotmail dot com
  2 siblings, 0 replies; 4+ messages in thread
From: lbana at hotmail dot com @ 2007-05-05  4:01 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from lbana at hotmail dot com  2007-05-05 05:01 -------
Subject: RE:  bug in g++ a.out for stl::map

Dear Gnu,
Sorry for logging invalid bug, I should have spend some time in reading it.
I thought all I need to do is pass stack variable. Anyhow thanks for the 
advice.
-Laxman
>------- Comment #2 from pcarlini at suse dot de  2007-05-05 00:11 -------
>You are only passing around a pointer to name and not allocating memory for 
>the
>various "C" strings, instead overwriting each time in the loop the same 
>char
>array. Well, there are also other problems in the code, like not passing to
>std::map as third template argument an appropriate comparison object:
>
>struct ltstr
>{
>   bool operator()(const char* s1, const char* s2) const
>   {
>     return std::strcmp(s1, s2) < 0;
>   }
>};
>
>I would suggest always using std::string, instead of "C" strings, at least
>while you are still learning std::map.
>
>
>--
>
>pcarlini at suse dot de changed:
>
>            What    |Removed                     |Added
>----------------------------------------------------------------------------
>              Status|UNCONFIRMED                 |RESOLVED
>          Resolution|                            |INVALID
>
>
>http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31825
>
>------- You are receiving this mail because: -------
>You reported the bug, or are watching the reporter.

_________________________________________________________________
Get a FREE Web site, company branded e-mail and more from Microsoft Office 
Live! http://clk.atdmt.com/MRT/go/mcrssaub0050001411mrt/direct/01/


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31825


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2007-05-05  4:01 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-05-04 22:00 [Bug c++/31825] New: bug in g++ a.out for stl::map lbana at hotmail dot com
2007-05-04 22:06 ` [Bug libstdc++/31825] " pinskia at gcc dot gnu dot org
2007-05-04 23:11 ` pcarlini at suse dot de
2007-05-05  4:01 ` lbana at hotmail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).