vector returns negative size with glibc 2.3.3??

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux