www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - D: Declaring empty pointer variables that return address inside

reply BoQsc <vaidas.boqsc gmail.com> writes:
Is it possible to declare empty pointer variable inside function 
calls and pass its address to the function?

These are sometimes required while using Win32 - Windows 
Operating System API.

* Empty pointer variables are used by functions to return 
information after the function is done.

My own horrible **suggestion** of empty pointer declaration 
inside function call:
`someFunction(uint & passingEmptyVariableForWrite);`

What it would do:
* A new variable is declared inside function call.
* Address of that variable is passed to the function.
* After function is done, you can refer to it for returned value.
Nov 23 2023
parent reply "H. S. Teoh" <hsteoh qfbox.info> writes:
On Thu, Nov 23, 2023 at 07:22:22PM +0000, BoQsc via Digitalmars-d-learn wrote:
 Is it possible to declare empty pointer variable inside function calls
 and pass its address to the function?
 
 These are sometimes required while using Win32 - Windows Operating
 System API.
 
 * Empty pointer variables are used by functions to return information
 after the function is done.
 
 My own horrible **suggestion** of empty pointer declaration inside
 function call:
 `someFunction(uint & passingEmptyVariableForWrite);`
 
 What it would do:
 * A new variable is declared inside function call.
 * Address of that variable is passed to the function.
 * After function is done, you can refer to it for returned value.
What's wrong with: uint* result; someFunction(&result); // use *result ? T -- One Word to write them all, One Access to find them, One Excel to count them all, And thus to Windows bind them. -- Mike Champion
Nov 23 2023
parent reply BoQsc <vaidas.boqsc gmail.com> writes:
On Thursday, 23 November 2023 at 20:00:31 UTC, H. S. Teoh wrote:
 On Thu, Nov 23, 2023 at 07:22:22PM +0000, BoQsc via 
 Digitalmars-d-learn wrote:
 Is it possible to declare empty pointer variable inside 
 function calls and pass its address to the function?
 
 These are sometimes required while using Win32 - Windows 
 Operating System API.
 
 * Empty pointer variables are used by functions to return 
 information after the function is done.
 
 My own horrible **suggestion** of empty pointer declaration 
 inside
 function call:
 `someFunction(uint & passingEmptyVariableForWrite);`
 
 What it would do:
 * A new variable is declared inside function call.
 * Address of that variable is passed to the function.
 * After function is done, you can refer to it for returned 
 value.
What's wrong with: uint* result; someFunction(&result); // use *result ? T
Nothing wrong. It would be just a more concise compact way to do the same. Also I mostly wanted to know if something like that is already possible in D language. It's not a huge loss if it is not possible.
Nov 23 2023
parent Julian Fondren <julian.fondren gmail.com> writes:
On Thursday, 23 November 2023 at 20:13:59 UTC, BoQsc wrote:
 Nothing wrong. It would be just a more concise compact way to 
 do the same.

 Also I mostly wanted to know if something like that is already 
 possible in D language.

 It's not a huge loss if it is not possible.
This is possible in Go: you dereference a literal constructor and it's either built on the stack or the heap depending on escape analysis. Example: ```go package main import "fmt" type T struct { data [5]byte } func usebuffer(a *T) *T { a.data[0] = 1 a.data[4] = 10 return a } func f() *T { return usebuffer(&T{}) // <-- } func main() { fmt.Println(f()) } ``` which is very close to this D that is explicitly allocating on the heap: ```d class T { byte[5] data; } T usebuffer(T a) { a.data[0] = 1; a.data[4] = 10; return a; } T f() { return usebuffer(new T()); // <-- } void main() { import std.stdio : writeln; writeln(f().data); } ``` Which if you want stack allocation, is very similar to: ```d class T { byte[5] data; } T usebuffer(T a) { a.data[0] = 1; a.data[4] = 10; return a; } void main() { import std.stdio : writeln; import std.typecons : scoped; writeln(usebuffer(scoped!T).data); } ``` but, I don't actually know if this is safe.
Nov 23 2023