As part of codeform, I have a dynamically allocated array of pointers. Some of the pointers point to the same location in memory; but if they do, they will be consecutive. So when I go to free the array, I use
However, I have been reading a C book*, and it seems to indicate that you can't do anything with "invalid pointers" (which can be formed by freeing the memory that a pointer pointed to), including compare them with anything else. This seems silly to me; I'm not dereferencing the pointer or anything. But still, I'm wondering if the above code is valid or not. If it isn't, that won't matter, for I could set duplicate pointers to NULL beforehand:Code:for(x = 0; x < size; x ++) { if(!x || array[x] == array[x-1]) free(array[x]); } free(array);
* C: A reference manual, fifth editon, page 139.Code:if(size) for(x = 0; x < size-1; x ++) { if(array[x] == array[x+1]) array[x] = 0; } for(x = 0; x < size; x ++) { free(array[x]); }