char myWord[20];
I want to delete last 10 chars of this array, I mean freeing the memory thats being used by last 10 chars. Is there anyway to do this ?
Heres an example function for substring where it might be useful.
str& substring(uint Start, uint Count){
for(uint x=0; x<Count; x++){
mString[x] = mString[x+Start];
}
// SOMEHOW FREE MEMORY OF mString FROM [Start+Count] TO [mLength]
mLength = Count;
开发者_如何学Go return *this;
}
No, not really. You can really only do it if you obtained a pointer to memory from malloc
, and then used realloc
to change the size, but I'm quite sure that it's not even guaranteed that it will free the unused bytes.
With stack-allocated arrays1, no. You can only use some special character to mark the "logical end" of the array; for strings usually the NUL character (0) is used.
- And even with stuff allocated on the heap usually you don't have that much granularity in allocations to free just 10 bytes of a 20-bytes long string.
精彩评论