On Friday, 15 September 2023 at 15:27:00 UTC, Vino wrote:
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
```
Hi All,
At last was able to resolve the issue, but not sure whether
this is the reight solution.
Code:
```
import std.stdio: writeln;
import std.exception: enforce;
import std.range: empty;
import std.format: format;
auto ref testNames(in string[] names) {
enforce(!empty(names), "Names cannot be Empty or Null");
import core.stdc.stdlib;
import std.algorithm: any, canFind;
string[] _names;
size_t len = 19;
char[] invalid = (cast(char*)malloc(char.sizeof *
len))[0..len];
invalid[0 ..len] = 0; //Array Initlization
try {
version(Posix) {
invalid =
['\'','\"',':',';','*','&','/','[',']','-','+','$','#','<','>','{','}','(',')'];
}
foreach(i; names.dup) {
auto result = i.any!(a => invalid.canFind(a));
if(result) { throw new Exception("Invalid Name passed:
%s".format(i)); }
else {_names = names.dup; return _names; }
}
} catch(Exception e) { writeln(e.msg); }
finally { invalid = null; free(invalid.ptr); } // added
invalid = null resolved the issue
return _names;
}
void main () {
writeln(testNames(["/T&name"]));
}
From,
Vino