Jump to page: 1 2 3
Thread overview
pointers, assignments, Garbage Collection Oh My?
Jul 10, 2013
JohnnyK
Jul 10, 2013
Jacob Carlborg
Jul 10, 2013
Namespace
Jul 10, 2013
Sean Kelly
Jul 10, 2013
Ali Çehreli
Jul 10, 2013
JohnnyK
Jul 10, 2013
H. S. Teoh
Jul 10, 2013
JohnnyK
Jul 10, 2013
Jonathan M Davis
Jul 11, 2013
Maxim Fomin
Jul 11, 2013
Jacob Carlborg
Jul 11, 2013
Maxim Fomin
Jul 11, 2013
Jacob Carlborg
Jul 11, 2013
Jacob Carlborg
Jul 11, 2013
Jacob Carlborg
Jul 11, 2013
Ali Çehreli
Jul 11, 2013
Maxim Fomin
Jul 11, 2013
Ali Çehreli
Jul 10, 2013
H. S. Teoh
Jul 10, 2013
John Colvin
Jul 11, 2013
Jesse Phillips
July 10, 2013
I hope you like the subject matter and I hope it is not too simplistic or have been answered before.
  Anyway I have a question about how the garbage collector works in a very specific situation.  When passing string type to a function in a shared library or DLL and assigning it to a variable of type string inside the function and returning the internal string.  Such as this.

export string mytest(string tstStr)
{
  string st = tstStr;
  /* abbreviated to protect the innocent but other operations
     such as concatenating and deleting may be done to st before the return
  */
  return st;
}

Is the string type a pointer or is it something else?  In the line where tstStr is assigned to st does it copy the address in tstStr to st or does it copy the value in tstStr?  I am just a bit confused about string types since I come from a C background and C has no type like this.  Also what is returned by this function?  Does this function return a pointer or the contents of an array?  If I do export this what does it do to the Garbage Collection?  Does the Garbage Collection collect tstStr or st?  Also notice the comment in the function.

July 10, 2013
On 2013-07-10 19:18, JohnnyK wrote:
> I hope you like the subject matter and I hope it is not too simplistic
> or have been answered before.
>    Anyway I have a question about how the garbage collector works in a
> very specific situation.  When passing string type to a function in a
> shared library or DLL and assigning it to a variable of type string
> inside the function and returning the internal string.  Such as this.
>
> export string mytest(string tstStr)
> {
>    string st = tstStr;
>    /* abbreviated to protect the innocent but other operations
>       such as concatenating and deleting may be done to st before the
> return
>    */
>    return st;
> }
>
> Is the string type a pointer or is it something else?  In the line where
> tstStr is assigned to st does it copy the address in tstStr to st or
> does it copy the value in tstStr?  I am just a bit confused about string
> types since I come from a C background and C has no type like this.
> Also what is returned by this function?  Does this function return a
> pointer or the contents of an array?  If I do export this what does it
> do to the Garbage Collection?  Does the Garbage Collection collect
> tstStr or st? Also notice the comment in the function.

A string in D, and all arrays, is a struct looking like this:

struct Array (T)
{
    T* ptr;
    size_t length;
}

"string" happens to be an alias looking like this:

alias immutable(char)[] string;

That means you can do all operations on a string except assigning to specific elements:

string str = "foo";
str ~= "bar"; // ok
auto str2 = str ~ "foo"; // ok
str[0] == "f"; // ok
str[0] = 'a'; // error, cannot assign to immutable
auto str3 = str[1 .. 3]; // ok

It won't collect anything along as it is scope. If a variable goes out of scope and nothing else points to that data it will collect it (eventually).

Hope this helps a bit.

-- 
/Jacob Carlborg
July 10, 2013
> A string in D, and all arrays, is a struct looking like this:
>
> struct Array (T)
> {
>     T* ptr;
>     size_t length;
> }

I always thought it looks like this:

struct Array(T) {
    T* ptr;
    size_t length, capacity;
}
July 10, 2013
On Jul 10, 2013, at 10:45 AM, Namespace <rswhite4@googlemail.com> wrote:

>> A string in D, and all arrays, is a struct looking like this:
>> 
>> struct Array (T)
>> {
>>    T* ptr;
>>    size_t length;
>> }
> 
> I always thought it looks like this:
> 
> struct Array(T) {
>    T* ptr;
>    size_t length, capacity;
> }

Sadly, no.  The only way to determine the capacity of an array is to query the GC.
July 10, 2013
On 07/10/2013 11:10 AM, Sean Kelly wrote:

> On Jul 10, 2013, at 10:45 AM, Namespace <rswhite4@googlemail.com> wrote:
>
>>> A string in D, and all arrays, is a struct looking like this:
>>>
>>> struct Array (T)
>>> {
>>>     T* ptr;
>>>     size_t length;
>>> }
>>
>> I always thought it looks like this:
>>
>> struct Array(T) {
>>     T* ptr;
>>     size_t length, capacity;
>> }
>
> Sadly, no.  The only way to determine the capacity of an array is to query the GC.
>

