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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:03:50+00:00 2026-05-24T10:03:50+00:00

Background I’m working with the Intel IPP Cryptographic Libraries for testing. They define several

  • 0

Background

I’m working with the Intel IPP Cryptographic Libraries for testing.

They define several opaque structs used for shared contexts over things like hashing and encryption which, of course, cannot be directly instantiated.

To initialize one of these opaque structs, you query for a byte size and then dynamically allocate some bytes and cast to the struct pointer.

Their examples work something like this:

int byteSize = 0;
ippsSHA256GetSize(&byteSize);

// IppsSHA256State shaCtx; // Error: incomplete type is not allowed
IppsSHA256State * shaCtx = (IppsSHA256State *)(new uint8_t[byteSize]);
// use shaCtx
delete [] (uint8_t *)shaCtx;

Question

What’s the proper way to wrap this in a scoped pointer class so that I don’t have to worry about deallocation?


Things I’ve Tried

I would think the following would not be safe since the call to delete in the destructor would be on the T type rather than a delete [] on the array that was actually allocated:

boost::scoped_ptr<IppsSHA256State> ctx(
    reinterpret_cast<IppsSHA256State *>(new uint8_t[byteSize])
    );

Another (simplified) option I’ve considered is a simple scoped pointer class of my own, but the casting there makes me unsure if this is correct, though my understanding of reinterpret_cast is that when cast back to the original type, there shouldn’t be any ambiguity:

template <typename T>
class IppsScopedState
{
public:
    explicit IppsScopedState(size_t byteSize)
        : _ptr(reinterpret_cast<T *>(new uint8_t[byteSize]))
    {}

    T * get() const { return _ptr; }

    ~IppsScopedState(void) {
        if (_ptr) delete [] reinterpret_cast<uint8_t *>(_ptr);
    }
private:
    T * _ptr;
    //NUKE_COPYASSIGN_CONSTRUCTORS
};

Finally, I’ve considered a slight variation on the above:

template <typename T>
class IppsScopedState
{
public:
    explicit IppsScopedState(size_t byteSize)
        : _ptr(new uint8_t[byteSize])
    {}

    T * get() const { return reinterpret_cast<T *>(_ptr); }

    ~IppsScopedState(void) {
        if (_ptr) delete [] _ptr;
    }
private:
    uint8_t * _ptr;
    //NUKE_COPYASSIGN_CONSTRUCTORS
};

In either case, the usage would be like this:

IppsScopedState<IppsSHA256State> ctx(byteSize); // after querying for the byteSize, of course
  • 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-24T10:03:51+00:00Added an answer on May 24, 2026 at 10:03 am

    You are correct that this:

    boost::scoped_ptr<IppsSHA256State> ctx(
        reinterpret_cast<IppsSHA256State *>(new uint8_t[byteSize])
    );
    

    Is a bad idea, for two reasons: delete will be called instead of delete[], and it will be deleting the wrong type.

    boost::scoped_array Should work fine:

    boost::scoped_array<uint8_t> temparray (new uint8_t[byteSize]);
    IppsSHA256State * shaCtx = (IppsSHA256State *)(temparray.get());
    

    But that gives you two local variables, and you have to remember not to keep shaCtx around after you’re done with temparray. So, rolling your own wrapper is a very appealing option, because it gives you that safety.

    Your current IppsScopedState is fine, but I’d suggest tweaking it slightly, to make use of boost::scoped_array internally, add the operator-> accessor, and const access:

    template <typename T>
    class IppsScopedState
    {
    public:
        explicit IppsScopedState(size_t byteSize)
            : _ptr(new uint8_t[byteSize])
        {}
    
        const T* operator->() const { return get(); }
        T* operator->() { return get(); }
        const T* get() const  { return reinterpret_cast<const T*> (_ptr.get()); }
        T* get()   { return reinterpret_cast<T*>(_ptr.get()); }
    
    private:
        boost::scoped_array<uint8_t> _ptr;
        //NUKE_COPYASSIGN_CONSTRUCTORS
    };
    

    Then you can use this wrapper easily:

    IppsScopedState<IppsSHA256State> shaCtx (byteSize);
    shaCtx->member;  // access any member of IppsSHA256State
    some_function(shaCtx.get());  // pass the IppsSHA256State* to a method
    

    Depending on your needs the operator-> and const get() may be overkill, and unnecessary.

    So, in the end, I’d recommend your custom wrapper, and using reinterpret_cast over the old c-style cast syntax.

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

Sidebar

Related Questions

Background We have a Windows .NET application used by our field employees who travel
Background: I'm working on a system where the developers seem to be using a
Background: I'm using Google's protobuf , and I would like to read/write several gigabytes
Background: I have a little video playing app with a UI inspired by the
Background: At my company we are developing a bunch applications that are using the
Background: Some time ago, I built a system for recording and categorizing application crashes
Background: I need to reserve an amount of memory below 0xA0000 prior to my
Background I am writing and using a very simple CGI-based (Perl) content management tool
Background I have a massive db for a SharePoint site collection. It is 130GB
Background I am trying to create a copy of a business object I have

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.