Set shared_ptr with new_pointer that is old_pointer + offset












11















Here is a smart pointer: std::shared_ptr<char> p(new char[size]) which represents array filled with raw binary file content. After (and only after) the whole array is copied from file to RAM, I can parse it, and during this I retrieve some header information (a few first dwords). Then actual data follows.



Without giving much more context, it's handy for me to to set mentioned shared pointer to new address that is beginning of actual data. This address is still in alocated memory. But how to set without losing it?



A question is (yes/no): Is it possible to set p to offset of prevous pointer, without invoking deletion of data?










share|improve this question




















  • 2





    Unfortunately, shared pointers don't work this way, and this cannot be done. You will need to find some other way to accomplish this goal. The short answer is: "no".

    – Sam Varshavchik
    6 hours ago








  • 2





    This sounds like a case where you'd want to pass a raw pointer instead of a shared_ptr.

    – Jeremy Friesner
    6 hours ago











  • ^^^ that's what it seems. Or tote around a offset value and base your data-read from p.get() + header_len. Trying to change the shared pointer p itself seems odd here.

    – WhozCraig
    6 hours ago













  • @JeremyFriesner I know and I will handle this situation, because I coded this shared pointer myself and futhermore I can read from file and parse simultaneously (lied in question). Just was wondering are smart pointers that good for low-level binary work. Thanks for suggestion.

    – Alex Larionov
    6 hours ago











  • Use vector<char> instead of a raw array.

    – Ulrich Eckhardt
    6 hours ago
















11















Here is a smart pointer: std::shared_ptr<char> p(new char[size]) which represents array filled with raw binary file content. After (and only after) the whole array is copied from file to RAM, I can parse it, and during this I retrieve some header information (a few first dwords). Then actual data follows.



Without giving much more context, it's handy for me to to set mentioned shared pointer to new address that is beginning of actual data. This address is still in alocated memory. But how to set without losing it?



A question is (yes/no): Is it possible to set p to offset of prevous pointer, without invoking deletion of data?










share|improve this question




















  • 2





    Unfortunately, shared pointers don't work this way, and this cannot be done. You will need to find some other way to accomplish this goal. The short answer is: "no".

    – Sam Varshavchik
    6 hours ago








  • 2





    This sounds like a case where you'd want to pass a raw pointer instead of a shared_ptr.

    – Jeremy Friesner
    6 hours ago











  • ^^^ that's what it seems. Or tote around a offset value and base your data-read from p.get() + header_len. Trying to change the shared pointer p itself seems odd here.

    – WhozCraig
    6 hours ago













  • @JeremyFriesner I know and I will handle this situation, because I coded this shared pointer myself and futhermore I can read from file and parse simultaneously (lied in question). Just was wondering are smart pointers that good for low-level binary work. Thanks for suggestion.

    – Alex Larionov
    6 hours ago











  • Use vector<char> instead of a raw array.

    – Ulrich Eckhardt
    6 hours ago














11












11








11








Here is a smart pointer: std::shared_ptr<char> p(new char[size]) which represents array filled with raw binary file content. After (and only after) the whole array is copied from file to RAM, I can parse it, and during this I retrieve some header information (a few first dwords). Then actual data follows.



Without giving much more context, it's handy for me to to set mentioned shared pointer to new address that is beginning of actual data. This address is still in alocated memory. But how to set without losing it?



A question is (yes/no): Is it possible to set p to offset of prevous pointer, without invoking deletion of data?










share|improve this question
















Here is a smart pointer: std::shared_ptr<char> p(new char[size]) which represents array filled with raw binary file content. After (and only after) the whole array is copied from file to RAM, I can parse it, and during this I retrieve some header information (a few first dwords). Then actual data follows.



Without giving much more context, it's handy for me to to set mentioned shared pointer to new address that is beginning of actual data. This address is still in alocated memory. But how to set without losing it?



A question is (yes/no): Is it possible to set p to offset of prevous pointer, without invoking deletion of data?







