public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Aubin LaBrosse <aubin.labrosse@tigoenergy.com>
To: gcc-help@gcc.gnu.org
Subject: vector returns negative size with glibc 2.3.3??
Date: Thu, 23 Jul 2009 01:12:00 -0000	[thread overview]
Message-ID: <1479fc020907221812l2ee16531kec231bd371f08fae@mail.gmail.com> (raw)

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

                 reply	other threads:[~2009-07-23  1:12 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1479fc020907221812l2ee16531kec231bd371f08fae@mail.gmail.com \
    --to=aubin.labrosse@tigoenergy.com \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).