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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:40:05+00:00 2026-06-05T09:40:05+00:00

for example, I try to write my own vector , so I just write

  • 0

for example, I try to write my own vector, so I just write its assign function like this

template <typename T>
void Vector<T> :: assign(T *start, T *end)
{
    if (end - start > _capacity)
    {
        resize(end - start);
    }
    _size = end - start;
    delete []ptr;
    ptr = new T[_capacity];
    memcpy(ptr, start, end - start);
}

I have new the pointer ptr before, but I can copy all I what between the pointer start and end

why? Thankyou very much.

  • 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-05T09:40:06+00:00Added an answer on June 5, 2026 at 9:40 am

    First problem is that this only works for simple types (read POD).
    Anything with a constructor/destructor needs to have them called.

    Second this is not exception safe.
    It does not even provide the basic guarantee let a lone the strong guarantee.

    You need to do all exception unsafe work before modifying the object. This means the new must be done before you modify the object (and definitely before the free). Otherwise you could potentially throw leaving the object in an invalid state (that might not seem bad, but what if you catch the exception and continue you now have an object that contains a pointer to released memory).

    So even if you use std::copy() you still have done the wrong thing.
    Personally I think the suggestion of std::copy() is a red herring. It will copy the data correctly but you are still writing your method badly. You need to use a twist on the copy and swap idium.

    template <typename T>
    void Vector<T> :: assign(T *start, T *end)
    {
        Vector<T> tmp(start, end);  // construct a temp object that allocates the memory.
    
    
    
        swap(tmp);                  // Swap the current object and the tmp objects data.
                                    // When the tmp object goes out of scope it will delete
                                    // what was the current objects data
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can PHP dissect its own syntax? For example, I'd like to write a function
For example, I try to do something like this: - (BOOL)compare:(NSDecimal)leftOperand greaterThan:(NSDecimal)rightOperand { BOOL
I try to develop my own little framework. For this, I'd like to read
I am working with this example to try and learn Sencha Touch. I have
I try to use this example to get images urls from http://www.nydailynews.com/cmlink/NYDN.Article.rss but no
I'll try and give an example what i want: (this isn't what I'm doing,
If I try to implement this simple-as-possible example: Update Content with AJAXRefreshRequest it does
I'll try to explain this with an example. I'm writing a chat application. There
I am using a modified version of the TaskCloud example to try and read/write
How can I know during runtime that my code threw a Warning? example try

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.