I am creating a application that downloads stuff from a url into a file and having a big problem with getting the substring of the url to use to create as the downloads name. I have spent the last 4 hours researching, watching videos, asking ai and many more but I just simply cant figure this out. my problem is when I allocate memory on the heap to store the last sub string of a url that I get from my own function once the statement completes the value just gets completely corrupted to a crap ton of f. This only happens when using this one function of mine which I assume is causing some sort of memory corruption but I cant figure it out.
below you can see i am allocating a char the can hold up to 256 characters which should be more than enough for pretty much any substring of a url and declaring and initializing it using my getLastTextUrl function
char* alloc2 = new char[256];
const char* AIO = getLastTextUrl("https://habitual.cc/wp-content/uploads/2023/10/RuntimeFiles.zip", alloc2);
my getLastTextUrl function
const char* getLastTextUrl(const std::string &url, char* &alloc)
{
size_t pos = url.find_last_of("/") + 1;
if (pos != std::string::npos)
{
alloc = (char*)url.c_str() + pos;
return url.c_str() + pos;
}
return url.c_str();
}
I have debugged it multiple times the function works correctly I believe after alloc = (char*)url.c_str() + pos;runs alloc is assigned correctly (RuntimeFiles.zip) I then step into twice to finish the function and it takes me back to what the main function and is still highlighting as it is debugging const char* AIO = getLastTextUrl("https://habitual.cc/wp-content/uploads/2023/10/RuntimeFiles.zip", alloc2); When hovering over alloc2 now it holds the correct string (RuntimeFiles.zip) but the moment I step into the next statement which is just char* alloc = new char[256]; alloc2 and AIO corrupts and becomes alot of f like I mentioned.
I am pretty new to c++ and this is my first time every posting on stack overflow sorry if there is some sloppy code or something doesnt make sense if anyone needs some clarification please let me know same with suggestions.
Clearly this
should be this
Your code copies a pointer (which then becomes invalid), when clearly you wanted to copy the string characters, not the pointer.
There is another issue, you test
posto see if it equalsstd::string::nposbut only after you've added one to it, which obviously won't work. But there is a useful trick you can play here, it's guaranteed thatstd::string::npos + 1equals0. So actually you don't need the test at all. Just copy characters fromurl.c_str() + poswhich will either be the whole string (if the slash is not found) or one character past the last slash (if a slash is found).But really don't use C strings in a C++ program. There are more complicated and much more fragile. Here's your function rewritten to use C++ strings (and also using the trick mentioned above).
If at some point you need to pass the returned string to an old API that requires a C string then use
.c_str()on the C++ string at the point you call the old API, not before. That essentially was the design mistake your code was making.