This is the mail archive of the
libc-help@sourceware.org
mailing list for the glibc project.
vector returns negative size with glibc 2.3.3??
- From: Aubin LaBrosse <aubin dot labrosse at tigoenergy dot com>
- To: libc-help <libc-help at sourceware dot org>
- Date: Tue, 21 Jul 2009 15:34:32 -0700
- Subject: vector returns negative size with glibc 2.3.3??
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
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 glibc 2.3.3?
Thanks in advance for any insights,
-aubin