On 5/7/2014 12:03 PM, Maxime Chevalier-Boisvert wrote:
auto ptr = cast(wchar*)alloca(wchar.sizeof * len);
if (ptr == null) throw new Error("...");
auto mySlice = ptr[0 .. len];
Is the slice going to be allocated on the stack? (I imagine the answer is yes)
Yes (slices do not copy).
On 5/7/2014 11:26 AM, Maxime Chevalier-Boisvert wrote:
I have a very specific use case (JIT compiler) in which I have a pre-allocated
array of wchar string data stored somewhere in memory. I'd like to be able to
create a temporary D wstring object to pass this as a "regular" string to other
funct
07.05.2014 22:26, Maxime Chevalier-Boisvert пишет:
I have a very specific use case (JIT compiler) in which I have a
pre-allocated array of wchar string data stored somewhere in memory. I'd
like to be able to create a temporary D wstring object to pass this as a
"regular" string to other functions
On Wed, 07 May 2014 19:41:16 +0100, Maxime Chevalier-Boisvert
wrote:
Unless I'm misunderstanding it should be as simple as:
wchar[100] stackws; // alloca() if you need it to be dynamically sized.
A slice of this static array behaves just like a slice of a dynamic
array.
I do need it to
John Colvin:
Sure, you have to keep track of the memory to make sure it
doesn't survive past the current scope (or at least isn't used
past that point, depending on how security critical your
application is), but that's often trivial to do and saving the
extra allocation can make a big differ
On Wednesday, 7 May 2014 at 18:34:10 UTC, Meta wrote:
On Wednesday, 7 May 2014 at 18:29:23 UTC, Brad Anderson wrote:
Unless I'm misunderstanding it should be as simple as:
wchar[100] stackws; // alloca() if you need it to be
dynamically sized.
A slice of this static array behaves just like a
On Wednesday, 7 May 2014 at 18:26:08 UTC, Maxime
Chevalier-Boisvert wrote:
I have a very specific use case (JIT compiler) in which I have
a pre-allocated array of wchar string data stored somewhere in
memory. I'd like to be able to create a temporary D wstring
object to pass this as a "regular"
On Wednesday, 7 May 2014 at 18:26:08 UTC, Maxime
Chevalier-Boisvert wrote:
I have a very specific use case (JIT compiler) in which I have
a pre-allocated array of wchar string data stored somewhere in
memory. I'd like to be able to create a temporary D wstring
object to pass this as a "regular"
On Wednesday, 7 May 2014 at 19:18:09 UTC, Maxime
Chevalier-Boisvert wrote:
Is the slice going to be allocated on the stack? (I imagine
the answer is yes)
Slicing doesn't change where the data is allocated. Slicing
means just creating a new struct that contains a length and
pointer to the data
Am 07.05.2014 20:41, schrieb Maxime Chevalier-Boisvert:
I do need it to be dynamically sized. I also want to avoid copying my
string data if possible. Basically, I just want to create a wstring
"view" on an existing "raw" buffer that exists in memory somewhere,
based on a pointer to this buffer
Is the slice going to be allocated on the stack? (I imagine
the answer is yes)
Slicing doesn't change where the data is allocated. Slicing
means just creating a new struct that contains a length and
pointer to the data (and the struct itself is allocated
in-place. So it's allocated on the sta
On Wednesday, 7 May 2014 at 18:41:17 UTC, Maxime
Chevalier-Boisvert wrote:
Basically, I just want to create a wstring "view" on an
existing "raw" buffer that exists in memory somewhere, based on
a pointer to this buffer and its length.
Looks like you actually don't need to allocate anything at
Maxime Chevalier-Boisvert:
Is the slice going to be allocated on the stack? (I imagine the
answer is yes)
Slicing doesn't change where the data is allocated. Slicing means
just creating a new struct that contains a length and pointer to
the data (and the struct itself is allocated in-place.
This is named slicing. You can also slice a
global/static/__gshared buffer.
alloca returns a void*, then you can cast it to the pointer
type you want, and then you slice the pointer:
auto ptr = cast(wchar*)alloca(wchar.sizeof * len);
if (ptr == null) throw new Error("...");
auto mySlice = ptr
Maxime Chevalier-Boisvert:
I do need it to be dynamically sized.
But often you can determine statically a maximum length of the
string, so you can use a fixed size stack buffer and slice it
with a dynamic length. If this is not acceptable, then use alloca.
Basically, I just want to create
Unless I'm misunderstanding it should be as simple as:
wchar[100] stackws; // alloca() if you need it to be
dynamically sized.
A slice of this static array behaves just like a slice of a
dynamic array.
I do need it to be dynamically sized. I also want to avoid
copying my string data if pos
Meta:
But you should avoid slicing the static array unless it's via
arr.dup.
Slicing fixed size arrays is often necessary, because many Phobos
functions don't accept fixed size arrays, they force you to lose
the compile-time knowledge of the length.
Bye,
bearophile
On Wednesday, 7 May 2014 at 18:29:23 UTC, Brad Anderson wrote:
Unless I'm misunderstanding it should be as simple as:
wchar[100] stackws; // alloca() if you need it to be
dynamically sized.
A slice of this static array behaves just like a slice of a
dynamic array.
But you should avoid slic
On Wednesday, 7 May 2014 at 18:26:08 UTC, Maxime
Chevalier-Boisvert wrote:
I have a very specific use case (JIT compiler) in which I have
a pre-allocated array of wchar string data stored somewhere in
memory. I'd like to be able to create a temporary D wstring
object to pass this as a "regular"
I have a very specific use case (JIT compiler) in which I have a
pre-allocated array of wchar string data stored somewhere in
memory. I'd like to be able to create a temporary D wstring
object to pass this as a "regular" string to other functions. For
performance reasons, it would be preferable
20 matches
Mail list logo