Set shared_ptr with new_pointer that is old_pointer + offset












14















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
    9 hours ago








  • 3





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

    – Jeremy Friesner
    9 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
    9 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
    9 hours ago











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

    – Ulrich Eckhardt
    9 hours ago
















14















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
    9 hours ago








  • 3





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

    – Jeremy Friesner
    9 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
    9 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
    9 hours ago











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

    – Ulrich Eckhardt
    9 hours ago














14












14








14








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 8 hours ago









Christophe

39.8k43576




39.8k43576










asked 9 hours ago









Alex LarionovAlex Larionov

15416




15416








  • 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
    9 hours ago








  • 3





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

    – Jeremy Friesner
    9 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
    9 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
    9 hours ago











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

    – Ulrich Eckhardt
    9 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
    9 hours ago








  • 3





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

    – Jeremy Friesner
    9 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
    9 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
    9 hours ago











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

    – Ulrich Eckhardt
    9 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
9 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
9 hours ago






3




3





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

– Jeremy Friesner
9 hours ago





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

– Jeremy Friesner
9 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
9 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
9 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
9 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
9 hours ago













Use vector<char> instead of a raw array.

– Ulrich Eckhardt
9 hours ago





Use vector<char> instead of a raw array.

– Ulrich Eckhardt
9 hours ago












1 Answer
1






active

oldest

votes


















24














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.



Note: The offset is a property of each shared_ptr so if you copy the shared_ptr nsp you get another shared_ptr with the same offset. This works whether you construct a new copy or assign a copy to an existing shared_ptr.



This means you can have different shared_ptr with different offsets that all manage the same, underlying resource which will only be cleaned up after all shared_ptr are destroyed.



To see this in operation consider the following code:



std::shared_ptr<char> original_sp(new char[1024], std::default_delete<char>());

std::shared_ptr<char> offset_100_sp1(original_sp, original_sp.get() + 100);
std::shared_ptr<char> offset_100_sp2 = offset_100_sp1;

std::shared_ptr<char> offset_200_sp1(original_sp, original_sp.get() + 200);
std::shared_ptr<char> offset_200_sp2 = offset_200_sp1;

std::cout << "nPointers managing the array: " << original_sp.use_count() << 'n';

std::cout << "nOffset 100 pointers:" << 'n';
std::cout << std::distance(original_sp.get(), offset_100_sp1.get()) << 'n';
std::cout << std::distance(original_sp.get(), offset_100_sp2.get()) << 'n';

std::cout << "nOffset 200 pointers:" << 'n';
std::cout << std::distance(original_sp.get(), offset_200_sp1.get()) << 'n';
std::cout << std::distance(original_sp.get(), offset_200_sp2.get()) << 'n';


Output:



Pointers managing the array: 5

Offset 100 pointers:
100
100

Offset 200 pointers:
200
200





share|improve this answer





















  • 1





    Yes this is exactly what the aliasing constructor is for!

    – n.m.
    9 hours ago











  • Of course, while this shares ownership with the shared_ptr used during creation (and all its clones), the offset is not applied to any of the existing clones. So it's not clear to meet whether this meets the question's stated goal of "setting" the shared_ptr

    – Ben Voigt
    2 hours ago











  • @BenVoigt I have made a note to the effect that the offset belongs to each shared_ptr and that offsets are transferred to another shared_ptr when copied (either copy constructed or copy assigned).

    – Galik
    1 hour 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









24














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.



Note: The offset is a property of each shared_ptr so if you copy the shared_ptr nsp you get another shared_ptr with the same offset. This works whether you construct a new copy or assign a copy to an existing shared_ptr.



This means you can have different shared_ptr with different offsets that all manage the same, underlying resource which will only be cleaned up after all shared_ptr are destroyed.



To see this in operation consider the following code:



std::shared_ptr<char> original_sp(new char[1024], std::default_delete<char>());

std::shared_ptr<char> offset_100_sp1(original_sp, original_sp.get() + 100);
std::shared_ptr<char> offset_100_sp2 = offset_100_sp1;

std::shared_ptr<char> offset_200_sp1(original_sp, original_sp.get() + 200);
std::shared_ptr<char> offset_200_sp2 = offset_200_sp1;

std::cout << "nPointers managing the array: " << original_sp.use_count() << 'n';

