public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* vector returns negative size with glibc 2.3.3??
@ 2009-07-23  1:12 Aubin LaBrosse
  0 siblings, 0 replies; only message in thread
From: Aubin LaBrosse @ 2009-07-23  1:12 UTC (permalink / raw)
  To: gcc-help

Hello all,

I'm including a very strange test program which causes std::vector to
return negative numbers when asked for its size. The source code is:

///////////////////////////////////////////////////////////////////////////////

extern "C"
{
       #include <stdlib.h>
       #include <string.h>
       #include <stdio.h>
}

///////////////////////////////////////////////////////////////////////////////

#include <vector>

///////////////////////////////////////////////////////////////////////////////

using namespace std;

///////////////////////////////////////////////////////////////////////////////
//
//  CList class
//

class CList
{
public:
       // Declaration of this variable makes m_Items
       // return negative size:
       char m_pDummyData[1037];

public:
       // Declaring m_Items before m_pDummyData fixes
       // the problem
       vector<int> m_Items;

public:
       CList(){};

public:
       /////////////////////////////////////////////

       void Add(int nItem)
       {
               printf("Before adding: %d\n", m_Items.size());
               m_Items.push_back(nItem);
               printf("After adding: %d\n", m_Items.size());
       }

       /////////////////////////////////////////////

       int GetCount()
       {
               printf("GetCount: size is %d\n", m_Items.size());
               return m_Items.size();
       }

       /////////////////////////////////////////////

       int Display()
       {
               for (vector<int>::iterator i = m_Items.begin(); i !=
m_Items.end(); i++)
               {
                       printf("Element: %d\n", *i);
               }
       }

       /////////////////////////////////////////////
};

///////////////////////////////////////////////////////////////////////////////
//
//  main loop
//

int main(int argc, char **argv)
{
       CList *List = new CList();

       List->Add(10);
       List->Add(11);
       List->Add(12);

       printf("List size: %d\n", List->GetCount());
       printf("List size: %d\n", List->GetCount());
       printf("List size: %d\n", List->GetCount());

       List->Display();

}

and here is its output:


Before adding: 0
After adding: -1
Before adding: -1
After adding: -2
Before adding: -2
After adding: -3
GetCount: size is -3
List size: -3
GetCount: size is -3
List size: -3
GetCount: size is -3
List size: -3
Element: 10
Element: 11
Element: 12


Things to note:

1) this works fine on glibc 2.8 (and presumably others, but i've only
tested 2.3 and 2.8) and fails on 2.3(.3 if it matters)

2) if you change the size of that dummy data var to 1036 or lower, it works fine

3) if you make m_items dynamic (with 'new') and change everything to
use a dynamically allocated list, it works fine

4) different gcc's - the working version is gcc 4.3.2/glibc2.8.x (i
don'r know x), the non working one is gcc 4.2.3/glibc2.3.3  no typos
in this line, checked carefully.

needless to say, I am very confused.  did I do something wrong in the
example or is there really some strange kind of bug in gcc4.3.2/glibc 2.3.3?

Thanks in advance for any insights,

-aubin

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2009-07-23  1:12 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-07-23  1:12 vector returns negative size with glibc 2.3.3?? Aubin LaBrosse

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).