Solved

STL bug or programming error?

Posted on 2003-10-30
12
597 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now