How to allocate one buffer for all formatted SQL queries?
hi,
i'm new arduino , c# have started play around mysql connector arduino library (by dr. charles a. bell). it's quite fun , interesting, have question rather basic. here goes:
in 1 of code examples dr. charles bell writes in comments sprintf uses twice memory , alternative option "to allocate 1 buffer formatted queries or allocate memory , free when you're done".
this might basic question, hope kind explain how this? or point me in direction examples.
here link bell's full example otherwise here specific comment:
i'm new arduino , c# have started play around mysql connector arduino library (by dr. charles a. bell). it's quite fun , interesting, have question rather basic. here goes:
in 1 of code examples dr. charles bell writes in comments sprintf uses twice memory , alternative option "to allocate 1 buffer formatted queries or allocate memory , free when you're done".
this might basic question, hope kind explain how this? or point me in direction examples.
here link bell's full example otherwise here specific comment:
code: [select]
serial.println("> running select dynamically supplied parameter");
// initiate query class instance
mysql_cursor *cur_mem = new mysql_cursor(&conn);
// supply parameter query
// here use query_pop format string , query the
// destination. uses twice memory option be
// allocate 1 buffer formatted queries or allocate the
// memory needed (just make sure allocate enough memory and
// free when you're done!).
sprintf(query, query_pop, 9000000);
// execute query
cur_mem->execute(query);
hi,sprintf() used insert variable values text string. in code example, sprintf(query,query_pop,9000000); taking parameter const char query_pop , inserting value 9,000,000 somewhere. the resultant 0 terminated char array returned in query.
i'm new arduino , c# have started play around mysql connector arduino library (by dr. charles a. bell). it's quite fun , interesting, have question rather basic. here goes:
in 1 of code examples dr. charles bell writes in comments sprintf uses twice memory , alternative option "to allocate 1 buffer formatted queries or allocate memory , free when you're done".
this might basic question, hope kind explain how this? or point me in direction examples.
here link bell's full example otherwise here specific comment:code: [select]serial.println("> running select dynamically supplied parameter");
// initiate query class instance
mysql_cursor *cur_mem = new mysql_cursor(&conn);
// supply parameter query
// here use query_pop format string , query the
// destination. uses twice memory option be
// allocate 1 buffer formatted queries or allocate the
// memory needed (just make sure allocate enough memory and
// free when you're done!).
sprintf(query, query_pop, 9000000);
// execute query
cur_mem->execute(query);
so, think saying there 2 similar copies of query_pop in ram @ same time, , query buffer allocated, used once.
personally instead of using sprintf() i use sprintf_p(). the difference format string stored in flash(program memory) instead of ram.
code: [select]
char buf[100]; // character buffer receive formatted string
sprintf_p(buf,pstr("this big number %ld"),9000000l);
serial.println(buf);
with avr chips, gcc complier uses ram store copy of string literal. so if used:
code: [select]
char buf[100]; // character buffer receive formatted string
sprintf(buf,"this big number %ld",9000000l);
serial.println(buf);
the string literal "this big number %ld" exists in 2 memory locations, in flash, in permanent ram location it's address can passed sprintf() function. the harvard architecture of avr chips have 2 memory spaces, ram , flash. all data assumed in ram, literal string must reside in ram, through little work using pstr() (program memory, flash) macro, direct address in flash of literal can known. using direct flash memory address, functions know how access it, constant data can stored in flash without wasting ram.
[/code]
chuck.
Arduino Forum > Using Arduino > Networking, Protocols, and Devices (Moderator: fabioc84) > How to allocate one buffer for all formatted SQL queries?
arduino
Comments
Post a Comment