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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:28:10+00:00 2026-06-16T03:28:10+00:00

I have a data buffer stored in a shared_ptr<void> . This buffer is organized

  • 0

I have a data buffer stored in a shared_ptr<void>.

This buffer is organized in several encapsulated layers so that I end up with:

-----------------------------------...
- Header 1 | Header 2 | Data
-----------------------------------...

(Actually it’s an Ethernet packet where I decapsulate the layers one after the other).

Once I read Header 1, I would like to pass the rest of the packet to the next layer for reading, so I would like to create a pointer to :

-----------------------...
- Header 2 | Data
-----------------------...

It would be very easy with a raw pointer, as it would just be a matter of pointer arithmetic. But how can I achieve that with a shared_ptr ? (I use boost::shared_ptr) :

  • I cannot create a new shared_ptr to “first shared_ptr.get() + offset” because it makes no sense to get the ownership to just Header 2 + Data (and delete would crash eventually)
  • I do not want to copy the data because it would be silly
  • I want the ownership on the whole buffer to be shared between the two objects (ie. as long as the parent object or the one which requires only Header 2 needs the data, the data should not be deleted).

I could wrap that up in a structure like boost::tuple<shared_ptr<void>, int /*offset*/, int /*length*/> but I wonder if there is a more convenient / elegant way to achieve that result.

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-16T03:28:11+00:00Added an answer on June 16, 2026 at 3:28 am

    By the way, I just used a simple wrapper that I reproduce here if someone ever stumbles on the question.

    class DataWrapper {
    public:
        DataWrapper (shared_ptr<void> pData, size_t offset, size_t length) : mpData(pData), mOffset(offset), mLength(length) {}
    
        void* GetData() {return (unsigned char*)mpData.get() + mOffset;}
        // same with const...
        void SkipData (size_t skipSize) { mOffset += skipSize; mLength -= skipSize; }
        void GetLength const {return mLength;}
    
        // Then you can add operator+, +=, (void*), -, -=
        // if you need pointer-like semantics.
        // Also a "memcpy" member function to copy just this buffer may be useful
        // and other helper functions if you need
    
    private:
        shared_ptr<void> mpData;
        size_t mOffset, mLength;
    };
    

    Just be careful when you use GetData: be sure that the buffer will not be freed while you use the unsafe void*. It is safe to use the void* as long as you know the DataWrapper object is alive (because it holds a shared_ptr to the buffer, so it prevents it from being freed).

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

Sidebar

Related Questions

I have one thread, that is sending data stored in a buffer of type
So.. I have a buffer with MP3 data (If I would save this buffer
I have some stored procedures to execute that use dbms_output.put_line() to put data into
I'm trying to figure out the data structure that is stored in 'buffer' after
I have 'data.xlsx' file with cells of numbers and letters. when I run this
I have data-segment attribute in my body tag that is changed by a slider.
I have data stored in three columns of Excel Column A: Serial Number Column
I have data from two different sources that I need to combine. Some data
Some programming languages have support for strings, that are stored as folows: For example,
This question is probably for MySQL experts and admins that have done this sort

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.