And to be pedantic, length comes first:

struct Array (T)
{
    size_t length;
    T* ptr;
}

Which is actually property-like because assigning to length does pretty complex stuff. So the member cannot be named as 'length':

struct Array (T)
{
    size_t length_;
    T* ptr;
}

Anyway... :)

Ali

July 10, 2013
On Wednesday, 10 July 2013 at 18:22:24 UTC, Ali Çehreli wrote:
> On 07/10/2013 11:10 AM, Sean Kelly wrote:
>
> > On Jul 10, 2013, at 10:45 AM, Namespace
> <rswhite4@googlemail.com> wrote:
> >
> >>> A string in D, and all arrays, is a struct looking like
> this:
> >>>
> >>> struct Array (T)
> >>> {
> >>>     T* ptr;
> >>>     size_t length;
> >>> }
> >>
> >> I always thought it looks like this:
> >>
> >> struct Array(T) {
> >>     T* ptr;
> >>     size_t length, capacity;
> >> }
> >
> > Sadly, no.  The only way to determine the capacity of an
> array is to query the GC.
> >
>
> And to be pedantic, length comes first:
>
> struct Array (T)
> {
>     size_t length;
>     T* ptr;
> }
>
> Which is actually property-like because assigning to length does pretty complex stuff. So the member cannot be named as 'length':
>
> struct Array (T)
> {
>     size_t length_;
>     T* ptr;
> }
>
> Anyway... :)
>
> Ali

Reminds me of how Delphi (aka Pascal) strings are work.  Thanks everyone this answers some of my questions.  Now what about when the return type of a function is a string?  Is D returning the pointer to the string structure or is it returning the structure?
July 10, 2013
On Wed, Jul 10, 2013 at 07:45:25PM +0200, Namespace wrote:
> >A string in D, and all arrays, is a struct looking like this:
> >
> >struct Array (T)
> >{
> >    T* ptr;
> >    size_t length;
> >}
> 
> I always thought it looks like this:
> 
> struct Array(T) {
>     T* ptr;
>     size_t length, capacity;
> }

Nope, the capacity is an attribute of the GC memory that the array is pointing to, not the array "itself", which is merely a slice of this GC memory.

When you append to an array, basically what happens is the GC is asked "is this the end of the GC block and can we extend it please?" If it's not the end of the GC block, a new block is allocated; otherwise, it is extended, then the new data is written into it.


T

-- 
Those who've learned LaTeX swear by it. Those who are learning LaTeX swear at it. -- Pete Bleackley
July 10, 2013
On Wed, Jul 10, 2013 at 08:38:40PM +0200, JohnnyK wrote: [...]
> Reminds me of how Delphi (aka Pascal) strings are work.  Thanks everyone this answers some of my questions.  Now what about when the return type of a function is a string?  Is D returning the pointer to the string structure or is it returning the structure?

D structs are always passed by value, so it's the length+pointer pair that's being returned. There's no extra indirection involved here. What it points to stays where it is on the GC heap.

So for example:

	int[] data = [1,2,3];

	int[] f() {
		return data;
	}

	void main() {
		auto arr = f();	// returns copy of data's ptr+length
		arr.length--;	// modifies this copy
		assert(arr == [1,2]);
		assert(data == [1,2,3]); // data itself hasn't changed
	}

Does this help?


T

-- 
Be in denial for long enough, and one day you'll deny yourself of things you wish you hadn't.
July 10, 2013
On Wednesday, July 10, 2013 20:38:40 JohnnyK wrote:
> Reminds me of how Delphi (aka Pascal) strings are work. Thanks everyone this answers some of my questions. Now what about when the return type of a function is a string? Is D returning the pointer to the string structure or is it returning the structure?

Absolutely everywhere that you see an array (or string) in code, it's really the struct underneath the hood. In no case is just a pointer passed, since a pointer would not be an array. To get a pointer to the array's data, you have to use its ptr property.

- Jonathan M Davis
July 10, 2013
On Wednesday, 10 July 2013 at 18:45:56 UTC, H. S. Teoh wrote:
> On Wed, Jul 10, 2013 at 08:38:40PM +0200, JohnnyK wrote:
> [...]
>> Reminds me of how Delphi (aka Pascal) strings are work.  Thanks
>> everyone this answers some of my questions.  Now what about when the
>> return type of a function is a string?  Is D returning the pointer
>> to the string structure or is it returning the structure?
>
> D structs are always passed by value, so it's the length+pointer pair
> that's being returned. There's no extra indirection involved here. What
> it points to stays where it is on the GC heap.
>
> So for example:
>
> 	int[] data = [1,2,3];
>
> 	int[] f() {
> 		return data;
> 	}
>
> 	void main() {
> 		auto arr = f();	// returns copy of data's ptr+length
> 		arr.length--;	// modifies this copy
> 		assert(arr == [1,2]);
> 		assert(data == [1,2,3]); // data itself hasn't changed
> 	}
>
> Does this help?
>
>
I understand thanks for the information.
« First   ‹ Prev
1 2 3