Re: Can't understand if deallocation happens?

2017-01-22 Thread Nicholas Wilson via Digitalmars-d-learn
On Monday, 23 January 2017 at 06:42:00 UTC, Suliman wrote: You have *two* distinct strings here. Yes, I understand, I am trying to find out how it's work on low level. Any ideas why zero is used? string *literals* in d are nul terminated to ease interoperation with C so string s = "foo";

Re: Can't understand if deallocation happens?

2017-01-22 Thread Suliman via Digitalmars-d-learn
You have *two* distinct strings here. Yes, I understand, I am trying to find out how it's work on low level. Any ideas why zero is used?

Re: Profiling calls to small functions

2017-01-22 Thread albert-j via Digitalmars-d-learn
I'm not sure if it's what happening in this case but, in code as simple as this, function calls can sometimes be the bottleneck. You should see how compiling with/without -O affects performance, and adding `pragma(inline)` to funcB. I guess my question is whether it is possible to have

Re: Compile to C?

2017-01-22 Thread Joakim via Digitalmars-d-learn
On Monday, 23 January 2017 at 02:20:02 UTC, Nestor wrote: On Monday, 23 January 2017 at 01:17:20 UTC, Adam D. Ruppe wrote: On Monday, 23 January 2017 at 01:12:21 UTC, Nestor wrote: You mean phobos, or system libraries? Phobos but mostly the druntime that interfaces with the system. I see,

Re: Compile to C?

2017-01-22 Thread rikki cattermole via Digitalmars-d-learn
On 23/01/2017 3:20 PM, Nestor wrote: On Monday, 23 January 2017 at 01:17:20 UTC, Adam D. Ruppe wrote: On Monday, 23 January 2017 at 01:12:21 UTC, Nestor wrote: You mean phobos, or system libraries? Phobos but mostly the druntime that interfaces with the system. I see, I was mostly thinking

Re: Can't understand if deallocation happens?

2017-01-22 Thread Mike Parker via Digitalmars-d-learn
On Sunday, 22 January 2017 at 15:59:47 UTC, Suliman wrote: On Sunday, 22 January 2017 at 15:51:01 UTC, Suliman wrote: string str = "abc"; writeln(str.ptr); str = "def"; writeln("last data: ", *(str.ptr)); writeln("old data: ", *(str.ptr-1)); // print

Re: Compile to C?

2017-01-22 Thread Nestor via Digitalmars-d-learn
On Monday, 23 January 2017 at 01:17:20 UTC, Adam D. Ruppe wrote: On Monday, 23 January 2017 at 01:12:21 UTC, Nestor wrote: You mean phobos, or system libraries? Phobos but mostly the druntime that interfaces with the system. I see, I was mostly thinking in Android and/or other platforms,

Re: Compile to C?

2017-01-22 Thread Adam D. Ruppe via Digitalmars-d-learn
On Monday, 23 January 2017 at 01:12:21 UTC, Nestor wrote: You mean phobos, or system libraries? Phobos but mostly the druntime that interfaces with the system.

Re: Compile to C?

2017-01-22 Thread Nestor via Digitalmars-d-learn
On Saturday, 21 January 2017 at 19:33:27 UTC, Adam D. Ruppe wrote: On Saturday, 21 January 2017 at 18:38:22 UTC, Nestor wrote: That would be cool for greater portability. The hard part in porting to a new platform is rarely the code generation - gdc and ldc have diverse backends already

Output range and writeln style functions

2017-01-22 Thread Jon Degenhardt via Digitalmars-d-learn
I've been increasingly using output ranges in my code (the "component programming" model described in several articles on the D site). It works very well, except that it would often be more convenient to use writeln style functions rather than 'put'. Especially when you start by drafting a

Re: Can't understand if deallocation happens?

2017-01-22 Thread Suliman via Digitalmars-d-learn
On Sunday, 22 January 2017 at 15:51:01 UTC, Suliman wrote: string str = "abc"; writeln(str.ptr); str = "def"; writeln("last data: ", *(str.ptr)); writeln("old data: ", *(str.ptr-1)); // print nothing writeln("old data: ", *(str.ptr-2)); // print c

Re: Can't understand if deallocation happens?

2017-01-22 Thread Suliman via Digitalmars-d-learn
string str = "abc"; writeln(str.ptr); str = "def"; writeln("last data: ", *(str.ptr)); writeln("old data: ", *(str.ptr-1)); // print nothing writeln("old data: ", *(str.ptr-2)); // print c It's look like that there is some gap between data, because

Re: Can't understand if deallocation happens?

2017-01-22 Thread Suliman via Digitalmars-d-learn
Supported answer: you don't, it has infinite lifetime and you're claiming it is immutable, but then trying to pull the memory out from under it! The supported solution is simply to let the garbage collector manage it. But.. //GC.free(str_ptr.ptr); // Error: function core.memory.GC.free

