2
char *p = new char[200];
char *p1 = p;
char *p2 = &p[100];
delete [] p1;
Btw ce n'est pas un test ou quoi que ce soit je vraiment besoin de savoir ça :)Que ferait ce code? (Gestion de la mémoire)
char *p = new char[200];
char *p1 = p;
char *p2 = &p[100];
delete [] p1;
Btw ce n'est pas un test ou quoi que ce soit je vraiment besoin de savoir ça :)Que ferait ce code? (Gestion de la mémoire)
// allocate memory for 200 chars
// p points to the begining of that
// block
char *p = new char[200];
// we don't know if allocation succeeded or not
// no null-check or exception handling
// **Update:** Mark. Or you use std::no_throw or set_new_handler.
// what happens next is not guranteed
// p1 now points to the same location as p
char *p1 = p;
// another pointer to char which points to the
// 100th character of the array, note that
// this can be treated as a pointer to an array
// for the remaining 100-odd elements
char *p2 = &p[100];
// free the memory for 200 chars
delete [] p1;
// **Update:** Doug. T
// [...] p and p2 are now pointing to freed memory
// and accessing it will be undefined behavior
// depending on the executing environment.
thx cela répond à ma question, mais est-il possible de supprimer uniquement les 100 premiers? – Michael
Vous pouvez réaffecter - la méthode normale serait d'allouer un nouveau bloc de 100 octets de taille, puis copiez le deuxième 100 octets dans ce –
Non, vous ne pouvez pas avoir la suppression partitionnée. Vous devez effectuer un deuxième nouvel appel, copier la partie que vous souhaitez enregistrer et supprimer l'original. – dirkgently