c++ shared-ptr smart-pointers pointer-arithmetic






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 5 hours ago









Christophe

39.8k43576




39.8k43576










asked 6 hours ago









Alex LarionovAlex Larionov

13916




13916








  • 2





    Unfortunately, shared pointers don't work this way, and this cannot be done. You will need to find some other way to accomplish this goal. The short answer is: "no".

    – Sam Varshavchik
    6 hours ago








  • 2





    This sounds like a case where you'd want to pass a raw pointer instead of a shared_ptr.

    – Jeremy Friesner
    6 hours ago











  • ^^^ that's what it seems. Or tote around a offset value and base your data-read from p.get() + header_len. Trying to change the shared pointer p itself seems odd here.

    – WhozCraig
    6 hours ago













  • @JeremyFriesner I know and I will handle this situation, because I coded this shared pointer myself and futhermore I can read from file and parse simultaneously (lied in question). Just was wondering are smart pointers that good for low-level binary work. Thanks for suggestion.

    – Alex Larionov
    6 hours ago











  • Use vector<char> instead of a raw array.

    – Ulrich Eckhardt
    6 hours ago














  • 2





    Unfortunately, shared pointers don't work this way, and this cannot be done. You will need to find some other way to accomplish this goal. The short answer is: "no".

    – Sam Varshavchik
    6 hours ago








  • 2





    This sounds like a case where you'd want to pass a raw pointer instead of a shared_ptr.

    – Jeremy Friesner
    6 hours ago











  • ^^^ that's what it seems. Or tote around a offset value and base your data-read from p.get() + header_len. Trying to change the shared pointer p itself seems odd here.

    – WhozCraig
    6 hours ago













  • @JeremyFriesner I know and I will handle this situation, because I coded this shared pointer myself and futhermore I can read from file and parse simultaneously (lied in question). Just was wondering are smart pointers that good for low-level binary work. Thanks for suggestion.

    – Alex Larionov
    6 hours ago











  • Use vector<char> instead of a raw array.

    – Ulrich Eckhardt
    6 hours ago








2




2





Unfortunately, shared pointers don't work this way, and this cannot be done. You will need to find some other way to accomplish this goal. The short answer is: "no".

– Sam Varshavchik
6 hours ago







Unfortunately, shared pointers don't work this way, and this cannot be done. You will need to find some other way to accomplish this goal. The short answer is: "no".

– Sam Varshavchik
6 hours ago






2




2





This sounds like a case where you'd want to pass a raw pointer instead of a shared_ptr.

– Jeremy Friesner
6 hours ago





This sounds like a case where you'd want to pass a raw pointer instead of a shared_ptr.

– Jeremy Friesner
6 hours ago













^^^ that's what it seems. Or tote around a offset value and base your data-read from p.get() + header_len. Trying to change the shared pointer p itself seems odd here.

– WhozCraig
6 hours ago







^^^ that's what it seems. Or tote around a offset value and base your data-read from p.get() + header_len. Trying to change the shared pointer p itself seems odd here.

– WhozCraig
6 hours ago















@JeremyFriesner I know and I will handle this situation, because I coded this shared pointer myself and futhermore I can read from file and parse simultaneously (lied in question). Just was wondering are smart pointers that good for low-level binary work. Thanks for suggestion.

– Alex Larionov
6 hours ago





@JeremyFriesner I know and I will handle this situation, because I coded this shared pointer myself and futhermore I can read from file and parse simultaneously (lied in question). Just was wondering are smart pointers that good for low-level binary work. Thanks for suggestion.

– Alex Larionov
6 hours ago













Use vector<char> instead of a raw array.

– Ulrich Eckhardt
6 hours ago





Use vector<char> instead of a raw array.

– Ulrich Eckhardt
6 hours ago












1 Answer
1






active

oldest

votes


















19














Yes this is possible. You can use constructor 8, the aliasing constructor from this reference: https://en.cppreference.com/w/cpp/memory/shared_ptr/shared_ptr



