Solved

STL bug or programming error?

Posted on 2003-10-30
12
600 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

831 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