Help on array pointers
Joe at bloow.edu
Joe at bloow.edu
Fri Sep 15 02:25:09 UTC 2023
On Thursday, 14 September 2023 at 14:21:09 UTC, Vino wrote:
> Hi All,
>
> Request your help to guide me in understanding about
> pointers, the below code works,I have few question which i need
> your help for better understanding.
>
> Questions:1
> ```
> char[] invalid = (cast(char*)malloc(char.sizeof *
> length))[0..length];
> ```
> The above statement allocate memory for char type and the size
> of the allocated memory is char.sizeof * length so what is the
> use of this "[0..length]";
>
> Question:2
> ```
> char[]* invalidptr = &invalid;
> ```
> Is this the right way to create a array pointer.
>
> Question: 3
> ```
> ST1: char[] invalid = (cast(char*)malloc(char.sizeof *
> length))[0..length];
> ST2: char[]* invalid = (cast(char*)malloc(char.sizeof *
> length))[0..length];
> ```
> What is the difference between the above to statement.
>
> Question: 4
> Who do we free the memory allocated.
> Code:
> ```
> auto ref testNames(in string[] names) {
> enforce(!empty(names), "Names cannot be Empty or Null");
>
> import core.stdc.stdlib;
> import std.algorithm: any, canFind;
>
> size_t length = 20;
> char[] invalid = (cast(char*)malloc(char.sizeof *
> length))[0..length];
> char[]* invalidptr = &invalid;
>
> version(Windows) { (*invalidptr) =
> ['\'','\"',':',';','*','&','[',']','-','+','$','#','<','>','{','}','(',')']; }
>
> foreach(i; names.dup) {
> auto result = i.any!(a => (*invalidptr).canFind(a));
> if(result) { throw new Exception("Invalid Name passed:
> %s".format(i)); }
> }
> string[] _names = names.dup;
> return _names;
> }
> ```
> From,
> Vino
A pointer is a type that points to something. It's literally that
simple. Every piece of data and code exist somewhere in memory.
Every piece of memory has an address. The address is what a
pointer contains. Sometimes we want a type that is the address
itself rather than a value/number.
https://run.dlang.io/gist/19c63d325ee412df23bdbefabce111b9
import std, std.stdio, core.stdc.stdlib;
void main()
{
float x = 43.534f; // Creates a float type that hold
sthe value 43.534
writeln("Value of x = ", x); // Displays it's value
writeln("Address of x = ", &x); // Displays it's address
writeln();
float* y = null; // creates a pointer of type float note
that it is point to no memory location(null) and so it is an
error to store a value to the location
writeln("Value of y = ", y); // Displays it's
address(remember, y is a ptr to a float, not a float)
y = new float(); // This allocates free/unused memory(of
float type) to y so we can store a value.
*y = 50.34f; // assigns it the value 50.34. We have to
use * because if we didn't we would be trying to change the
address of y
writeln("Value of y = ", y); // Displays it's
address(remember, y is a ptr to a float, not a float)
writeln("Address of y = ", &y); // Writes it's address(y
is a pointer but it also is stored in a location in memory and
hence has an address
writeln("Dereferenced value of y = ", *y); // Displays
the value y is pointing to interpreting it as a float(the base
type of the pointer)
writeln("Dereferences the value of y as if it were an int
= ", *(cast(int*)y)); // Displays the value y is pointing to
interpreting it as a int. We have to force the compilier to
reinterpret it as an int*.
writeln();
byte[] z; // Creates an array of bytes. That is, the
compiler will create a pointer to an array of memory and track
it's length and deal with memory allocation and all that.
writeln("Value of z = ", z); // Displays it's
address(remember, z is a ptr to a float, not a float)
writeln("Address of z = ", &z); // Note that z is an
array but it is a pointer to and &z gets the address where the
"array" is stored.
writeln("Value of z's pointer = ", z.ptr); // Displays
it's address(remember, y is a ptr to a float, not a float)
writeln("Length of z = ", z.length); // Writes it's
address(y is a pointer but it also is stored in a location in
memory and hence has an address
writeln();
z ~= 4; // We can store/append a value to our array. The
compiler will take care of dealing with allocating memory and all
that.
writeln("Value of z = ", z); // Displays it's
address(remember, z is a ptr to a float, not a float)
writeln("Address of z = ", &z); // Note that z is an
array but it is a pointer to and &z gets the address where the
"array" is stored.
writeln("Value of z's pointer = ", z.ptr); // Displays
it's address(remember, y is a ptr to a float, not a float)
writeln("Length of z = ", z.length); // Writes it's
address(y is a pointer but it also is stored in a location in
memory and hence has an address
z ~= 54; // We can store/append a value to our array. The
compiler will take care of dealing with allocating memory and all
that.
writeln("Value of z = ", z); // Displays it's
address(remember, z is a ptr to a float, not a float)
writeln("Address of z = ", &z); // Note that z is an
array but it is a pointer to and &z gets the address where the
"array" is stored.
writeln("Value of z's pointer = ", z.ptr); // Displays
it's address(remember, y is a ptr to a float, not a float)
writeln("Length of z = ", z.length); // Writes it's
address(y is a pointer but it also is stored in a location in
memory and hence has an address
z ~= 14; // We can store/append a value to our array. The
compiler will take care of dealing with allocating memory and all
that.
writeln("Value of z = ", z); // Displays it's
address(remember, z is a ptr to a float, not a float)
writeln("Address of z = ", &z); // Note that z is an
array but it is a pointer to and &z gets the address where the
"array" is stored.
writeln("Value of z's pointer = ", z.ptr); // Displays
it's address(remember, y is a ptr to a float, not a float)
writeln("Length of z = ", z.length); // Writes it's
address(y is a pointer but it also is stored in a location in
memory and hence has an address
writeln("\nNotice now that the value of z's pointer is
not null but some address, the reason is that the compiler
allocated the memory for us so we would not get an exception
trying to write to unallocated memory.\n");
// We can use a pointer as an array also, this is the
"old school way of creating arrays".
int qlen = 5;
int* q = cast(int*)malloc(int.sizeof*qlen);
*(q+0) = 4; // Store a value at the first entry of q;
*(q+1) = 54; // Store a value at the second entry of q;
*(q+2) = 14; // Store a value at the third entry of q;
writeln(*(q+1));
writeln(*cast(ushort*)(q)); // We temporarily turned q in
to a ushort pointer and then got it's value(it's the same as the
first since this is effectively just casting an int to a short
and 4 fits in both.
}
More information about the Digitalmars-d-learn
mailing list