Solved

STL bug or programming error?

Posted on 2003-10-30
12
595 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
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
Comment Utility
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
Comment Utility
>> 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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:clorda
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Can you get any STL code to compile?
0
 
LVL 4

Expert Comment

by:PhilipNicoletti
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

772 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

9 Experts available now in Live!

Get 1:1 Help Now