Re: Member access of __gshared global object
Indeed it's confusing. So AA is a value type that behaves like a pointer/reference. We can add a rule to the initialization to make AA behave more like reference types: If someone `refer` to an unitialized AA, that is, by doing: ```d string[string] aa; string[string] bb = aa; // bb `refers` to aa, by actually copying the AA struct. ``` They must want to use bb the same as aa, and it mostly will be followed by an assignment to bb (otherwise why bother refering it?). So we can initialize the AA BEFORE copying the struct, similar to the process before assigning an unitialized AA. Actually, I think ANY structs that mimics a reference behavior should add this rule to really look like a reference. On Thursday, 7 August 2014 at 02:17:19 UTC, H. S. Teoh via Digitalmars-d-learn wrote: On Thu, Aug 07, 2014 at 02:00:27AM +, Puming via Digitalmars-d-learn wrote: On Wednesday, 6 August 2014 at 15:42:05 UTC, Marc Schütz wrote: [...] Indeed, it was just what the OP suspected as the culprit. You are right, I didn't know about the AA initialization problem then. When I writln the AA and it outputs '[]', I thought it was initialized, which in that case was actually null. [...] This is a known gotcha with AA's and built-in arrays: they are null until you insert something into them, which means that while they are null, passing them into functions that add stuff to them won't update the original references because there is no common object that null points to. But once they become non-empty, passing them around to functions that change their contents will affect what's seen through the original references, since now they are pointing at a common object in memory. So they behave like value types when null, but acquire reference semantics once they are non-empty. This can be rather confusing for newbies. T
Re: Member access of __gshared global object
It's an optimization of memory allocation: you can always have a usable AA without allocating anything for it, so when you fill it with data, you may want to assign it back to where it should stay, similar to a slice.
Re: Member access of __gshared global object
Yes indeed, null initial value is reasonable. My suggestion does not affect that rationale, but is only based on my observation that if someone want to `refer` to an AA, he is more likely to fill it very soon, and he really mean to refer to it. These are similar concerns: - create a null AA, then fill it, and the compiler/runtime will automatically initialize it before the fill. - create a null AA, then refer to it (and would then use it), and the compiler/runtime wil automatically initialize it before the refer. On Thursday, 7 August 2014 at 11:05:33 UTC, Kagamin wrote: It's an optimization of memory allocation: you can always have a usable AA without allocating anything for it, so when you fill it with data, you may want to assign it back to where it should stay, similar to a slice.
Re: CSV Data to Binary File
On Thursday, 7 August 2014 at 15:11:48 UTC, TJB wrote: I am trying to read data in from a csv file into a struct, and then turn around and write that data to binary format. Here is my code: import std.algorithm; import std.csv; import stdio = std.stdio; import std.stream; align(1) struct QuotesBin { int qtim;9 int bid; int ofr; int bidsiz; int ofrsiz; short mode; char[1] ex; char[4] mmid; } void main() { string infile = temp.csv; string outfile = temp.bin; Stream fin = new BufferedFile(infile); Stream fout = new BufferedFile(outfile, FileMode.Out); foreach(ulong n, char[] line; fin) { auto record = csvReader!QuotesBin(line).front; fout.writeExact(record, QuotesBin.sizeof); } fin.close(); fout.close(); } Here is a snippet of my csv data: 34220, 37, 371200, 1, 1, 12, N, 34220, 369000, 372500, 1, 11, 12, P, 34220, 37, 371200, 1, 2, 12, N, 34220, 37, 371100, 1, 33, 12, N, 34220, 369400, 371100, 6, 3, 12, P, 34220, 37, 371200, 1, 2, 12, N, 34220, 369300, 371200, 9, 2, 12, N, 34220, 369300, 371200, 5, 2, 12, N, 34220, 368900, 371200, 13, 2, 12, N, 34220, 368900, 371100, 13, 1, 12, N, For some reason this fails miserably. Can anyone help me out as to why? What do I need to do differently? Thanks, TJB Some of the code got messed up when I pasted. Should be: align(1) struct QuotesBin { int qtim; int bid; int ofr; int bidsiz; int ofrsiz; short mode; char[1] ex; char[4] mmid; } Thanks!
CSV Data to Binary File
I am trying to read data in from a csv file into a struct, and then turn around and write that data to binary format. Here is my code: import std.algorithm; import std.csv; import stdio = std.stdio; import std.stream; align(1) struct QuotesBin { int qtim;9 int bid; int ofr; int bidsiz; int ofrsiz; short mode; char[1] ex; char[4] mmid; } void main() { string infile = temp.csv; string outfile = temp.bin; Stream fin = new BufferedFile(infile); Stream fout = new BufferedFile(outfile, FileMode.Out); foreach(ulong n, char[] line; fin) { auto record = csvReader!QuotesBin(line).front; fout.writeExact(record, QuotesBin.sizeof); } fin.close(); fout.close(); } Here is a snippet of my csv data: 34220, 37, 371200, 1, 1, 12, N, 34220, 369000, 372500, 1, 11, 12, P, 34220, 37, 371200, 1, 2, 12, N, 34220, 37, 371100, 1, 33, 12, N, 34220, 369400, 371100, 6, 3, 12, P, 34220, 37, 371200, 1, 2, 12, N, 34220, 369300, 371200, 9, 2, 12, N, 34220, 369300, 371200, 5, 2, 12, N, 34220, 368900, 371200, 13, 2, 12, N, 34220, 368900, 371100, 13, 1, 12, N, For some reason this fails miserably. Can anyone help me out as to why? What do I need to do differently? Thanks, TJB
Re: CSV Data to Binary File
On Thursday, 7 August 2014 at 15:14:00 UTC, TJB wrote: align(1) struct QuotesBin { int qtim; int bid; int ofr; int bidsiz; int ofrsiz; short mode; char[1] ex; char[4] mmid; } Thanks! (You forgot to include the error. For other readers: It fails to compile with template std.conv.toImpl cannot deduce function from argument types !(char[4])(string) and similar error messages.) This is caused by the two `char` arrays. `std.conv.to` cannot convert strings to fixed-size char arrays, probably because it's not clear what should happen if the input string is too long or too short. Would it be a good idea to support this? As a workaround, you could declare a second struct with the same members, but `ex` and `mmid` as strings, read your data into these, and assign it to the first structure: import std.algorithm; import std.csv; import stdio = std.stdio; import std.stream; align(1) struct QuotesBinDummy { int qtim; int bid; int ofr; int bidsiz; int ofrsiz; short mode; string ex; string mmid; } align(1) struct QuotesBin { int qtim; int bid; int ofr; int bidsiz; int ofrsiz; short mode; char[1] ex; char[4] mmid; } void main() { string infile = temp.csv; string outfile = temp.bin; Stream fin = new BufferedFile(infile); Stream fout = new BufferedFile(outfile, FileMode.Out); foreach(ulong n, char[] line; fin) { auto temp = csvReader!QuotesBinDummy(line).front; QuotesBin record; record.tupleof = temp.tupleof; fout.writeExact(record, QuotesBin.sizeof); } fin.close(); fout.close(); } The line record.tupleof = temp.tupleof; will however fail with your example data, because the `ex` field includes a space in the CSV, and the last field is empty, but needs to be 4 chars long.
Re: Very Stupid Regex question
On Thu, 07 Aug 2014 16:05:16 +, seany wrote: obviously there are ways like counting the match length, and then using the maximum length, instead of breaking as soon as a match is found. Are there any other better ways? You're not really using regexes properly. You want to greedily match as much as possible in this case, e.g.: void main() { import std.regex; auto re = regex(ab(cd)?); assert(PREabcdPOST.matchFirst(re).hit == abcd); assert(PREabPOST.matchFirst(re).hit == ab); }
Re: Very Stupid Regex question
On Thursday, 7 August 2014 at 16:05:17 UTC, seany wrote: Cosider please the following: string s1 = PREabcdPOST; string s2 = PREabPOST; string[] srar = [ab, abcd]; // this can not be constructed with a particular order foreach(sr; srar) { auto r = regex(sr; g); auto m = matchFirst(s1, r); break; // this one matches ab // but I want this to match abcd // and for s2 I want to match ab } obviously there are ways like counting the match length, and then using the maximum length, instead of breaking as soon as a match is found. Are there any other better ways? It's not clear to me what exactly you want, but: Are the regexes in `srar` related? That is, does one regex always include the previous one as a prefix? Then you can use optional matches: /ab(cd)?/ This will match abcd if it is there, but will also match ab otherwise.
Re: Very Stupid Regex question
On Thursday, 7 August 2014 at 16:12:59 UTC, Justin Whear wrote: On Thu, 07 Aug 2014 16:05:16 +, seany wrote: obviously there are ways like counting the match length, and then using the maximum length, instead of breaking as soon as a match is found. Are there any other better ways? You're not really using regexes properly. You want to greedily match as much as possible in this case, e.g.: void main() { import std.regex; auto re = regex(ab(cd)?); assert(PREabcdPOST.matchFirst(re).hit == abcd); assert(PREabPOST.matchFirst(re).hit == ab); } thing is, abcd is read from a file, and in the compile time, i dont know if cd may at all be there or not, ir if it should be ab(ef)
Re: CSV Data to Binary File
On Thursday, 7 August 2014 at 16:08:01 UTC, TJB wrote: Thanks Marc. Not sure what to do here. I need to the binary data to be exactly the number of bytes as specified by the struct. How to handle the conversion from string to char[]? Well, in your CSV data, they don't have the right length, so you have to decide how to handle that. The easiest way would be to set the length. This will fill up the string with \0 bytes if it is to short: align(1) struct QuotesBin { int qtim; int bid; int ofr; int bidsiz; int ofrsiz; short mode; char[1] ex; char[4] mmid; this(const QuotesBinDummy rhs) { this.qtim = rhs.qtim; this.bid = rhs.bid; this.ofr = rhs.ofr; this.bidsiz = rhs.bidsiz; this.ofrsiz = rhs.ofrsiz; this.mode = rhs.mode; string tmp; tmp = rhs.ex; tmp.length = this.ex.length; this.ex = tmp; tmp = rhs.mmid; tmp.length = this.mmid.length; this.mmid = tmp; } } ... auto temp = csvReader!QuotesBinDummy(line).front; QuotesBin record = temp; fout.writeExact(record, QuotesBin.sizeof); ... But of course, whether this is correct depends on whether your binary format allows it, or requires all chars to be non-zero ASCII values.
Re: CSV Data to Binary File
On Thursday, 7 August 2014 at 16:08:01 UTC, TJB wrote: Thanks Marc. Not sure what to do here. I need to the binary data to be exactly the number of bytes as specified by the struct. Something else: The `align(1)` on your type definition specifies the alignment of the entire struct, but has no effect on the alignment of its fields relative to the beginning. Your probably want this: align(1) struct QuotesBin { align(1): int qtim; int bid; int ofr; int bidsiz; int ofrsiz; short mode; char[1] ex; char[4] mmid; } This align the struct as a whole, and all its fields at byte boundaries. Without the second `align(1)`, there should be a gap between `mode` and `ex`. Strangely enough, when I test it, there's none. Will have to ask...
Re: Very Stupid Regex question
On Thu, Aug 07, 2014 at 04:49:05PM +, seany via Digitalmars-d-learn wrote: On Thursday, 7 August 2014 at 16:12:59 UTC, Justin Whear wrote: On Thu, 07 Aug 2014 16:05:16 +, seany wrote: obviously there are ways like counting the match length, and then using the maximum length, instead of breaking as soon as a match is found. Are there any other better ways? You're not really using regexes properly. You want to greedily match as much as possible in this case, e.g.: void main() { import std.regex; auto re = regex(ab(cd)?); assert(PREabcdPOST.matchFirst(re).hit == abcd); assert(PREabPOST.matchFirst(re).hit == ab); } thing is, abcd is read from a file, and in the compile time, i dont know if cd may at all be there or not, ir if it should be ab(ef) So basically you have a file containing regex patterns, and you want to find the longest match among them? One way to do this is to combine them at runtime: string[] patterns = ... /* read from file, etc. */; // Longer patterns match first patterns.sort!((a,b) = a.length b.length); // Build regex string regexStr = %((%(%c%))%||%).format(patterns); auto re = regex(regexStr); ... // Run matches against input char[] input = ...; auto m = input.match(re); auto matchedString = m.captures[0]; T -- When solving a problem, take care that you do not become part of the problem.
Struct alignment vs alignment of fields
(Original discussion: http://forum.dlang.org/thread/fckwpddiwxonabqaf...@forum.dlang.org#post-pskjgieddhpntzaokohj:40forum.dlang.org) align(1) struct A { align(1): int qtim; int bid; int ofr; int bidsiz; int ofrsiz; short mode; char[1] ex; char[4] mmid; } align(1) struct B { int qtim; int bid; int ofr; int bidsiz; int ofrsiz; short mode; char[1] ex; char[4] mmid; } I would expect `B` to have a gap between `ex` and `mmid`. AFAIK the outer `align(1)` only applies to the struct in its entirety, not to the individual fields. However for both DMD git and LDC 0.14.0-alpha1 (based on DMD 2.065), `A` and `B` have the same size. After some thinking, I believe this is because arrays inherit the alignment of their element types. Is this correct? If yes, where is this documented? I had expected `char[4]` to be aligned at a 4-byte boundary.
Re: CSV Data to Binary File
On Thursday, 7 August 2014 at 17:12:35 UTC, Marc Schütz wrote: This align the struct as a whole, and all its fields at byte boundaries. Without the second `align(1)`, there should be a gap between `mode` and `ex`. Strangely enough, when I test it, there's none. Will have to ask... Sorry, should have been `ex` and `mmid`. I've posted my question here: http://forum.dlang.org/post/bkearrybmwguqrlie...@forum.dlang.org
Re: Very Stupid Regex question
On Thu, 07 Aug 2014 10:22:37 -0700, H. S. Teoh via Digitalmars-d-learn wrote: So basically you have a file containing regex patterns, and you want to find the longest match among them? // Longer patterns match first patterns.sort!((a,b) = a.length b.length); // Build regex string regexStr = %((%(%c%))%||%).format (patterns); auto re = regex(regexStr); This only works if the patterns are simple literals. E.g. the pattern 'a +' might match a longer sequence than 'aaa'. If you're out for the longest possible match, iteratively testing each pattern is probably the way to go.
Re: Very Stupid Regex question
On Thu, Aug 07, 2014 at 05:33:42PM +, Justin Whear via Digitalmars-d-learn wrote: On Thu, 07 Aug 2014 10:22:37 -0700, H. S. Teoh via Digitalmars-d-learn wrote: So basically you have a file containing regex patterns, and you want to find the longest match among them? // Longer patterns match first patterns.sort!((a,b) = a.length b.length); // Build regex string regexStr = %((%(%c%))%||%).format (patterns); auto re = regex(regexStr); This only works if the patterns are simple literals. E.g. the pattern 'a +' might match a longer sequence than 'aaa'. If you're out for the longest possible match, iteratively testing each pattern is probably the way to go. Hmm, you're right. I was a bit disappointed to find out that the | operator in std.regex (and also in Perl's regex) doesn't do longest-match but first-match. :-( I had always thought it did longest-match, like in lex/flex. I wish we can extend std.regex to allow longest-match for alternations... but there may be performance consequences. T -- There's light at the end of the tunnel. It's the oncoming train.
getting autocompletion in emacs
I want to learn SDL2 and learn D at the same time, for the SDL2 part autocompletion would be very nice. I've found DCD but can't get it working (not finding symbols or declarations) at the moment but I was wondering if there are any alternatives, it's hard to google for d things and emacs, cause of emacs.d :)
Re: Very Stupid Regex question
On Thu, Aug 07, 2014 at 10:42:13AM -0700, H. S. Teoh via Digitalmars-d-learn wrote: [...] Hmm, you're right. I was a bit disappointed to find out that the | operator in std.regex (and also in Perl's regex) doesn't do longest-match but first-match. :-( I had always thought it did longest-match, like in lex/flex. I wish we can extend std.regex to allow longest-match for alternations... but there may be performance consequences. https://issues.dlang.org/show_bug.cgi?id=13268 T -- Valentine's Day: an occasion for florists to reach into the wallets of nominal lovers in dire need of being reminded to profess their hypothetical love for their long-forgotten.
Passing Command Line Arguments to a new Thread
What is the best way to forward a string[] as argument to a function called through std.concurrency.spawn(). I need this in the following example where I start the vibe.d event loop in the main thread (the only way I've managed to get runEventLoop() to work) and run my other program logic in another which requires command line arguments to passed to the new thread. void otherMain(string[] args) { // use args } void main(string[] args) { import std.concurrency: spawn; auto otherMainTid = spawn(otherMain, args); // this line fails runEventLoop(); } The line calling spawn() fails as /home/per/opt/x86_64-unknown-linux-gnu/dmd/linux/bin64/src/phobos/std/concurrency.d(442): Error: static assert Aliases to mutable thread-local data not allowed.
Re: Passing Command Line Arguments to a new Thread
On Thu, Aug 07, 2014 at 06:23:24PM +, Nordlöw via Digitalmars-d-learn wrote: What is the best way to forward a string[] as argument to a function called through std.concurrency.spawn(). I need this in the following example where I start the vibe.d event loop in the main thread (the only way I've managed to get runEventLoop() to work) and run my other program logic in another which requires command line arguments to passed to the new thread. void otherMain(string[] args) { // use args } void main(string[] args) { import std.concurrency: spawn; auto otherMainTid = spawn(otherMain, args); // this line fails runEventLoop(); } The line calling spawn() fails as /home/per/opt/x86_64-unknown-linux-gnu/dmd/linux/bin64/src/phobos/std/concurrency.d(442): Error: static assert Aliases to mutable thread-local data not allowed. Maybe try args.idup instead? T -- 2+2=4. 2*2=4. 2^2=4. Therefore, +, *, and ^ are the same operation.
Re: Passing Command Line Arguments to a new Thread
On Thursday, 7 August 2014 at 18:33:40 UTC, H. S. Teoh via Digitalmars-d-learn wrote: On Thu, Aug 07, 2014 at 06:23:24PM +, Nordlöw via Digitalmars-d-learn wrote: What is the best way to forward a string[] as argument to a function called through std.concurrency.spawn(). I need this in the following example where I start the vibe.d event loop in the main thread (the only way I've managed to get runEventLoop() to work) and run my other program logic in another which requires command line arguments to passed to the new thread. void otherMain(string[] args) { // use args } void main(string[] args) { import std.concurrency: spawn; auto otherMainTid = spawn(otherMain, args); // this line fails runEventLoop(); } The line calling spawn() fails as /home/per/opt/x86_64-unknown-linux-gnu/dmd/linux/bin64/src/phobos/std/concurrency.d(442): Error: static assert Aliases to mutable thread-local data not allowed. Maybe try args.idup instead? But this shouldn't be necessary, right? It's a mutable slice to immutable data, but the slice is passed by value, so no mutable sharing takes place.
Re: Passing Command Line Arguments to a new Thread
On Thursday, 7 August 2014 at 18:38:37 UTC, Marc Schütz wrote: But this shouldn't be necessary, right? It's a mutable slice to immutable data, but the slice is passed by value, so no mutable sharing takes place. I agree. I'll use .idup anyhow. For this work I however have to do void otherMain(immutable string[] args) { useArgs(args.dup); } as my function useArgs has signature useArgs(string[] args) Seems awkward.
Re: Passing Command Line Arguments to a new Thread
On Thursday, 7 August 2014 at 18:38:37 UTC, Marc Schütz wrote: But this shouldn't be necessary, right? It's a mutable slice to immutable data, but the slice is passed by value, so no mutable sharing takes place. The elements of the slice itself are mutable, you can e.g. assign some other string to args[1], which would be a potentially racy change among all threads which share that slice. Only the information start address of slice and length of slice are copied by value, which doesn't protect from this. To be able to share the slice, it would need to be typed as immutable(string)[] instead.
Re: Passing Command Line Arguments to a new Thread
On 2014-08-07 20:23, Nordlöw wrote: What is the best way to forward a string[] as argument to a function called through std.concurrency.spawn(). What about just accessing core.runtime.Runtime.args from the new thread? -- /Jacob Carlborg
Re: Passing Command Line Arguments to a new Thread
On Thursday, 7 August 2014 at 18:23:26 UTC, Nordlöw wrote: What is the best way to forward a string[] as argument to a function called through std.concurrency.spawn(). I need this in the following example where I start the vibe.d event loop in the main thread (the only way I've managed to get runEventLoop() to work) and run my other program logic in another which requires command line arguments to passed to the new thread. void otherMain(string[] args) { // use args } void main(string[] args) { import std.concurrency: spawn; auto otherMainTid = spawn(otherMain, args); // this line fails runEventLoop(); } The line calling spawn() fails as /home/per/opt/x86_64-unknown-linux-gnu/dmd/linux/bin64/src/phobos/std/concurrency.d(442): Error: static assert Aliases to mutable thread-local data not allowed. If you don't care how you get the args to otherMain, you can also use Runtime.args. That way you wouldn't even need to pass it to the function in the first place. http://dlang.org/phobos/core_runtime.html#.Runtime.args
Re: Passing Command Line Arguments to a new Thread
On Thursday, 7 August 2014 at 19:08:37 UTC, Johannes Blume wrote: On Thursday, 7 August 2014 at 18:38:37 UTC, Marc Schütz wrote: But this shouldn't be necessary, right? It's a mutable slice to immutable data, but the slice is passed by value, so no mutable sharing takes place. The elements of the slice itself are mutable, you can e.g. assign some other string to args[1], which would be a potentially racy change among all threads which share that slice. Only the information start address of slice and length of slice are copied by value, which doesn't protect from this. To be able to share the slice, it would need to be typed as immutable(string)[] instead. Ah, indeed. It's mutable ref to mutable ref to immutable data.
Re: Very Stupid Regex question
On Thursday, 7 August 2014 at 18:16:11 UTC, H. S. Teoh via Digitalmars-d-learn wrote: https://issues.dlang.org/show_bug.cgi?id=13268 T Thank you soo much!!
Re: multidimensional indexing/slicing docs?
On Wed, Aug 06, 2014 at 12:16:57PM -0700, H. S. Teoh via Digitalmars-d-learn wrote: On Wed, Aug 06, 2014 at 11:21:51AM -0700, H. S. Teoh via Digitalmars-d-learn wrote: [...] In any case, what Kenji did was basically to implement support for: arr[i,j,k,...]; // opIndex arr[i,j,k,...] = ...; // opIndexAssign arr[i1 .. i2, j1 .. j2, ...]; // opSlice arr[i..$, j..$, k..$]; // opSlice / opDollar and perhaps one or two others. OK, found the pull that implemented this, which also has a description of what was implemented: https://github.com/D-Programming-Language/dmd/pull/443 I'll see if I can cook up a PR to incorporate this into the language docs on dlang.org. [...] https://github.com/D-Programming-Language/dlang.org/pull/625 T -- What is Matter, what is Mind? Never Mind, it doesn't Matter.
Re: A little of coordination for Rosettacode
On Tuesday, 12 February 2013 at 01:07:35 UTC, bearophile wrote: In practice at the moment I am maintaining all the D entries of Rosettacode. Here's a candidate for http://rosettacode.org/wiki/Extensible_prime_generator#D in case it is preferred to the existing entry: http://dpaste.dzfl.pl/43735da3f1d1
Re: CSV Data to Binary File
On Thursday, 7 August 2014 at 15:11:48 UTC, TJB wrote: Here is a snippet of my csv data: 34220, 37, 371200, 1, 1, 12, N, 34220, 369000, 372500, 1, 11, 12, P, 34220, 37, 371200, 1, 2, 12, N, I can't help but think somehow that as long as the data is numbers or words, that scanf would be useful even if it's a C function... Someone mentioned the final empty field, this makes me scratch my head... And a struct of exactly 27 bytes... i'd probably pad that to 28 or 32 if possible which allows you to expand your definition later... not to mention being 32bit aligned (if speed becomes important)
Re: Struct alignment vs alignment of fields
On Thursday, 7 August 2014 at 17:22:15 UTC, Marc Schütz wrote: (Original discussion: http://forum.dlang.org/thread/fckwpddiwxonabqaf...@forum.dlang.org#post-pskjgieddhpntzaokohj:40forum.dlang.org) I would expect `B` to have a gap between `ex` and `mmid`. AFAIK the outer `align(1)` only applies to the struct in its entirety, not to the individual fields. However for both DMD git and LDC 0.14.0-alpha1 (based on DMD 2.065), `A` and `B` have the same size. After some thinking, I believe this is because arrays inherit the alignment of their element types. Is this correct? If yes, where is this documented? I had expected `char[4]` to be aligned at a 4-byte boundary. I'm not sure about all the latest compiler changes, but let's try and answer some of this. TDPL pg. 268-269 explains this (although could be out of date with recent changes). I'll copy what's relevant.. 7.1.11.1 The align Attribute If you want to override the compiler's choice of alignment, which influences the padding inserted. You could use an align modifier... etc.. class A { char a; align(1) int b; char c; } With the specification above the fields of A are laid out without gaps between them. You may use align with an entire class definition: align(1) struct S { char a; int b; char c } ... Align is not suppose to be used with pointers and references... Back to the question. Most fields will be aligned on 4-byte boundaries, or 8-byte depending on if it's 32/64 bit. This is mostly for performance reasons, but also with addresses it may affect the GC. Overriding the compiler is mostly going to be more useful when working against C/C++ structures where they are also forcibly aligned for space. So assuming we have the above struct. S[2] s; It's probably going to be aligned on 4's for the first one; But it doesn't have to be. But the others? I'm not so sure... The inner alignment and padding is 4 per, so if the struct S has a size of 12, then it's still going to be aligned by 4's by default... i think? Maybe the alignment has to do if it's inserted into another object. On the stack it probably ignores the alignment attribute... struct B { char a; S s; //infers align(1) by it's definition? } Well regardless, unless you're overriding both, you're probably going to get some form of alignments of 4, be it for arrays or for speed... I hope this isn't confusing.
Re: Struct alignment vs alignment of fields
Still watching this, but the Dconf 2014 bare metal presentation gets into it a bit... http://youtu.be/qErXPomAWYI?t=37m20s