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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:39:53+00:00 2026-06-18T08:39:53+00:00

I’m currently sitting with an assignment in which you are supposed to read spheres

  • 0

I’m currently sitting with an assignment in which you are supposed to read spheres from a file.
A typhical input from the file can look like this: “0.182361 -0.017057 0.129582 0.001816”.
Representing the, x, y and z coordinates plus the Spheres radius.
While reading the File I’m using my own method: “AddSphere” which adds a sphere into an array.

void Array::AddSphere(MySphere inp)
{
if (length == 10000 || length == 200000 || length == 30000)
{
    ResizeBuffer(); 
}
this->length++;
*arr = inp;
arr++;
//this->length++;
}

The class “Array” is supposed to be like a holder for all spheres and is containing the variable “arr” that is pointing on the current element.

    class Array
    {
    public :

    int length;
    void AddSphere(MySphere inp);
    int arrLength();
    void RemoveAt(int pos);
    void AddFromFile();
    MySphere * Buffer;
    void CreatenewBuffer();
    private:
    MySphere * arr;

    public:Array()
       {
           Buffer = new MySphere[10000];
           arr = Buffer;
           length = 0;
       }
       ~Array()
       {
           delete[] arr;
           delete[] Buffer;
       }
};

It is also containing “Buffer” that is pointing on the first element in “arr”. So now what is my problem? The problem is that i want to be able to dynamicly increase the Buffer’s size whenever “length” equals a specified value. Lets say my file contains 15000 elements. Then i need to increase the Buffers size to be able to have the correct length on the array.with ResizeBuffer() i try to do that.

    void Array::ResizeBuffer()
{

    MySphere * tempBuff = new MySphere[length+10000];
    memcpy(tempBuff, Buffer, sizeof((MySphere)*Buffer));
    this->Buffer = new MySphere[length+10000];
    arr = Buffer;
    memcpy(Buffer, tempBuff, sizeof((MySphere)*tempBuff));


    //int a = 0;

    }

But for some reason i only get the last 5000 elements in my output instead of all the 15000. I figured that it has something to do with the pointer of arr not being pointed to the whole buffer, but none of my attempts work. So why is this happening?

Thank you for your time!

  • 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-18T08:39:55+00:00Added an answer on June 18, 2026 at 8:39 am

    Couple of problems:

    The dynamic data is only allocated once (though you have two pointers to the arrea).

    Array()
       {
           Buffer = new MySphere[10000];
           arr = Buffer;
           length = 0;
       }
    

    SO you can only delete it once (one allocation one destruction).

       ~Array()
       {
           delete[] arr;
           delete[] Buffer;  // Double delete.
       }
    

    When copying objects you can not use memcpy (unless the object falls under a very special subcategory). If you are going to use C++ with methods and all its other features this is unlikely to be the case. Prefer to use std::copy() to copy the content from one array to another.

    MySphere * tempBuff = new MySphere[length+10000];
    memcpy(tempBuff, Buffer, sizeof((MySphere)*Buffer));
    this->Buffer = new MySphere[length+10000];
    arr = Buffer;
    memcpy(Buffer, tempBuff, sizeof((MySphere)*tempBuff));
    

    Also why are you copying it twice?

    Note in addition to using std::copy
    The resize should happen in three distinct phases.

    1. Allocate and copy data       // Dangerous may throw
    2. Reset object                 // Safe
    3. Release old resources.       // Dangerous may throw
    

    So this is how it should look.

    // Phase 1: Allocate and copy.
    std::unique_ptr<MySphere> tempBuffer = new MySphere[length+1000];   // Hold in a smart pointer
                                                                        // For exception safety
    // Prefer to use std::copy. It will use the objects copy constructor
    std::copy(Buffer, Buffer+length, MySphere);                         // Copies the objects correctly. 
    
    
    // Everything worked so Phase 2. Reset the state of the object.
    // We can now put the tempBuffer into the object
    MySphere* toDelete = Buffer;                                        // keep old buffer for stage 3.
    Buffer = tempBuffer.release();
    length += 1000;
    
    // Phase 3
    // State of the new object is consistent.
    // We can now delete the old array
    delete [] toDelete;
    

    If you are not allowed to use a smart pointer. Then replace phase 1 with this:

    // Phase 1: Allocate and copy.
    MySphere* tempBuffer = new MySphere[length+1000];
    
    try                                                 // For exception safety
    {
        // Prefer to use std::copy. It will use the objects copy constructor
        std::copy(Buffer, Buffer+length, MySphere);     // Copies the objects correctly. 
    }
    catch(...)
    {
        delete [] tempBuffer;
        throw;
    }
    

    Since your class has taken ownership of a dynamic allocated object. Your class should also implement the rule of three. This means you need to define the copy constructor and the assignment operator.

    What you really need to have dynamic array are three things:

        1) A pointer to the buffer.
        2) The current size the user knows about (reported by size)
        3) The actual size. At which point you need to resize
    

    Here is an example of how to implement the rule of three for an array:

    https://stackoverflow.com/a/255744/14065

    Anything else is superfluous.

    Unnecessary in your case as this is a project. But for extra marks look up how to use placement new. This should prevent extra initialization of objects that don’t really exist in your array.

    An example of how to use place new:

    https://stackoverflow.com/a/13994853/14065

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I want use html5's new tag to play a wav file (currently only supported
I would like to run a str_replace or preg_replace which looks for certain words
I am trying to render a haml file in a javascript response like so:
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I am currently running into a problem where an element is coming back from
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.