Re: Can't understand if deallocation happens?

2017-01-22 Thread Adam D. Ruppe via Digitalmars-d-learn
On Sunday, 22 January 2017 at 14:04:55 UTC, Suliman wrote: So str.ptr is just shortcut? str.ptr is the actual member. In D, pointers to structs (and an array is virtually the same as a struct) will automatically dereference themselves. T* t; t.member; // automatically rewritten into

Re: Can't understand if deallocation happens?

2017-01-22 Thread Suliman via Digitalmars-d-learn
str_ptr.ptr returns exactly the same thing as str.ptr or (*str_ptr).ptr, a pointer to the contents. When you write str_ptr, you print the pointer to the container. So str.ptr is just shortcut? Ok, but how to free memory from first located value (from `aaa`)? I changed my code to next:

Re: Can't understand if deallocation happens?

2017-01-22 Thread Adam D. Ruppe via Digitalmars-d-learn
On Sunday, 22 January 2017 at 12:49:11 UTC, Suliman wrote: writeln(str_ptr); writeln("before dealloc: ", str_ptr.length); GC.free(str_ptr.ptr); writeln(str_ptr); You freed the CONTENTS, but are printing the CONTAINER. str_ptr.ptr returns exactly the same thing as

Re: Can't understand if deallocation happens?

2017-01-22 Thread Adam D. Ruppe via Digitalmars-d-learn
On Sunday, 22 January 2017 at 13:34:10 UTC, Suliman wrote: str[] = ""[]; That means copy the contents of the right hand array into the location of the left hand array. It copies data, that operation will never change pointers. str = ""; would change the pointer.

Re: Can't understand if deallocation happens?

2017-01-22 Thread Suliman via Digitalmars-d-learn
You do not append to anything, only overwrite it. There is no reallocation because "aaa".length == "bbb".length. I changed my code to: str_ptr.length +=1; str[] = ""[]; But now it's print length 4 before and after writing "bbb" to `str`. I expected that size will be 3+4=7.

Re: Can't understand if deallocation happens?

2017-01-22 Thread Nicholas Wilson via Digitalmars-d-learn
On Sunday, 22 January 2017 at 12:49:11 UTC, Suliman wrote: import std.stdio; import std.string; import core.memory; void main() { char [] str = "aaa".dup; char [] *str_ptr = writeln("before: ", str_ptr.ptr);// address of structure writeln(*str_ptr.ptr); // address of data

Re: Why does multidimensional arrays not allocate properly?

2017-01-22 Thread Nicholas Wilson via Digitalmars-d-learn
On Sunday, 22 January 2017 at 08:18:35 UTC, Jot wrote: On Sunday, 22 January 2017 at 08:07:26 UTC, rikki cattermole wrote: On 22/01/2017 9:05 PM, Jot wrote: auto x = new int[][](n,m); But one cannot freely assign anywhere in x: x[3,6] = 4 crashes. I, can, of course, convert everything to a

Re: Why does multidimensional arrays not allocate properly?

2017-01-22 Thread albert-j via Digitalmars-d-learn
In anycase, what is the correct notation for indexing? x = new int[][](width, height) and x[height][width] or x[width][height]? It's x[width][height], but because indexing is 0-based, largest valid indexes are x[width-1][height-1].

Re: Why does multidimensional arrays not allocate properly?

2017-01-22 Thread Jot via Digitalmars-d-learn
On Sunday, 22 January 2017 at 08:07:26 UTC, rikki cattermole wrote: On 22/01/2017 9:05 PM, Jot wrote: auto x = new int[][](n,m); But one cannot freely assign anywhere in x: x[3,6] = 4 crashes. I, can, of course, convert everything to a linear matrix and index by i+w*j, but what's the point

dub subpackage output to shared lib not working

2017-01-22 Thread Tofu Ninja via Digitalmars-d-learn
Trying to get a dub sub package to output as a shared lib but for some reason I can only get it to output as a static lib. dub.json --- { "name": "tofueng", "targetType": "executable", "targetPath" : "game", "sourcePaths": ["eng"], "importPaths": ["eng"],

Re: Why does multidimensional arrays not allocate properly?

2017-01-22 Thread rikki cattermole via Digitalmars-d-learn
On 22/01/2017 9:05 PM, Jot wrote: auto x = new int[][](n,m); But one cannot freely assign anywhere in x: x[3,6] = 4 crashes. I, can, of course, convert everything to a linear matrix and index by i+w*j, but what's the point of having multidimensional matrices in D if they don't allocate them

Why does multidimensional arrays not allocate properly?

2017-01-22 Thread Jot via Digitalmars-d-learn
auto x = new int[][](n,m); But one cannot freely assign anywhere in x: x[3,6] = 4 crashes. I, can, of course, convert everything to a linear matrix and index by i+w*j, but what's the point of having multidimensional matrices in D if they don't allocate them fully?