delete or delete [ ]??
-
In the following code snippet which delete should I use?? char *ptr1=new char[100]; char arr[10]="abcdefghi"; strcpy(ptr1,arr); //delete ptr1; delete []ptr1; Should I use delete ptr1; or delete []ptr1;
-
In the following code snippet which delete should I use?? char *ptr1=new char[100]; char arr[10]="abcdefghi"; strcpy(ptr1,arr); //delete ptr1; delete []ptr1; Should I use delete ptr1; or delete []ptr1;
-
In the following code snippet which delete should I use?? char *ptr1=new char[100]; char arr[10]="abcdefghi"; strcpy(ptr1,arr); //delete ptr1; delete []ptr1; Should I use delete ptr1; or delete []ptr1;
delete[] because the memory has been allocated with new[]. The rule is simple: if memory is allocated with new[], then use delete[]. If memory is allocated with new, use delete.
-
In the following code snippet which delete should I use?? char *ptr1=new char[100]; char arr[10]="abcdefghi"; strcpy(ptr1,arr); //delete ptr1; delete []ptr1; Should I use delete ptr1; or delete []ptr1;
if u r allocating an array of data say *ptr=new [100] then go for delete[] complier will know an array of data has to be deleted.Otherwise if u dont do this only the first block of data (depending upon data type say char only first byte in case of char will be deleted this memory leak!) remember this combination new-->delete new[]-->delete[]
-
In the following code snippet which delete should I use?? char *ptr1=new char[100]; char arr[10]="abcdefghi"; strcpy(ptr1,arr); //delete ptr1; delete []ptr1; Should I use delete ptr1; or delete []ptr1;