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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:22:48+00:00 2026-06-16T05:22:48+00:00

Up to now I’ve allocated a buffer for some data processing and deleted it

  • 0

Up to now I’ve allocated a buffer for some data processing and deleted it afterwards. Since the code got bigger and caught exceptions at some points can occur, i thought about making it safer with a std::unique_ptr and came up with those solutions:

unique_ptr<char, void (*)(void *)> p1( (char*)operator new(bufSize), operator delete);
unique_ptr<char[], void (*)(void *) > p2( (char*)operator new(bufSize), operator delete);

memcpy(&((p1.get())[0]), "xyz", 3);
memcpy(&(p2[0]), "xyz", 3);

char x1 = p1.get()[0];
char x2 = p2[0];

to me the first solutions (p1) seems to be the correct one, but it’s tedious to have to write (p1.get())[…]

the second solution would be the convenient one, but here is my question:

std::unique_ptr<T[]> seems to be templated in a special way supporting operator[], which makes me wonder, if i use std::unique_ptr<T[]> with a custom new and delete operation, is there anything that could go wrong with the “operator[] or any other functions or is the second solution (p2) fine?

  • 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-16T05:22:49+00:00Added an answer on June 16, 2026 at 5:22 am

    std::unique_ptr is specialised for arrays. You can just write the following:

    std::unique_ptr<char[]> str(new char[4]);
    char foo[] = "str";
    std::copy(foo, foo + sizeof(foo), &str[0]);
    

    If that didn’t exist you could do something similar yourself, or you could write a using alias and a make_array function which does the setting up for you and returns the correct type, then using it would be as simple as

    auto str = make_array<char>(4);
    

    … or something like that.

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

Sidebar

Related Questions

Now for some reason i can't get this code to work When i move
now i need to insert some data from the sqlserver into a word,i know
Now I try to developing an application related to Image processing, for that I
Now I'm trying some tests to erase the actual touch to beam in NFC
Now here is another problem.. my code: <script type=text/javascript> function validate_form () { if
Now that frameworks like GWT, Morfik etc exist which compile Java client side code
Now that I'm working in F#, is it time to start writing up some
Now I have a file with many data in it. And I know the
Now I have the following MFC SDI application code,this code comes from my view
Now All of this code works fine in Firfox but in IE the divs

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.