www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Question on Container Array.

reply Vino.B <vino.bheeman hotmail.com> writes:
Hi All,

   Can some one explain me on the below question.

Q1: void main (Array!string args) : Why can't we use container 
array in void main?

Q2: What is the difference between the below?
insert, insertBack
stableInsert, stableInsertBack
linearInsert, stableLinearInsert, stableLinearInsert

Q3: Storing the data in a container array store's the data in 
memory which is managed by malloc/free, where as operation such 
as appending data using any of the above nor "~=" is managed by 
gc, is my understanding correct.


From,
Vino.B
Sep 18
parent Eugene Wissner <belka caraus.de> writes:
On Monday, 18 September 2017 at 11:47:07 UTC, Vino.B wrote:
 Hi All,

   Can some one explain me on the below question.

 Q1: void main (Array!string args) : Why can't we use container 
 array in void main?

 Q2: What is the difference between the below?
 insert, insertBack
 stableInsert, stableInsertBack
 linearInsert, stableLinearInsert, stableLinearInsert

 Q3: Storing the data in a container array store's the data in 
 memory which is managed by malloc/free, where as operation such 
 as appending data using any of the above nor "~=" is managed by 
 gc, is my understanding correct.


 From,
 Vino.B
Q1: I think that someone could explain it better, but basically a program gets its arguments as an array of C strings. So a C main looks like: main(int argc, char **argv); To make this a bit safer, D's main works with an array of strings instead of pointers. D's main function is called from druntime: https://github.com/dlang/druntime/blob/95fd6e1e395e6320284a22f5d19fa41de8e1dcbb/s c/rt/dmain2.d#L301. And it wouldn't be that cool to make the druntime depend on phobos and containers. But theoretically it would be possible to make 'void main (Array!string args)' with custom dmd and druntime. Q2: They are the same for Array. But theoretically they can be defined differently. "stable" in "stableInsert" just means that a range got from container can be used after changing the container. So if you get an Array range with Array[], you can still use this range after stableInsert. "insert" is just shorter than "insertBack". Q3: "~=" uses GC only for built-in arrays. You can define your own "~=" for containers. "~=" for Array calls insertBack. So it will use malloc here.
Sep 18