Help on array pointers

Vino akashvino79 at gmail.com
Fri Sep 15 15:27:00 UTC 2023


On Friday, 15 September 2023 at 02:25:09 UTC, Joe wrote:
> On Thursday, 14 September 2023 at 14:21:09 UTC, Vino wrote:
>> [...]
>
> 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.
>
> [...]

Hi Joe,
Thank you very much for the explanation can you please correct me 
if my understanding is incorrect
```
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.
```
If we use the above method then :
The compiler takes care of initilizing the array and free the 
memory after the usage.
And this is the recommended method.

```
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);
```	
If we use the above method then :
We need to manual initilize the array.
Ensure that the memory is freed after the usage using try/final 
block.

By default the memory allocation for arrays in D is based on GC 
(expect for std.array containers) if we want to reduce/avoid GC 
then we need to use the old school way of creating the arrays.

In case of using the old school ways then can you guide me what 
is wrong in my earlier code that I am getting the below error and 
how do I correct the same.

Error
```
Invalid Name passed: /T&name
double free or corruption (out)
Error: program killed by signal 6
```



More information about the Digitalmars-d-learn mailing list