// make sure you use an array deleter
std::shared_ptr<char> osp(new char[1024], std::default_delete<char>());

// load the data into your buffer at osp.get()

// Find the offset in the data by parsing
auto const offset = parse_buffer_for_offset(osp.get());

// Now set a new offset into the data
std::shared_ptr<char> nsp(osp, osp.get() + offset);


Now nsp.get() returns the offset address but the original array will get deleted properly.






share|improve this answer





















  • 1





    Yes this is exactly what the aliasing constructor is for!

    – n.m.
    6 hours ago











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54737556%2fset-shared-ptr-with-new-pointer-that-is-old-pointer-offset%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









19














Yes this is possible. You can use constructor 8, the aliasing constructor from this reference: https://en.cppreference.com/w/cpp/memory/shared_ptr/shared_ptr



// make sure you use an array deleter
std::shared_ptr<char> osp(new char[1024], std::default_delete<char>());

// load the data into your buffer at osp.get()

// Find the offset in the data by parsing
auto const offset = parse_buffer_for_offset(osp.get());

// Now set a new offset into the data
std::shared_ptr<char> nsp(osp, osp.get() + offset);


Now nsp.get() returns the offset address but the original array will get deleted properly.






share|improve this answer





















  • 1





    Yes this is exactly what the aliasing constructor is for!

    – n.m.
    6 hours ago
















19














Yes this is possible. You can use constructor 8, the aliasing constructor from this reference: https://en.cppreference.com/w/cpp/memory/shared_ptr/shared_ptr



// make sure you use an array deleter
std::shared_ptr<char> osp(new char[1024], std::default_delete<char>());

// load the data into your buffer at osp.get()

// Find the offset in the data by parsing
auto const offset = parse_buffer_for_offset(osp.get());

// Now set a new offset into the data
std::shared_ptr<char> nsp(osp, osp.get() + offset);


Now nsp.get() returns the offset address but the original array will get deleted properly.






share|improve this answer





















  • 1





    Yes this is exactly what the aliasing constructor is for!

    – n.m.
    6 hours ago














19












19








19







Yes this is possible. You can use constructor 8, the aliasing constructor from this reference: https://en.cppreference.com/w/cpp/memory/shared_ptr/shared_ptr



// make sure you use an array deleter
std::shared_ptr<char> osp(new char[1024], std::default_delete<char>());

// load the data into your buffer at osp.get()

// Find the offset in the data by parsing
auto const offset = parse_buffer_for_offset(osp.get());

// Now set a new offset into the data
std::shared_ptr<char> nsp(osp, osp.get() + offset);


Now nsp.get() returns the offset address but the original array will get deleted properly.






share|improve this answer















Yes this is possible. You can use constructor 8, the aliasing constructor from this reference: https://en.cppreference.com/w/cpp/memory/shared_ptr/shared_ptr



// make sure you use an array deleter
std::shared_ptr<char> osp(new char[1024], std::default_delete<char>());

// load the data into your buffer at osp.get()

// Find the offset in the data by parsing
auto const offset = parse_buffer_for_offset(osp.get());

// Now set a new offset into the data
std::shared_ptr<char> nsp(osp, osp.get() + offset);


Now nsp.get() returns the offset address but the original array will get deleted properly.







share|improve this answer














share|improve this answer



share|improve this answer








edited 6 hours ago

























answered 6 hours ago









GalikGalik

34.2k34980




34.2k34980








  • 1





    Yes this is exactly what the aliasing constructor is for!

    – n.m.
    6 hours ago














  • 1





    Yes this is exactly what the aliasing constructor is for!

    – n.m.
    6 hours ago








1




1





Yes this is exactly what the aliasing constructor is for!

– n.m.
6 hours ago





Yes this is exactly what the aliasing constructor is for!

– n.m.
6 hours ago




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54737556%2fset-shared-ptr-with-new-pointer-that-is-old-pointer-offset%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Fluorita

Hulsita

Península de Txukotka