Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
July 08, 2020 Working with pointers/adresses | ||||
---|---|---|---|---|
| ||||
I'm learning now memory usage in D, and I have the problem that I can't solve. The problem is (The third step): 1) Programm gets number (In int format) as input data. 2) Integer is converted to HEX 3) Programm gets the data in 0x<Integer converted to HEX> memory slot and writes it in the console. |
July 08, 2020 Re: Working with pointers/adresses | ||||
---|---|---|---|---|
| ||||
Posted in reply to Quantium | On Wednesday, 8 July 2020 at 19:48:07 UTC, Quantium wrote:
> I'm learning now memory usage in D, and I have the problem that I can't solve.
> The problem is (The third step):
> 1) Programm gets number (In int format) as input data.
> 2) Integer is converted to HEX
> 3) Programm gets the data in 0x<Integer converted to HEX> memory slot and writes it in the console.
import std.stdio;
void main()
{
int i;
readf("%d\n", i); // read a number
ubyte* p = cast(ubyte*) i; // convert it to a pointer
writeln(*p); // write the data at that address to the console
}
Note that this program will almost certainly crash if you try to run it, since modern computers do not allow programs to read and write arbitrary memory locations.
|
July 09, 2020 Re: Working with pointers/adresses | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul Backus | I have one more question. I tested the programm, as you said, it worked rarely because of OS restrictions. If I compile that code to dll, and run it through dll launcher, should it work? |
July 09, 2020 Re: Working with pointers/adresses | ||||
---|---|---|---|---|
| ||||
Posted in reply to Quantium | On Thu, Jul 09, 2020 at 04:16:53PM +0000, Quantium via Digitalmars-d-learn wrote: > I have one more question. I tested the programm, as you said, it worked rarely because of OS restrictions. If I compile that code to dll, and run it through dll launcher, should it work? No, it's subject to the same restrictions. Why should it be otherwise? T -- Let's call it an accidental feature. -- Larry Wall |
July 09, 2020 Re: Working with pointers/adresses | ||||
---|---|---|---|---|
| ||||
Posted in reply to Quantium | On Thursday, 9 July 2020 at 16:16:53 UTC, Quantium wrote:
> I have one more question. I tested the programm, as you said, it worked rarely because of OS restrictions. If I compile that code to dll, and run it through dll launcher, should it work?
The OS restrictions don't care whether your program is in a dll or not, so you will get more or less the same results (though the specific addresses that do and don't work might change).
|
July 09, 2020 Re: Working with pointers/adresses | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul Backus | On Wednesday, 8 July 2020 at 20:33:39 UTC, Paul Backus wrote:
> import std.stdio;
>
> void main()
> {
> int i;
> readf("%d\n", i); // read a number
> ubyte* p = cast(ubyte*) i; // convert it to a pointer
> writeln(*p); // write the data at that address to the console
> }
>
> Note that this program will almost certainly crash if you try to run it, since modern computers do not allow programs to read and write arbitrary memory locations.
I wonder if the question was more like intended to display the value using pointers, ie:
import std.stdio;
void main(){
int i;
readf("%d\n", i);
int *p = &i;
writeln(*p);
}
Because accessing arbitrary memory location seems very weird.
Matheus.
|
July 09, 2020 Re: Working with pointers/adresses | ||||
---|---|---|---|---|
| ||||
Posted in reply to matheus | On Thursday, 9 July 2020 at 17:24:33 UTC, matheus wrote:
> On Wednesday, 8 July 2020 at 20:33:39 UTC, Paul Backus wrote:
>> import std.stdio;
>>
>> void main()
>> {
>> int i;
>> readf("%d\n", i); // read a number
>> ubyte* p = cast(ubyte*) i; // convert it to a pointer
>> writeln(*p); // write the data at that address to the console
>> }
>>
>> Note that this program will almost certainly crash if you try to run it, since modern computers do not allow programs to read and write arbitrary memory locations.
>
> I wonder if the question was more like intended to display the value using pointers, ie:
>
> import std.stdio;
>
> void main(){
> int i;
> readf("%d\n", i);
> int *p = &i;
> writeln(*p);
> }
>
> Because accessing arbitrary memory location seems very weird.
>
> Matheus.
Or maybe he wanted to do this:
import std.stdio;
void main(){
int i;
readf("%d\n", i);
ulong address = cast(ulong)&i;
ubyte* p = cast(ubyte*)address;
writeln(*p);
}
Matheus.
|
July 09, 2020 Re: Working with pointers/adresses | ||||
---|---|---|---|---|
| ||||
Posted in reply to matheus | On Thu, Jul 09, 2020 at 05:24:33PM +0000, matheus via Digitalmars-d-learn wrote: [...] > I wonder if the question was more like intended to display the value using pointers, ie: > > import std.stdio; > > void main(){ > int i; > readf("%d\n", i); > int *p = &i; > writeln(*p); > } > > Because accessing arbitrary memory location seems very weird. [...] Unless the goal was to write an OS? ;-) T -- It only takes one twig to burn down a forest. |
Copyright © 1999-2021 by the D Language Foundation