std::cout << "nOffset 100 pointers:" << 'n';
std::cout << std::distance(original_sp.get(), offset_100_sp1.get()) << 'n';
std::cout << std::distance(original_sp.get(), offset_100_sp2.get()) << 'n';

std::cout << "nOffset 200 pointers:" << 'n';
std::cout << std::distance(original_sp.get(), offset_200_sp1.get()) << 'n';
std::cout << std::distance(original_sp.get(), offset_200_sp2.get()) << 'n';


Output:



Pointers managing the array: 5

Offset 100 pointers:
100
100

Offset 200 pointers:
200
200





share|improve this answer





















  • 1





    Yes this is exactly what the aliasing constructor is for!

    – n.m.
    9 hours ago











  • Of course, while this shares ownership with the shared_ptr used during creation (and all its clones), the offset is not applied to any of the existing clones. So it's not clear to meet whether this meets the question's stated goal of "setting" the shared_ptr

    – Ben Voigt
    2 hours ago











  • @BenVoigt I have made a note to the effect that the offset belongs to each shared_ptr and that offsets are transferred to another shared_ptr when copied (either copy constructed or copy assigned).

    – Galik
    1 hour ago
















24














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.



Note: The offset is a property of each shared_ptr so if you copy the shared_ptr nsp you get another shared_ptr with the same offset. This works whether you construct a new copy or assign a copy to an existing shared_ptr.



This means you can have different shared_ptr with different offsets that all manage the same, underlying resource which will only be cleaned up after all shared_ptr are destroyed.



To see this in operation consider the following code:



std::shared_ptr<char> original_sp(new char[1024], std::default_delete<char>());

std::shared_ptr<char> offset_100_sp1(original_sp, original_sp.get() + 100);
std::shared_ptr<char> offset_100_sp2 = offset_100_sp1;

std::shared_ptr<char> offset_200_sp1(original_sp, original_sp.get() + 200);
std::shared_ptr<char> offset_200_sp2 = offset_200_sp1;

std::cout << "nPointers managing the array: " << original_sp.use_count() << 'n';

std::cout << "nOffset 100 pointers:" << 'n';
std::cout << std::distance(original_sp.get(), offset_100_sp1.get()) << 'n';
std::cout << std::distance(original_sp.get(), offset_100_sp2.get()) << 'n';

std::cout << "nOffset 200 pointers:" << 'n';
std::cout << std::distance(original_sp.get(), offset_200_sp1.get()) << 'n';
std::cout << std::distance(original_sp.get(), offset_200_sp2.get()) << 'n';


Output:



Pointers managing the array: 5

Offset 100 pointers:
100
100

Offset 200 pointers:
200
200





share|improve this answer





















  • 1





    Yes this is exactly what the aliasing constructor is for!

    – n.m.
    9 hours ago











  • Of course, while this shares ownership with the shared_ptr used during creation (and all its clones), the offset is not applied to any of the existing clones. So it's not clear to meet whether this meets the question's stated goal of "setting" the shared_ptr

    – Ben Voigt
    2 hours ago











  • @BenVoigt I have made a note to the effect that the offset belongs to each shared_ptr and that offsets are transferred to another shared_ptr when copied (either copy constructed or copy assigned).

    – Galik
    1 hour ago














24












24








24







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.



Note: The offset is a property of each shared_ptr so if you copy the shared_ptr nsp you get another shared_ptr with the same offset. This works whether you construct a new copy or assign a copy to an existing shared_ptr.



This means you can have different shared_ptr with different offsets that all manage the same, underlying resource which will only be cleaned up after all shared_ptr are destroyed.



To see this in operation consider the following code:



std::shared_ptr<char> original_sp(new char[1024], std::default_delete<char>());

std::shared_ptr<char> offset_100_sp1(original_sp, original_sp.get() + 100);
std::shared_ptr<char> offset_100_sp2 = offset_100_sp1;

std::shared_ptr<char> offset_200_sp1(original_sp, original_sp.get() + 200);
std::shared_ptr<char> offset_200_sp2 = offset_200_sp1;

std::cout << "nPointers managing the array: " << original_sp.use_count() << 'n';

std::cout << "nOffset 100 pointers:" << 'n';
std::cout << std::distance(original_sp.get(), offset_100_sp1.get()) << 'n';
std::cout << std::distance(original_sp.get(), offset_100_sp2.get()) << 'n';

