Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7063993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:46:47+00:00 2026-05-28T04:46:47+00:00

I have a class polymer with a static int count . When I create

  • 0

I have a class polymer with a static int count.
When I create a new polymer to add to an array of pointers I am using the count to find the correct location in the array and then I update the count in the constructor. While compiling in Windows it worked. However, when compiling in Linux (Ubuntu) it crashes unless I remove the updating of the count out of the constructor.

WORKS in Windows and Ubuntu:

polymerPointer[polymer::count] = new polymer();
polymer::count++;

WHEN the constructor doesn’t update the static variable (see below)

polymer::polymer(){
    //sets up lots of variables but doesn't update the static member
};

CRASHES in Ubuntu (works in Windows):

polymerPointer[polymer::count] = new polymer();

WHEN the constructor does update the static variable (see below)

polymer::polymer(){
    //sets up lots of variables and then updates the static member
    count++;
};

I can rewrite the code, but I liked not having to remember to update the variable separately, which is why I put the update in the constructor. Any ideas on what is going wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:46:48+00:00Added an answer on May 28, 2026 at 4:46 am

    You’re hitting undefined behavior.

    The following:

    polymerPointer[polymer::count] = new polymer();
    polymer::count++;
    

    is not equivalent to

    polymerPointer[polymer::count] = new polymer();
    

    where polymer() increments polymer::count.

    The undefined behavior results from the fact that you’re modifying a value and using that value in the same statement:

    §1.9 p15 If a side effect on a scalar object is unsequenced relative
    to either another side effect on the same scalar object or a value
    computation using the value of the same scalar object, the behavior is
    undefined.

    What probably is occuring is that the count is incremented and then the object is placed in that new position in the array. Now code will access the empty spot left as though it held a valid pointer, or when you get to the end of an array you may try to place the pointer out of the bounds of the array.

    It’s bad design to put the count increment in a different place than where you actually insert into the array. What you should do is write a static member function that adds elements to the array and updates the count, and then use that instead of manually creating the object and manually placing it in the array, while expecting the count to get updated automatically.

    class polymer {
        static void create_new_polymer() {
            polymerPointer[polymer::count] = new polymer();
            count++;
        }
    };
    

    Even better would be to just use a vector and have it manage it’s own count:

    polymerPointer.push_back(new polymer());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have: class first{ private: int *array; public: first(int x){ array = new int[x][10];
I have class Fred { public: void inspect() const {}; void modify(){}; }; int
I have class X and in it there is a static method called doStuff()
I have class GUI so I can create object like this: GUI g1 =
I have class instantiated in a web service that, in a static member, holds
I have class that extends BroadcastReceiver and gets all new sms. When I get
I have class that I believe should not be a singleton or static class.
I have class MyForm which inherited from QMainWindow . Here's my code: std::auto_ptr<MyForm> pForm(new
I have class method: public object MyMethod(object obj) { // I want to add
I have class(Customer) which holds more than 200 string variables as property. I'm using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.