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 591033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:35:33+00:00 2026-05-13T15:35:33+00:00

I have some basic C++ design/syntax questions and would appreciate your reply. I have

  • 0

I have some basic C++ design/syntax questions and would appreciate your reply.

  • I have N number of regions
  • Each region needs to store information about an object “element”

i.e. I want to achieve something like this:

region[i].elements = list of all the elements for region i.

Question 1:
Does the following syntax (see code below) / design look correct. Am I missing anything here?

EDIT

The instances of struct elem are created by some other class and its memory deallocation is handles by that class only I just want to access that object and its members using reg[i].elements list (vector) … so, how should I add these element objects to the vector “elements” in class Region?

//Already have this stucture that I need to use

struct elemt {
  int* vertex; 
  int foo1;
  double foo2;
};


class Region{
   public:
      // I am not sure what should be the syntax here! 
      // is it correct?
      std::vector <elemt*> elements;      
}

// Following is the constructor of "class A"
A::A(){
    // --header file defines: Region *reg;
    // Let numOfRegions be a class variable. ( changes based on "Mac"'s suggestion)
    numOfRegions = 100;
    //allocate memory:
    reg = new Region[numOfRegions];
}

A::~A(){
  delete [] reg;
  reg = NULL;
}

A::doSomething(){

// here I want to append the elements to the vector
// Let i be region 10. 
// Let e1 be an element of "struct elemt" that needs to be added

  reg[i].elements.push_back(e1);

}

Question 2:
Is the syntax in doSomething() correct? Later I want to run an iterator over all the elements in reg[i] and want to access, e1->foo1, e1->foo2 and like that.

Question 3:
In do something method, how do I ensure that e1 is not already in the “elements”

UPDATE

Corrected some syntax errors, and hopefully fixed memory leak noticed by user ‘Mac. ‘

  • 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-13T15:35:33+00:00Added an answer on May 13, 2026 at 3:35 pm

    First of all get rid of the memory leak from the code.

    A::A(int numOfRegions = 100){
    
        m_reg = new Region[numOfRegions]; // define Region *m_reg in the class 
    }
    
    A::~A(){
      delete [] m_reg;
      m_reg = NULL;
    }
    
    You are allocating memory in the constructor and storing return address in local variable and it ll get destroyed when its scope is over .
    
    You should store the base address so that you can delete it .
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 309k
  • Answers 309k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Be careful using autocorrection such as forcing user input to… May 13, 2026 at 9:58 pm
  • Editorial Team
    Editorial Team added an answer >>> x=['a','a','b','c','c','c'] >>> map(x.count,x) [2, 2, 1, 3, 3, 3]… May 13, 2026 at 9:58 pm
  • Editorial Team
    Editorial Team added an answer You don't need to use Flash Pro to do that.… May 13, 2026 at 9:58 pm

Related Questions

I have just started reading Modern C++ Design Generic programming and Design Patterns Applied
I have to do this for a basic C++ lecture at my university, so
For my current project I want to be able to load some classes from
In Visual C++ 2008 Express Edition when adding forms all of the default handlers
I'm working on kernel design, and I've got some questions concerning paging. The basic

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.