So i needed a function for my program that will find all the files in exe-location/Files. Since i have yet to delve into windows programming, i just found to differnt bits of code and splice them into my Frankenstein function here. the first bit gets the working directory of the exe, and then appends "//Files\\*" to it so the next section can return the name of all the files in that directory.
My question is about the char* "buffer", and whether or not i need to delete/free it from memory. when i put "delete buffer" at the end, it crashes the program right after i build it, but then when i run it again from the same build, it will no longer crash. not including the delete statement causes no problems, but i'm curious if that is memory leak.
any help as to why this occurs, whether or not leaving buffer creates a memory leak, or tips on a better way to achieve my desired result(which is storing strings containing all the file names in that folder) would help
Therefore, you have to call free() to release the memory. Calling delete on memory allocated with malloc() is actually undefined behavior, so the compiler can do whatever it wants, including crashing your program.
I should have mentioned that i have tried "free()". the result is around the same as delete. as in it crashes the program. so that wouldnt seem to be the main issue.
I just tried running the program with
delete: Seemingly random crashes
free(): Seemingly random crashes
no deletion: no crash
Alright! i spent some time commenting out various bits and found out that "strcat" was what was causing the deletion error.
i fixed the error by creating a string, copying the buffer to that, and appending the rest of the file path to that string. This works perfectly.
*side note, both free() & delete do not cause crashes now