I'm very new to C++ and is trying to compile a program, however it's leaking memory and this part of the code.
char* ReadString(u_int address, unsigned int size) {
    char* data = new char[size];
    for(int i=0; i < size; i++){
        vm_readv(reinterpret_cast<void*>(address + (sizeof(char)*i)), reinterpret_cast<void*>(data + i), sizeof(char));
        if(data[i] == '\0'){
            break; 
        }
    }
    return data;
}
I'm not sure how to fix it.
I've tried to add delete[] data; before break; and it stops the memory, the program also runs.
But i think this may crash the program somewhere after??
At any rate, I'm confused on how to properly deal with the leak.
I've been reading and using a smart pointer might be a good way to resolve it, but again, I don't know how to properly turn char* data = new char[size]; into a pointer without breaking data[i].
Edit: Attempts on stopping the leak, the leak stop with this. But I think it may cause a crash later?
char* ReadString(u_int address, unsigned int size) {
    char* data = new char[size];
    for(int i=0; i < size; i++){
        vm_readv(reinterpret_cast<void*>(address + (sizeof(char)*i)), reinterpret_cast<void*>(data + i), sizeof(char));
        if(data[i] == '\0'){
            delete [] data; // ------->>>>>>> Here
            break; 
        }
    }
    return data;
}
 
    