std::cout << "nOffset 200 pointers:" << 'n';
std::cout << std::distance(original_sp.get(), offset_200_sp1.get()) << 'n';
std::cout << std::distance(original_sp.get(), offset_200_sp2.get()) << 'n';


Output:



Pointers managing the array: 5

Offset 100 pointers:
100
100

Offset 200 pointers:
200
200





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.



Note: The offset is a property of each shared_ptr so if you copy the shared_ptr nsp you get another shared_ptr with the same offset. This works whether you construct a new copy or assign a copy to an existing shared_ptr.



This means you can have different shared_ptr with different offsets that all manage the same, underlying resource which will only be cleaned up after all shared_ptr are destroyed.



To see this in operation consider the following code:



std::shared_ptr<char> original_sp(new char[1024], std::default_delete<char>());

std::shared_ptr<char> offset_100_sp1(original_sp, original_sp.get() + 100);
std::shared_ptr<char> offset_100_sp2 = offset_100_sp1;

std::shared_ptr<char> offset_200_sp1(original_sp, original_sp.get() + 200);
std::shared_ptr<char> offset_200_sp2 = offset_200_sp1;

std::cout << "nPointers managing the array: " << original_sp.use_count() << 'n';

std::cout << "nOffset 100 pointers:" << 'n';
std::cout << std::distance(original_sp.get(), offset_100_sp1.get()) << 'n';
std::cout << std::distance(original_sp.get(), offset_100_sp2.get()) << 'n';

std::cout << "nOffset 200 pointers:" << 'n';
std::cout << std::distance(original_sp.get(), offset_200_sp1.get()) << 'n';
std::cout << std::distance(original_sp.get(), offset_200_sp2.get()) << 'n';


Output:



Pointers managing the array: 5

Offset 100 pointers:
100
100

Offset 200 pointers:
200
200






share|improve this answer














share|improve this answer



share|improve this answer








edited 1 hour ago

























answered 9 hours ago









GalikGalik

34.3k34980




34.3k34980








  • 1





    Yes this is exactly what the aliasing constructor is for!

    – n.m.
    9 hours ago











  • Of course, while this shares ownership with the shared_ptr used during creation (and all its clones), the offset is not applied to any of the existing clones. So it's not clear to meet whether this meets the question's stated goal of "setting" the shared_ptr

    – Ben Voigt
    2 hours ago











  • @BenVoigt I have made a note to the effect that the offset belongs to each shared_ptr and that offsets are transferred to another shared_ptr when copied (either copy constructed or copy assigned).

    – Galik
    1 hour ago














  • 1





    Yes this is exactly what the aliasing constructor is for!

    – n.m.
    9 hours ago











  • Of course, while this shares ownership with the shared_ptr used during creation (and all its clones), the offset is not applied to any of the existing clones. So it's not clear to meet whether this meets the question's stated goal of "setting" the shared_ptr

    – Ben Voigt
    2 hours ago











  • @BenVoigt I have made a note to the effect that the offset belongs to each shared_ptr and that offsets are transferred to another shared_ptr when copied (either copy constructed or copy assigned).

    – Galik
    1 hour ago








1




1





Yes this is exactly what the aliasing constructor is for!

– n.m.
9 hours ago





Yes this is exactly what the aliasing constructor is for!

– n.m.
9 hours ago













Of course, while this shares ownership with the shared_ptr used during creation (and all its clones), the offset is not applied to any of the existing clones. So it's not clear to meet whether this meets the question's stated goal of "setting" the shared_ptr

– Ben Voigt
2 hours ago





Of course, while this shares ownership with the shared_ptr used during creation (and all its clones), the offset is not applied to any of the existing clones. So it's not clear to meet whether this meets the question's stated goal of "setting" the shared_ptr

– Ben Voigt
2 hours ago













@BenVoigt I have made a note to the effect that the offset belongs to each shared_ptr and that offsets are transferred to another shared_ptr when copied (either copy constructed or copy assigned).

– Galik
1 hour ago





@BenVoigt I have made a note to the effect that the offset belongs to each shared_ptr and that offsets are transferred to another shared_ptr when copied (either copy constructed or copy assigned).

– Galik
1 hour 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

How to label and detect the document text images

Vallis Paradisi

Tabula Rosettana