CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Hybrid View

  1. #1
    Join Date
    Mar 2011
    Posts
    3

    [RESOLVED] deprecated conversion from string const to char*

    I am having an error after compiling the program with above warning. i believe i'm doing something wrong my code is as below. program terminates with seg fault

    in main i'm calling sqlfunction.cpp to query mysql database.
    timer loop dumps the result to the mysql database by querying sqlfunction.
    program runs in the main ok even with the warnings. but it doesnt run in the timer loop and terminates with seg fault.????? please help what should i change in this

    in main cpp


    void *timer(void *arg)
    {

    int ret;
    char command[1024];
    double Myarray[3000];

    while (1){
    clock_t endwait;
    endwait = clock() + 1 * CLOCKS_PER_SEC;
    while (clock() < endwait){}//do nothing

    float mul=0;
    mul= GetNemo(0);
    mul= MyFloat(mul);
    printf("timed float ===== %f\n",mul);

    int i = 0;
    sprintf(command,"insert into meter (Voltage) values (%f);",mul); //warnign here
    ret = Querydb(command,Myarray);
    printf("Querydb = %d\n",ret);
    printf("__________\n");
    }

    return arg;
    }
    int main(int ac, char **av)
    {
    PARAM p;

    int s, ret;

    ret = Opendb("localhost","root","pass","tetsdB"); //warning nere
    printf("opendb = %d\n",ret);

    ret = thread.create(timer,NULL); //THREAD FOR TIMER
    printf("thread_timer ret = %d\n", ret);

    MYSQL_RES *res;
    double Myarray[128];
    int i = 0;
    char command[1024];

    ret = Querydb("insert into table (Voltage) values (10);",Myarray);
    if(ret != 0){
    for(i=0;i<128;i++){
    printf("myarray ==== %f\n",Myarray[i]);}
    mysql_free_result(res);
    }



    .h file

    extern bool Opendb(char *pc, char *user, char *pass, char *db);
    extern bool Closedb();
    extern bool Querydb(char *query, double *Myarray);
    extern int GetNemo(int bytes);



    in sqlfuntion.cpp one of the function is..


    bool Querydb(char *query, double Myarray[3000])
    {
    int i=0;
    char buffer[50];
    double value1;
    if (mysql_query(conn, query)) {
    fprintf(stderr, "%s\n", mysql_error(conn));
    return 0;
    }

    else {
    res = mysql_use_result(conn);
    //output table name
    printf("MySQL Tables in mysql database:\n");
    while ((row = mysql_fetch_row(res)) != NULL){
    printf("%s\n",row[0]);
    sprintf(buffer,"%s",row[1]);
    value1 = atof(buffer);
    Myarray[i]=value1;
    i++;
    }
    return 0;
    }
    }

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: deprecated conversion from string const to char*

    Quote Originally Posted by rpar View Post
    I am having an error after compiling the program with above warning. i believe i'm doing something wrong my code is as below. program terminates with seg fault
    Please use code tags when posting code. The code you posted is practically unreadable.

    Second, why not go through your code with a debugger? At the very least, please find out which line is causing the issue. If it's one of those database calls or return values from these calls, then how can we help you if we don't have the database or the data that is being returned?

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Mar 2011
    Posts
    3

    Angry Re: deprecated conversion from string const to char*

    Thanks Paul. sorry about the first post. Ive been mucking around with this since yesterday for 5-6 hours. if you look around the timer loop you will find the return value is just a float ("mul" check comment with //)
    "Querydb" function works fine in main even though i have compile error.
    but if the same function is called from the timer thread then the program crashes with seg fault.(with compiler spitting out above warning but no error)

    I know the issue and it is the mysql string i'm using with the function in sqlfunction.cpp file
    Code:
    bool Querydb(char * query, double Myarray)
    but i dont know how i define the query in Querydb so it can take strings as an input

    regards



    in main cpp

    Code:
    void *timer(void *arg)
    {
    
    int ret;
    char command[1024];
    double Myarray[3000];
    
    while (1){
    clock_t endwait;
    endwait = clock() + 1 * CLOCKS_PER_SEC;
    while (clock() < endwait){}//do nothing
    
    float mul=0;
    mul= GetNemo(0);
    mul= MyFloat(mul);
    printf("timed float ===== %f\n",mul);  //works fine and returns value in float
    
    int i = 0;
    sprintf(command,"insert into meter (Voltage) values (%f);",mul); //warning here
    ret = Querydb(command,Myarray);
    printf("Querydb = %d\n",ret);
    printf("__________\n");
    }
    
    return arg;
    }
    int main(int ac, char **av)
    {
    PARAM p;
    
    int s, ret;
    
    ret = Opendb("localhost","root","pass","tetsdB"); //warning nere
    printf("opendb = %d\n",ret);
    
    ret = thread.create(timer,NULL); //THREAD FOR TIMER
    printf("thread_timer ret = %d\n", ret);
    
    MYSQL_RES *res;
    double Myarray[128];
    int i = 0;
    char command[1024];
    
    ret = Querydb("select Voltage from meter where Num between 'xvalue' and 'yvalue';",Myarray);//works fine with warning 
    if(ret != 0){
    for(i=0;i<128;i++){
    printf("myarray ==== %f\n",Myarray[i]);}
    mysql_free_result(res);
    }
    sqlfunction.h file
    Code:
    extern bool Opendb(char *pc, char *user, char *pass, char *db);
    extern bool Closedb();
    extern bool Querydb(char *query, double *Myarray);
    extern int GetNemo(int bytes);

    in sqlfuntion.cpp one of the function is..

    Code:
    bool Querydb(char *query, double Myarray[3000])
    {
    int i=0;
    char buffer[50];
    double value1;
    if (mysql_query(conn, query)) {
    fprintf(stderr, "%s\n", mysql_error(conn));
    return 0;
    }
    
    else {
    res = mysql_use_result(conn);
    //output table name
    printf("MySQL Tables in mysql database:\n");
    while ((row = mysql_fetch_row(res)) != NULL){
    printf("%s\n",row[0]);
    sprintf(buffer,"%s",row[1]);
    value1 = atof(buffer);
    Myarray[i]=value1;
    i++;
    }
    return 0;
    }
    }

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: deprecated conversion from string const to char*

    Quote Originally Posted by rpar View Post
    "Querydb" function works fine in main even though i have compile error.
    If there was a compiler error, then the program would never have been created.

    First, please indent your code. It still is almost impossible to read.
    but if the same function is called from the timer thread then the program crashes with seg fault.(with compiler spitting out above warning but no error)
    But you did not identify exactly which line is causing the problem.
    I know the issue and it is the mysql string i'm using with the function in sqlfunction.cpp file
    Code:
    bool Querydb(char * query, double Myarray)
    That is just a function prototype -- it is not the actual executable line that is invoked to cause the error. Please state which line, when you run the program, causes the crash.

    Secondly, those prototypes take char*, and by convention, functions that take char* can potentially change the characters being pointed to. But if you take a look at how you're calling these functions, you're passing string-literals, and string literals cannot be modified.
    Code:
    extern bool Opendb(char *pc, char *user, char *pass, char *db);
    If a programmer were using the API for the first time, they would see the prototype as char*, meaning whatever you pass must be passed as a modifiable buffer. The programmer would then pass a modifiable array of char:
    Code:
    // THIS IS AN EXAMPLE
    char pcStr[] = "whatever";
    Opendb( pcStr, ... );
    //..
    // NOT THIS!!
    char *pcStr = "whatever";
    Opendb( pcStr, ... );
    So either you pass true arrays of char, or if you wrote the Opendb function, change the parameters to const char * to indicate that the function doesn't change the string in any way.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: deprecated conversion from string const to char*

    Another thing -- does the library documentation state that exceptions are thrown on error? If so, then your code must be surrounded by try/catch blocks.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Mar 2011
    Posts
    3

    Re: deprecated conversion from string const to char*

    If a programmer were using the API for the first time, they would see the prototype as char*, meaning whatever you pass must be passed as a modifiable buffer. The programmer would then pass a modifiable array of char:
    Code:

    // THIS IS AN EXAMPLE
    char pcStr[] = "whatever";
    Opendb( pcStr, ... );
    //..
    // NOT THIS!!
    char *pcStr = "whatever";
    Opendb( pcStr, ... );
    this really helped to resolved issue half. no more comilor warnings but still the prog crashes so trying to resolve it from mysql side.

    thanks much :-))

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured