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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:46:54+00:00 2026-06-13T01:46:54+00:00

I am working on a physics engine related project. In the C++ code below:

  • 0

I am working on a physics engine related project. In the C++ code below:

#include <iostream>
#include <vector>

struct Vector3
{
  float x;
  float y;
  float z;
};

struct object
{
  std::vector < std::vector < Vector3 > > set_vertices;
  int ID;
  int Type;
};

class World
{
  public:
    std::vector< object > objects;

  private:
    // other members
};

int main (void)
{
  World world;

  // How to fill in "objects" of "world"?
  //   Is this safe to do?
  world.objects.resize(5);
  //   What about the way of allocating "set_vertices" below?
  for(size_t i=0; i<world.objects.size(); i++)
  {
    world.objects[i].set_vertices.resize(12, std::vector < Vector3 >(3));
    for(size_t j=0; j<world.objects[i].set_vertices.size(); j++)
    {
      for(size_t k=0; k<world.objects[i].set_vertices[j].size(); k++)
      {
        world.objects[i].set_vertices[j][k].x = 0.0f;
        world.objects[i].set_vertices[j][k].y = 0.0f;
        world.objects[i].set_vertices[j][k].z = 0.0f;
        world.objects[i].ID = i;
        world.objects[i].Type = 0;
      }
    }
  }

  return 0;
}

is the way I have allocated memory for objects of world safe? There would be any memory related issue? Is there any better way of initializing objects dynamically (i.e. not in constructor)? If so, how? thanks.

  • 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-06-13T01:46:55+00:00Added an answer on June 13, 2026 at 1:46 am

    I’d imagine that using RAII and having the constructor and destructor of object to take care of the memory would be a better way of doing this.

    Adding in a new object into world you can make use of std::vector ability to resize dynamically by just calling push_back() instead of needing to create the loop that has i as the index. To avoid unnecessary resizes if you know how many elements you are adding in advance you can call .reserve(n) where n is the number of elements you are adding.

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

Sidebar

Related Questions

I've been working on a computational physics project (plotting related rates of chemical reactants
I am working on making a physics engine (with the help of a book),
I'm working on a educational physics project and I've chosen Box2D as my main
I am considering working on a rigid body parallel physics engine, with MPI, as
I'm working on a purely continuous physics engine, and I need to choose algorithms
Im working with AndEngine in eclipse (which is a 2d physics/tiling engine) Everything works
I am currently working on an amateur physic engine for a school project but
I an a graduate student of nuclear physics currently working on a data analysis
I am a graduate student of physics and I am working on writing some
Salut, I'm currently working on a pool game in flash, using box2d for physics

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.