Solved

STL bug or programming error?

Posted on 2003-10-30
12
598 Views
Last Modified: 2013-12-14
I have the following test code:

#include <vector>

typedef struct
{
   char field1[1024];
} TEST_STRU;

typedef std::vector<TEST_STRU> Test_Vector;

int main()
{
   Test_Vector fooStorage;
   TEST_STRU foo;

   for (unsigned int i = 0; i < 50000; ++i)
   {
      fooStorage.push_back(foo);
   }
}

It compiles and runs on Solaris 2.8, HPUX 11i, and RedHat Linux 8.  It compiles but then produces a core file when it runs on AIX 4.3.3.11 and AIX 5.2 using the VisualAge C++ compiler version 5 and 6.  The vague core files and backtraces seem to point to memory and allocation problems but we are not running out of memory.  We've been developing our application on these platforms for nearly 10 years so we've learned what works and what doesn't but I've exhausted all my resources on this one.  
0
Comment
Question by:clorda
  • 3
  • 3
  • 3
  • +1
12 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 300 total points
ID: 9650964
This behaviour is more likely to be a problem with the STL implementation you are using on AIX - especially if it runs file on other platforms.
0
 
LVL 4

Expert Comment

by:PhilipNicoletti
ID: 9651039
My guess is that you are running out of STACK space.

Basically, you have a 50000 char[1024] variables

try this code on the machines that it crashes on :

int main()
{
    const int N = 50000000;
    char str[N];

    for (int i=0; i<N; ++i)
        str[i] = 'a';

    return 0;
}

You might consider using std::string instead of char[1024]
0
 
LVL 86

Expert Comment

by:jkr
ID: 9651077
>>My guess is that you are running out of STACK space.

The contents of the vector are allocated on the heap.
0
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 8

Accepted Solution

by:
Exceter earned 200 total points
ID: 9651114
Strange... That should compile fine. Can you get any STL code to compile? Have you tried implementing TEST_STRU as a class? For example,

#include <vector>

class TEST_STRU
{
    public:
        TEST_STRU() { field1 = new char[1024]; }
            ~TEST_STRU() { delete [] field1; }
            TEST_STRU( const TEST_STRU &n ){ strcpy( field1, n.field1 ); }
            TEST_STRU &operator=( const TEST_STRU &n ){ strcpy( field1, n.field1 ); return *this; }

        char *field1;
};

typedef std::vector<TEST_STRU> Test_Vector;

int main()
{
  Test_Vector fooStorage;
  TEST_STRU t;

  for (unsigned int i = 0; i < 50000; ++i)
   {
     fooStorage.push_back(t);
  }

  return 0;
}

Exceter
0
 
LVL 8

Expert Comment

by:Exceter
ID: 9651133
>> The contents of the vector are allocated on the heap.

Ditto, it has to be or a vector would be unable to resize itself.
0
 
LVL 4

Expert Comment

by:PhilipNicoletti
ID: 9651364
OK ... I went braindead for a moment :)

I agree it is with the resize.  Try using Exceters class (or use
std::string if possible)

Another thing to try if you know how many elements upfront is
to do a reserve() ...

  fooStorage.reserve(50000);

  for (unsigned int i = 0; i < 50000; ++i)
   {
     fooStorage.push_back(t);
  }
0
 

Author Comment

by:clorda
ID: 9651485
The class change cores also.  Anybody have a local AIX box they can try this on?  We have 4G of real, 4G of virtual, and unlimited user area so I don't think stack space is an issue.
0
 
LVL 86

Expert Comment

by:jkr
ID: 9651538
Try STLport (http://www.stlport.org/download.html) if the native STL on AIX gives problems - as I wrote above, this is more likely to be a problem with the STL implementation you are using.
0
 

Author Comment

by:clorda
ID: 9651893
STLPort advertises every platform but we could only get it to work on Solaris which didn't help us.  I also believe it is a problem with STL but I was hoping to zero in on what that might be.  Glaciers melt faster than IBM admits to problems and we have a critical delivery coming up.
0
 
LVL 8

Expert Comment

by:Exceter
ID: 9651984
Can you get any STL code to compile?
0
 
LVL 4

Expert Comment

by:PhilipNicoletti
ID: 9652387
If you used Exceter's class you need to make one small change.
The copy constructor did not allocate space

        TEST_STRU( const TEST_STRU &n )
        {
            field1 = new char[1024];
            strcpy( field1, n.field1 );
        }


and although it is not needed in this case, often you check for
self-assignment in the copy assignment ...

        TEST_STRU &operator=( const TEST_STRU &n )
        {
            if (&n != this)
               strcpy( field1, n.field1 );

            return *this;
        }
0
 

Author Comment

by:clorda
ID: 9652481
This was a test program created from the original program which is many hundred lines of code for a scheduling application.  It's part of a suite of programs with thousands of lines of code that runs on all of the originally mentioned platforms.  The problem occurs with both deque and vector, vector just takes a little longer.  
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
I could not build boost code, 10 88
Error C2678: binary '!=': no operator found... 4 48
max float value 3 41
c++, dynamic object by json 1 42
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question