?
Solved

STL bug or programming error?

Posted on 2003-10-30
12
Medium Priority
?
605 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
  • +1
12 Comments
 
LVL 86

Assisted Solution

by:jkr
jkr earned 900 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 8

Accepted Solution

by:
Exceter earned 600 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

800 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