emufear
Πολύ δραστήριο μέλος
Ο emufear αυτή τη στιγμή δεν είναι συνδεδεμένος. Είναι 37 ετών και επαγγέλεται Φοιτητής/τρια. Έχει γράψει 1,730 μηνύματα.
15-06-05
16:34
Διαβάστε τα πάντα για αυτή: https://www.digitalmars.com/d/
Παραθέτω ένα παραδειγματάκι σε σχέση με την απλή C:
Creating an array of variable size
In C, C cannot do this with arrays. It is necessary to create a separate variable for the length, and then explicitly manage the size of the array:
In D, D supports dynamic arrays, which can be easily resized. D supports all the requisite memory management.
Παραθέτω ένα παραδειγματάκι σε σχέση με την απλή C:
Creating an array of variable size
In C, C cannot do this with arrays. It is necessary to create a separate variable for the length, and then explicitly manage the size of the array:
Code:
#include <stdlib.h>
int array_length;
int *array;
int *newarray;
newarray = (int *)
realloc(array, (array_length + 1) * sizeof(int));
if (!newarray)
error("out of memory");
array = newarray;
array[array_length++] = x;
In D, D supports dynamic arrays, which can be easily resized. D supports all the requisite memory management.
Code:
int[] array;
array.length = array.length + 1;
array[array.length - 1] = x;
Σημείωση: Το μήνυμα αυτό γράφτηκε 18 χρόνια πριν. Ο συντάκτης του πιθανόν να έχει αλλάξει απόψεις έκτοτε.