Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

there are some proplems with fio_str_tmp function #82

Closed
immortalChensm opened this issue Dec 16, 2019 · 3 comments
Closed

there are some proplems with fio_str_tmp function #82

immortalChensm opened this issue Dec 16, 2019 · 3 comments
Labels
API design Discussions about API choices

Comments

@immortalChensm
Copy link

this is my source code

typedef struct table_data{
    char **fields;
    char **value;
}table_data;
char *fields[] = {"id","name","age"};
    char *value[] = {"1","jack","100"};
    insert("users",(table_data){.fields=fields,.value=value}); 
int insert(char *table,table_data data)
{
    int i=0;
    FIOBJ fields = fiobj_str_tmp();
    FIOBJ val = fiobj_str_tmp();
    if (table){
        while (data.fields[i]){
            fiobj_str_write(fields,"`",1);
            fiobj_str_write(fields,data.fields[i],strlen(data.fields[i]));
            fiobj_str_write(fields,"`",1);
            fiobj_str_write(fields,",",1);

            fiobj_str_write(val,"'",1);
            fiobj_str_write(val,data.value[i],strlen(data.value[i]));
            fiobj_str_write(val,"'",1);
            fiobj_str_write(val,",",1);
            i++;
        }
        printf("fields=%s\n",fiobj_obj2cstr(fields).data);
        printf("value=%s\n",fiobj_obj2cstr(val).data);

    }
}

run it shows

fields=`id`,'1',`name`,'jack',`age`,'100',
value=`id`,'1',`name`,'jack',`age`,'100',
@boazsegev
Copy link
Owner

Hi @immortalChensm ,

Thank you for opening this issue.

This is a design issue, solved in the 0.8.x branch, where the fiobj_str_tmp function is deprecated in favor of a macro based solution that allows multiple temporary strings to be placed on the stack.

Under the current design, the fiobj_str_tmp returns a thread-safe static variable. It is limited to a single temporary string per thread.

As it stands, there is no way to fix the fiobj_str_tmp function.

I agree with you that this design is suboptimal and so, it was deprecated.

However, allocating a string with fiobj_str_new should be fast enough... even if you need to remember to free the string with fiobj_free.

I hope this helps.

Kindly,
Bo.

@boazsegev boazsegev added the API design Discussions about API choices label Dec 16, 2019
@boazsegev
Copy link
Owner

P.S. - I closed this issue because it's solved in 0.8.0 (pre-beta).

Thank you again for opening the issue.
Bo.

@immortalChensm
Copy link
Author

Thank you very much for your reply. I see

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API design Discussions about API choices
Projects
None yet
Development

No branches or pull requests

2 participants