www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - memory/array question

reply "Eric" <eric makechip.com> writes:
Suppose I have some memory allocated on the heap, and I have
two pointers pointing to the beginning and end of a contiguous 
segment
of that memory.  Is there a way I can convert those two pointers
to an array slice without actually copying anything within the 
segment?

Thx,
Eric
Jul 31 2014
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Eric:

 Suppose I have some memory allocated on the heap, and I have
 two pointers pointing to the beginning and end of a contiguous 
 segment
 of that memory.  Is there a way I can convert those two pointers
 to an array slice without actually copying anything within the 
 segment?
Use something like this (but make sure the length is correct): auto mySlice = ptr1[0 .. ptr2 - ptr1]; Bye, bearophile
Jul 31 2014
parent reply "Eric" <eric makechip.com> writes:
On Thursday, 31 July 2014 at 19:43:00 UTC, bearophile wrote:
 Eric:

 Suppose I have some memory allocated on the heap, and I have
 two pointers pointing to the beginning and end of a contiguous 
 segment
 of that memory.  Is there a way I can convert those two 
 pointers
 to an array slice without actually copying anything within the 
 segment?
Use something like this (but make sure the length is correct): auto mySlice = ptr1[0 .. ptr2 - ptr1]; Bye, bearophile
Thanks. That really works. I timed doing auto mySlice = ptr1[0 .. ptr2 - ptr1]; 1,000,000 times versus auto mySlice = ptr1[0 .. ptr2 - ptr1].dup; 1,000,000 times and I am quite convinced the data is not being copied.
Jul 31 2014
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Eric:

 Thanks. That really works.  I timed doing

 auto mySlice = ptr1[0 .. ptr2 - ptr1]; 1,000,000 times

 versus

 auto mySlice = ptr1[0 .. ptr2 - ptr1].dup; 1,000,000 times

 and I am quite convinced the data is not being copied.
Take a look at the asm! Bye, bearophile
Jul 31 2014
parent reply "Vlad Levenfeld" <vlevenfeld gmail.com> writes:
On Thursday, 31 July 2014 at 20:43:11 UTC, bearophile wrote:
 Take a look at the asm!

 Bye,
 bearophile
I use DMD and Dub, how do I view the asm?
Jul 31 2014
parent reply "Eric" <eric makechip.com> writes:
On Thursday, 31 July 2014 at 20:59:46 UTC, Vlad Levenfeld wrote:
 On Thursday, 31 July 2014 at 20:43:11 UTC, bearophile wrote:
 Take a look at the asm!

 Bye,
 bearophile
I use DMD and Dub, how do I view the asm?
Actually I did't think to look at the asm, mainly because I've never bothered to do it before. But I was just reading Adam's book the other day, and I remember seeing this: objdump -d -M intel simpleOctal Not sure what the switches are for; the name of the program is simpleOctal. But the name of the utility is objdump. (on Linux). Not sure about Windoze. -Eric
Jul 31 2014
parent "anonymous" <anonymous example.com> writes:
On Thursday, 31 July 2014 at 21:50:25 UTC, Eric wrote:
 objdump -d  -M intel simpleOctal

 Not sure what the switches are for;
-d disassemble - Essential if you want to, well, disassemble. -M intel Intel syntax - Because no one likes AT&T syntax. Wikipedia has a comparison: http://en.wikipedia.org/wiki/X86_assembly_language#Syntax
Jul 31 2014