aeroson
aeroson

Reputation: 1129

anyway to delete only part of array?

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;
    return *this;
}

Upvotes: 1

Views: 1455

Answers (2)

Matteo Italia
Matteo Italia

Reputation: 126787

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.


  1. 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.

Upvotes: 2

dreamlax
dreamlax

Reputation: 95335

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.

Upvotes: 3

Related Questions