Menu

Question on fix for this error with --enable=all

2023-05-01
2023-05-05
  • Michael Setzer II

    Program has 4 qsort options that give this.
    fixzzallbz.cpp:557:42: style: C-style pointer casting [cstyleCast]
    qsort(datax, num2, sizeof(datax[0]), (int ()(const void , const void *))strcmp);

    Usually find it gives a do you mean option..
    Have never had a problem, but if there is a more correct way to do it, would like to update it.
    Thanks

     
  • CHR

    CHR - 2023-05-01

    A few ideas:

    • use std::sort with a lambda
    • write a wrapper function and use static_cast on the arguments
    • use reinterpret_cast on strcmp
     

    Last edit: CHR 2023-05-01
  • Michael Setzer II

    Not clear on first one, since what I've found seems sort is for numbers?
    Found something with second one.
    Create
    int cmpstr(void const a, void const b)
    {
    char const aa = (char const )a;
    char const bb = (char const )b;
    return strcmp(aa, bb);
    }
    and change strcmp option to just cmpstr, but it then gives this output.
    style: C-style pointer casting [cstyleCast]
    char const aa = (char const )a;
    ^
    style: C-style pointer casting [cstyleCast]
    char const bb = (char const )b;
    ^
    So, changes style issue on each qsort to just the two lines in cmpstr.

    The cppcheck without --enable=all doesn't show anything, but with --enable=all it does.
    The program seems to run fine with the original or with the cmpstr mode.

    Just wondering if there is a more correct method to do this .

    Thanks for the info. Will have to continue looking.

     
  • CHR

    CHR - 2023-05-03

    Using the -v option, the output is

    bar.cpp:2:18: style: C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected. See also: https://www.securecoding.cert.org/confluence/display/cplusplus/EXP05-CPP.+Do+not+use+C-style+casts. [cstyleCast]
      return strcmp((const char*)a, (const char*)b);
                     ^
    

    That's why I wrote "use static_cast on the arguments".
    Btw, std::sort is a generic sorting algorithm.

     
  • Michael Setzer II

    I must be missing something. I've look at a number of links that say they work, but compiler gives a bunch of error lines?? The old style shell sort works perfectly?

    Tried a number of things with various different cmp setups, but none compiled.
    Have fedora 37 with latest compiler.
    real programs have arrays of 300 to 700 strings, but did a simple example.

    #include <cstdio></cstdio>

    include <cstdlib></cstdlib>

    include <cstring></cstring>

    include <algorithm></algorithm>

    using namespace std;
    bool cmp(const char str1, const char str2){
    if(strcmp(str1, str2)<0) return true;
    else return false;
    }
    int main()
    {
    char temp[20], test[10][20]={ "Xray", "Milk", "Apple", "Orange", "Cat", "Dog", "Tree", "Motorcycle", "Tishawnna"};
    int i,j;
    for(i=0;i<10;i++) printf("%s\n",test[i]);
    // sort(test,test+8,cmp);
    // std::sort(std::begin(test), std::end(test));
    // std::sort(test, test+8, { return strcmp(lhs,rhs) < 0; });
    for(i=0;i<9;i++)
    for(j=i+1;j<=9;j++)
    if(strcmp(test[i],test[j])>0) { strcpy(temp,test[i]); strcpy(test[i],test[j]); strcpy(test[j],temp); };
    for(i=0;i<10;i++) printf("%s\n",test[i]);
    return 0;
    }

     
  • Michael Setzer II

    Tried some things, and found a solution, but is strange.
    sort2 version has cppcheck --enable all report the earlier style message.
    sort3 and sort4 don't give error, but strangely all three compiles produce identical binary files after strip??
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    using namespace std;
    int cmpstr(void const a, void const b)
    {
    // from sort2.cpp
    // char const aa = (char const )a;
    // char const bb = (char const )b;
    // return strcmp(aa, bb);
    // from sort3.cpp
    // char const aa = static_cast<char const="" *="">(a);
    // char const </char>
    bb = static_cast<char const="" *="">(b);
    // return strcmp( aa, bb);
    // from sort4.cpp
    return strcmp( static_cast<char const="" *="">(a), static_cast<char const="" *="">(b));
    }</char></char></char></cstring></cstdlib></cstdio>

    int main()
    {
    char test[10][20]={ "Xray", "Milk", "Apple", "Orange", "Cat", "Dog", "Tree", "Motorcycle", "Tishawnna"};
    int i;
    for(i=0;i<10;i++) printf("%s\n",test[i]);
    qsort(test, 9, sizeof(test[0]), cmpstr);
    for(i=0;i<10;i++) printf("%s\n",test[i]);
    return 0;
    }

    So, static_cast gets rid of style error, but binary code is same??
    
     

    Last edit: Michael Setzer II 2023-05-04
  • CHR

    CHR - 2023-05-04

    This is expected, since the semantics of the program remain the same (it is a style warning after all).
    std::sort must be able to assign the elements that it sorts.
    Example: https://godbolt.org/z/c8hsf3jjE

     
  • Michael Setzer II

    Thanks for al the info. Search had mentioned something about there error might cause run time error that the compiler wouldn't catch with the Cstyle casting? That's why I was looking for solution. Seems the sort would require complete redo of the C style arrays that are created elsewhere in program. Example seems to require more than what is needed with qsort or the simple old shell sort.
    Interesting info. thanks again.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.