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

  • SEARCH
  • Home
  • 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 7016889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:50:37+00:00 2026-05-27T22:50:37+00:00

I have a class containing a set of C dynamic arrays which are all

  • 0

I have a class containing a set of C dynamic arrays which are all of the same size, with one of the members being the size of these arrays :

class stuff {
  int size;
  float *abc;
  float *def;
  // etc.
 };

To ease memory management of this class (and a lot of other classes having a similar layout), I would like to use standard containers. However I must keep the alignment constraint provided by C arrays (and vectors) because this data will be passed around in a lot of C functions.

My problem here is that each vector has its own size, which is redundant and can lead to errors if for one reason or another one changes its size and not the others.

Is there a way to force the arrays/vectors/whatevers to always be the same size ?

  • 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-27T22:50:38+00:00Added an answer on May 27, 2026 at 10:50 pm

    This is perhaps one case where it make sense to create a class of your own to manage the memory (no business logic, just memory).

    You may, of course, articulate this class around vectors:

    #define CHECK_CLASS_INVARIANT() \
      ClassInvariant inv##__LINE__ (*this); (void) inv##__LINE__;
    
    class Foo {
    public:
      size_t size() const { return _a.size(); }
    
      float* getA() { return &_a[0]; }
      float* getB() { return &_b[0]; }
    
      void push(float a, float b) {
        CHECK_CLASS_INVARIANT()
        _a.push_back(a);
        _b.push_back(b);
      }
    
    private:
      struct ClassInvariant {
        ClassInvariant (Foo& f): _f(f) {}
        ~ClassInvariant () { assert(_f._a.size() == _f._b.size()); }
    
        Foo& _f;
      }; // struct ClassInvariant
    
      std::vector<float> _a;
      std::vector<float> _b;
    };
    

    We make sure to always check for the class invariant (in mutating methods) to ensure that it always apply.

    Note: there is a slight flow here, if _b.push_back(b); throws, then _a was extended and _b was not, this can be handled in a try/catch block OR as SteveJessop aptly remarked, by (1) reserving enough space beforehand in both vectors and (2) pushing in them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class containing a set of attributes, which I want to be
I have a class containing several properties (all are strings if it makes any
I have a java class containing all the columns of a database table as
I have a class with 4 arrays: tab_1,tab_2,tab_3 and tab_T. I set the picker's
I have a page containing a set of jQuery tabs. All the tabs point
I have a class Param.cpp which is a set of parameters for the main
I have designed a class containing some information about a given object which will
I have class containing an std::set of boost::weak_ptr<T> . I have two functions begin()
I have a class containing a collection property which I want to display and
I have a class containing Linq To SQL objects that are used to populate

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.