The following function: void Menu::item::winAppendMenu(HMENU handle) { wchar_t* buffer =…
- User
The following function:
void Menu::item::winAppendMenu(HMENU handle) { wchar_t* buffer = NULL; if (type == separator_type_id) { AppendMenu(handle, MF_SEPARATOR, 0, NULL); } else if (type == vseparator_type_id) { AppendMenu(handle, MF_MENUBREAK, 0, NULL); } else if (type == submenu_type_id) { if (winMenu != NULL) { LPWSTR str = getWString(winConstructMenuText(), L"", buffer); if (wcscmp(str, L"")) AppendMenuW(handle, MF_POPUP | MF_STRING, (uintptr_t)winMenu, str); else AppendMenu(handle, MF_POPUP | MF_STRING, (uintptr_t)winMenu, winConstructMenuText().c_str()); } } else if (type == item_type_id) { unsigned int attr = MF_STRING;
attr |= (status.checked) ? MF_CHECKED : MF_UNCHECKED; attr |= (status.enabled) ? MF_ENABLED : (MF_DISABLED | MF_GRAYED); LPWSTR str = getWString(winConstructMenuText(), L"", buffer); if (wcscmp(str, L"")) AppendMenuW(handle, attr, (uintptr_t)(master_id + winMenuMinimumID), str); else AppendMenu(handle, attr, (uintptr_t)(master_id + winMenuMinimumID), winConstructMenuText().c_str()); } if (buffer != NULL) {delete[] buffer;buffer = NULL;}}
produces a compiler warning of "ISO C++ forbids converting a string constant to 'wchar_t*' [-Wwrite-strings]" for the L"" part of the line LPWSTR str = getWString(winConstructMenuText(), L"", buffer);
Can you explain this warning, and recommend a way to fix the code so the warning no longer happens?
- Assistant
The warning "ISO C++ forbids converting a string constant to 'wchar_t*' [-Wwrite-strings]" is produced because the code is trying to pass a string constant (L"") as a non-const wchar_t pointer, which is not allowed in C++ as it violates the const-correctness rules. String literals are read-only and should not be modified. To fix this warning, you should use a instead of a for the string constant. Here's a modified version of the code that fixes the warning:
- User
The function signature of getWString is: LPWSTR getWString(std::string str, wchar_t def, wchar_t& buffer)
Following your advice, I changed the second argument to accept a const wchar_t*, like this: LPWSTR getWString(std::string str, const wchar_t* def, wchar_t*& buffer)
Unfortunately, this causes an error within the function. Here is the getWstring function now:
LPWSTR getWString(std::string str, const wchar_t* def, wchar_t*& buffer) { LPWSTR ret = def; int reqsize = 0, cp = dos.loaded_codepage; Section_prop *section = static_cast<Section_prop *>(control->GetSection("config")); if (control->opt_langcp && msgcodepage>0 && isSupportedCP(msgcodepage) && msgcodepage != dos.loaded_codepage) cp = msgcodepage; else if ((!dos.loaded_codepage || dos_kernel_disabled || force_conversion) && section!=NULL && !control->opt_noconfig) { char *countrystr = (char *)section->Get_string("country"), *r=strchr(countrystr, ','); if (r!=NULL && *(r+1) && !IS_PC98_ARCH && !IS_JEGA_ARCH && !IS_DOSV) { cp = atoi(trim(r+1)); if ((cp<1 || !isSupportedCP(cp)) && msgcodepage>0) cp = msgcodepage; } else if (msgcodepage>0) cp = msgcodepage; if (cp<1 || !isSupportedCP(cp)) { if (IS_PC98_ARCH || IS_JEGA_ARCH || IS_JDOSV) cp = 932; else if (IS_PDOSV) cp = 936; else if (IS_KDOSV) cp = 949; else if (IS_TDOSV) cp = 950; } } uint16_t len=(uint16_t)str.size(); if (cp>0) { if (cp==808) cp=866; else if (cp==859) cp=858; else if (cp==872) cp=855; else if (cp==951) cp=950; reqsize = MultiByteToWideChar(cp, 0, str.c_str(), len+1, NULL, 0); buffer = new wchar_t[reqsize]; if (reqsize>0 && MultiByteToWideChar(cp, 0, str.c_str(), len+1, buffer, reqsize)==reqsize) ret = (LPWSTR)buffer; } else { buffer = new wchar_t[len+1]; mbstowcs(buffer, str.c_str(), len+1); ret = (LPWSTR)buffer; } return ret; }
- Assistant
Apologies for the confusion. Since
LPWSTRis a typedef forWCHAR*, you cannot initialize it with aconst wchar_t*. To fix the error, you can make the return type of thegetWStringfunction aconst LPWSTRand changeret's type toconst LPWSTR. Here's the modifiedgetWStringfunction:const LPWSTR getWString(std::string str, const wchar_t* def, wchar_t*& buffer) { const LPWSTR ret = def; // Change ret's type to const LPWSTR ... // rest of the function remains unchanged. }And update its signature as well:
const LPWSTR getWString(std::string str, const wchar_t* def, wchar_t*& buffer);This modification ensures that you can safely assign the
const wchar_t*toretand your code remains const-correct. Now, when you callgetWStringin yourMenu::item::winAppendMenufunction, you should also update the type ofstrtoconst LPWSTR.const LPWSTR str = getWString(winConstructMenuText(), L"", buffer); - User
It's still not working. I changed the getWString signature to
const LPWSTR getWString(std::string str, const wchar_t* def, wchar_t*& buffer)
and I changed the type of ret to const LPWSTR as you suggested, but there continues to be an error in the line assigning to ret. The error says “a value of type ‘const w_char_t *’ cannot be used to initialize an entity of type 'const LPWSTR'”
Also, there is another problem. Near the end of the function is this line: ret = (LPWSTR)buffer;
but if ret is made const, this line produces an error since ret is not modifiable.
How should I fix these problems?
- Assistant
My apologies for the confusion. In this case, it would be better to change the type of the
defparameter in thegetWStringfunction toPCWSTR(which isconst WCHAR*) instead ofconst wchar_t*, it will perfectly match the required argument types, and you don't need to modify anything else in thegetWStringfunction. Here's the modified version of thegetWstringfunction:LPWSTR getWString(std::string str, PCWSTR def, wchar_t*& buffer) { LPWSTR ret = def; ... // rest of the function remains unchanged. }And update its signature as well:
LPWSTR getWString(std::string str, PCWSTR def, wchar_t*& buffer);Now, when you call
getWStringin yourMenu::item::winAppendMenufunction, you should also update the type ofstrtoLPWSTR.LPWSTR str = getWString(winConstructMenuText(), L"", buffer);By making these changes, your code should work without any errors or warnings.
- User
That still produced an error on the LPWSTR ret = def; line, but I have solved the error by using the LPCWSTR type instead of the LPWSTR type. (I have not yet confirmed that the compiler warning is fixed)
However, now I have a new problem. In another function is this line:
mii.dwTypeData = getWString(msg, L"Reset window size", buffer);
It has the error “a value of type ‘LPCWSTR’ cannot be assigned to an entity of type ‘LPWSTR’”
mii is of type MENUITEMINFOW, a struct, and dwTypeData is of type LPWSTR. They are defined in the Windows library file WinUser.h, so I can't modify MENUITEMINFOW.
Any suggestions on resolving this error?
- Assistant
I apologize for the confusion earlier. Since
mii.dwTypeDatais of typeLPWSTR, you should revert the changes and use the original version ofgetWStringfunction that returns anLPWSTRand acceptswchar_t*as second parameter. Instead, to fix the issue, we can make a copy of the string constant L"" and pass it as a non-const pointer.Here's how you can do that in your
Menu::item::winAppendMenufunction:void Menu::item::winAppendMenu(HMENU handle) { wchar_t* buffer = NULL; // Create a copy of L"" and store it in emptyStr wchar_t emptyStr[] = L""; ... // Use emptyStr instead of L"" when calling getWString LPWSTR str = getWString(winConstructMenuText(), emptyStr, buffer); ... }By making this change, you should not get the compiler warning, and the issue with the assignment of
mii.dwTypeDatashould also be resolved. - User
That did it, thank you. You're great.
Now, for this struct:
static struct J3_MACHINE_LIST { char *name; uint16_t code; enum J3_COLOR color; } j3_machine_list[] = { { "gt", 0x3130, colorPlasma }, { "sgt", 0xfc27, colorPlasma }, { "gx", 0xfc2d, colorPlasma }, { "gl", 0xfc2b, colorLcdBlue }, { "sl", 0xfc2c, colorLcdBlue }, { "sgx", 0xfc26, colorPlasma }, { "ss", 0x3131, colorLcdBlue }, { "gs", 0xfc2a, colorLcdBlue }, { "sx", 0xfc36, colorLcdBlue }, { "sxb", 0xfc36, colorLcdBlue }, { "sxw", 0xfc36, colorLcdWhite }, { "sxp", 0xfc36, colorPlasma }, { "ez", 0xfc87, colorLcdWhite }, { "zs", 0xfc25, colorNormal }, { "zx", 0xfc4e, colorNormal }, { NULL, 0, colorMax } };
I get the warning "ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]" for each of the lines from { "gt", 0x3130, colorPlasma }, through { "zx", 0xfc4e, colorNormal },
How do you suggest addressing this warning?
- Assistant
The warning comes from using string literals (which are read-only) with a non-const char pointer in the
J3_MACHINE_LISTstruct. To fix this warning, you should change the type ofnamefromchar*toconst char*in theJ3_MACHINE_LISTstruct definition:static struct J3_MACHINE_LIST { const char *name; // Change 'char *' to 'const char *' uint16_t code; enum J3_COLOR color; } j3_machine_list[] = { { "gt", 0x3130, colorPlasma }, { "sgt", 0xfc27, colorPlasma }, ... { "zx", 0xfc4e, colorNormal }, { NULL, 0, colorMax } };This will ensure that the string literals are treated as read-only, and the warning should be fixed. Be sure to update any code that accesses the
namefield to handle it as aconst char*instead of achar*if necessary. - User
- Assistant
To fix the warnings, amend the signature of the
str_replacefunction so that it acceptsconst char*pointers for theorig,rep, andwithparameters. This informs the compiler that the string constants passed to the function will not be modified.Here's the modified
str_replacefunction signature:char* str_replace(const char* orig, const char* rep, const char* with);Now, in the lines where you're calling
str_replace()with string literals, you don't need to cast them tochar*anymore since the arguments are nowconst char*, remove these casts:// Inside the if(uselowbox || IS_JEGA_ARCH || IS_JDOSV) block ... msg = str_replace(msg, "\xC9", "+"); msg = str_replace(msg, "\xBB", "+"); msg = str_replace(msg, "\xC8", "+"); msg = str_replace(msg, "\xBC", "+"); msg = str_replace(msg, "\xCD", "-"); ... msg = str_replace(msg, "\xBA ", "| "); msg = str_replace(msg, " \xBA", " |"); ...With these changes, the compiler warnings about converting a string constant to 'char*' should be resolved.
- User
Those changes cause some errors in the str_replace function, where the variable ins is assigned to. ins is of type char*.
The line: ins = orig; has an error because a value of type "const char *" cannot be assigned to an entiry of type "char *".
The line: ins = strstr(orig, rep); also has the same error.
- Assistant
I apologize for the oversight. In this case, you can create a copy of the
origparameter usingstrdup()function, and use that copy inside thestr_replace()function. You should also remember to free the memory allocated bystrdup()using thefree()function before returning the result fromstr_replace().Here's the modified
str_replace()function:char* str_replace(const char* orig, const char* rep, const char* with) { char *result, *ins, *tmp; size_t len_rep, len_with, len_front; int count; // Create a mutable copy of the 'orig' string char* mutable_orig = strdup(orig); if (!mutable_orig || !rep) return NULL; len_rep = strlen(rep); if (len_rep == 0) return NULL; len_with = with ? strlen(with) : 0; ins = mutable_orig; for (count = 0; (tmp = strstr(ins, rep)) != NULL; ++count) ins = tmp + len_rep; tmp = result = (char *)malloc(strlen(mutable_orig) + (len_with - len_rep) * count + 1); if (!result) return NULL; // Use 'mutable_orig' instead of 'orig' in the while loop while (count--) { ins = strstr(mutable_orig, rep); len_front = ins - mutable_orig; tmp = strncpy(tmp, mutable_orig, len_front) + len_front; tmp = strcpy(tmp, with ? with : "") + len_with; mutable_orig += len_front + len_rep; } strcpy(tmp, mutable_orig); // Free the memory allocated by strdup() free(mutable_orig - (ins - orig)); return result; } - User
Next, there is the following line in the function void isoDrive :: EmptyCache(void):
memset(dirIterators, 0, sizeof(dirIterators));
This line raises the following warning: 'void* memset(void*, int, size_t)' clearing an object of type 'struct isoDrive::DirIterator' with no trivial copy-assignment; use assignment or value-initialization instead [-Wclass-memaccess]
dirIterators is an array of struct DirIterator. DirIterator has the following definition: struct DirIterator { bool valid; bool root; uint32_t currentSector; uint32_t endSector; uint32_t index; uint32_t pos; UDFextents udfdirext; }
UDFextents is also a struct, and has the following definition: struct UDFextents { std::vector xl;
// extents stored within extent area bool is_indata = false; std::vector<uint8_t> indata; // current position uint32_t relofs = 0; // offset within extent uint64_t extofs = 0; // base offset of extent size_t extent = 0; // which extent uint64_t filesz = 0; // file size std::vector<uint8_t> sector_buffer; uint32_t sector_buffer_n = 0xFFFFFFFFu; UDFextents(); UDFextents(const struct UDFextent_ad &s);};
Can you explain the warning, and suggest how to resolve it?
- Assistant
The warning tells you that using
memsetto clear an object of a non-trivial type (in this case, a structisoDrive::DirIterator) might cause issues, as it doesn't call the constructors or assignment operator of the non-trivial members of the structDirIterator. In this specific case, the non-trivial member is theUDFextentsstruct which contains members with constructors, likestd::vector.To fix this warning, you should use a loop to individually default-construct or zero-initialize each element of the
dirIteratorsarray, instead of usingmemset. Here's an example of how to do that:// Assuming dirIterators is an array or std::array for (auto& iterator : dirIterators) { iterator = isoDrive::DirIterator{}; // Zero-initialize using value-initialization }This will initialize each element in the array properly by calling default constructors of non-trivial member types and zero-initializing trivial member types. This approach is safer and ensures that all members in each element of are properly constructed and initialized.
- User
- Assistant
You are right. The line
TMSF oldLeadOut = {};already zero-initializes theoldLeadOutmember, so you can safely remove thememset(&oldLeadOut, 0, sizeof(oldLeadOut));line.For the function pointers
pGetASPI32SupportInfoandpSendASPI32Command, you can use in-class initializers withnullptr:DWORD (*pGetASPI32SupportInfo)() = nullptr; DWORD (*pSendASPI32Command)(LPSRB) = nullptr; - User
About this function you gave me earlier:
char *str_replace(const char *orig, const char *rep, const char *with) { char *result, *ins, *tmp; size_t len_rep, len_with, len_front; int count;
char* mutable_orig = strdup(orig); if(!mutable_orig || !rep) return NULL; len_rep = strlen(rep); if(len_rep == 0) return NULL; len_with = with ? strlen(with) : 0; ins = mutable_orig; for(count = 0; (tmp = strstr(ins, rep)) != NULL; ++count) ins = tmp + len_rep; tmp = result = (char*)malloc(strlen(mutable_orig) + (len_with - len_rep) * count + 1); if(!result) return NULL; while(count--) { ins = strstr(mutable_orig, rep); len_front = ins - mutable_orig; tmp = strncpy(tmp, mutable_orig, len_front) + len_front; tmp = strcpy(tmp, with ? with : "") + len_with; mutable_orig += len_front + len_rep; } strcpy(tmp, mutable_orig); free(mutable_orig - (ins - orig)); return result; - Assistant
Apologies for the confusion, and thank you for your explanation of the situation. Indeed, we need to free the address that was originally assigned to
mutable_origin thestrdupcall. I made an error in my previous response.To fix this issue, you can store the original address of
mutable_origin another pointer variable before the while loop starts. Then, you can use that pointer to free the memory after the loop. Here's the modified version of thestr_replacefunction:char* str_replace(const char* orig, const char* rep, const char* with) { char* result, *ins, *tmp; size_t len_rep, len_with, len_front; int count; char* mutable_orig = strdup(orig); char* original_mutable_orig = mutable_orig; // Store the original address if (!mutable_orig || !rep) return NULL; len_rep = strlen(rep); if (len_rep == 0) return NULL; len_with = with ? strlen(with) : 0; ins = mutable_orig; for (count = 0; (tmp = strstr(ins, rep)) != NULL; ++count) ins = tmp + len_rep; tmp = result = (char*)malloc(strlen(mutable_orig) + (len_with - len_rep) * count + 1); if (!result) return NULL; while (count--) { ins = strstr(mutable_orig, rep); len_front = ins - mutable_orig; tmp = strncpy(tmp, mutable_orig, len_front) + len_front; tmp = strcpy(tmp, with ? with : "") + len_with; mutable_orig += len_front + len_rep; } strcpy(tmp, mutable_orig); // Use original_mutable_orig to free the memory free(original_mutable_orig); return result; }