braces usage without function name [duplicate] - c

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Practical use of extra braces in C
Unnecessary curly braces in C++?
What is the usage of the braces, for example as shown below:
int var;
{
some coding...
...
}
there is no function name before the braces, nor typedef, etc.
Updated:
i found this code in gwan sqlite.c example, http://gwan.com/source/sqlite.c
i partially quote it below:
...some coding
sqlite3_busy_timeout(db, 2 * 1000); // limit the joy
// -------------------------------------------------------------------------
// create the db schema and add records
// -------------------------------------------------------------------------
{ //<-- here is the starting brace
static char *TableDef[]=
{
"CREATE TABLE toons (id int primary key,"
"stamp int default current_timestamp,"
"rate int,"
"name text not null collate nocase unique,"
"photo blob);",
// you can add other SQL statements here, to add tables or records
NULL
};
sqlite3_exec(db, "BEGIN EXCLUSIVE", 0, 0, 0);
int i = 0;
do
{
if(sql_Exec(argv, db, TableDef[i]))
{
sqlite3_close(db);
return 503;
}
}
while(TableDef[++i]);
// add some records to the newly created table
sql_Exec(argv, db,
"INSERT INTO toons(rate,name) VALUES(4,'Tom'); "
"INSERT INTO toons(rate,name) VALUES(2,'Jerry'); "
"INSERT INTO toons(rate,name) VALUES(6,'Bugs Bunny'); "
"INSERT INTO toons(rate,name) VALUES(4,'Elmer Fudd'); "
"INSERT INTO toons(rate,name) VALUES(5,'Road Runner'); "
"INSERT INTO toons(rate,name) VALUES(9,'Coyote');");
sqlite3_exec(db, "COMMIT", 0, 0, 0);
// not really useful, just to illustrate how to use it
xbuf_cat(reply, "<br><h2>SELECT COUNT(*) FROM toons (HTML Format):</h2>");
sql_Query(argv, db, reply, &fmt_html, "SELECT COUNT(*) FROM toons;", 0);
} //<-- here is the ending brace
...some coding

Statements can be grouped into blocks, and the braces indicate the start and end of a block. A function body is a block. Blocks introduce a new variable scope that starts at the opening brace and ends at the closing brace.
What you've got there is a block.

braces usage without function name
I guess that rather than what it is the answer can focus on why this is done.
For example, you can re-use a variable name to do something else, using a different type (the SQLite example does this to keep using the same terminology while restarting from scratch instead of risking naming conflicts):
{
int i = 2;
...
{
int i = 10; // this is a different variable
// the old value of 'i' will be restored once this block is exited.
}
}
{
void *i = alloca(16 * 1024); // this memory will be freed automatically
... // when the block will be exited
}
But this also lets you free memory allocated on the stack with alloca(), like done above.
This is also a clear indication for the compiler that the variables defined in a block are no longer needed (this can be useful to make sure that CPU registers are freed for other tasks).
As you see, defining a scope can have cosmetic and technical uses. Both of which are useful.

To create a new scope for local variables within {}
for example in C:
fun(){
int i; // i -1
{
int i; // i -2 its new variable
}
}

Related

How to get COUNT(*) value from sqlite3_exec()?

In my C program I have to check if count of a table in database is one or zero and to do that i am executing query as follows:
char *sql = "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name=family;";
int table_count = sqlite3_exec(db, sql, 0, 0, &err_msg);
printf("\n%d\n", table_count);
I'm expecting table_count to be 1 as only one table exists with name family but printf outputs table_count as '21' which is incorrect. How can we get the COUNT(*) value from C/C++ API in C program the right/correct way?
After reading SQLite Documentation and following other kind implicit/explicit suggestions in the comments on the question, I have realized my mistakes in that code snippet quoted in the question.
Mistake 1:
I did not implement callback function to receive the result set after the SQL query gets executed. [Have implemented this callback: see checkTable_Callback in code below]
Mistake 2:
That output of '21' is actually the error code and as per the SQLite documentation that error code corresponds to SQLite_MISUSE Which was being generated, perhaps, because I was using a separate function to open my test database file and instance of that opened database, i assume, stayed inside that openDb function, and when i used another function checkTableCount from where i took that messy snippet to quote in my question, there db instance perhaps was null, hence 21. Experts can elaborate further if that's why i was receiving error code 21. Anyways, now i have fixed that function and made that openDb return an opened db instance (better word?) and now 21 error is gone. [see code below]
Here is fixed and 'adapted-for-my-case' code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "sqlite3.h" /* sqlite3.dll and sqlite3.h both reside in
my <program_source.c>'s folder */
static int checkTable_Callback(
void *unnecessary_here,
int number_of_columns_in_result_row, /* will always be 1 in this case */
char **value_of_count, /* will be either 0 or 1 in this case */
char **label_for_count) { /* will be COUNT(*) normally,
but modified via 'AS table_tablename' in this case*/
printf("COUNT(*) %s\t=>\t%s\n", label_for_count[0], value_of_count[0] );
return 0;
} // end of checkTable_Callback()
char * build_sql(const char *sql_partA, const char *sql_partB) {
size_t size = strlen(sql_partA) + strlen(sql_partB);
char *sql_final = malloc(size + 1);
strcpy(sql_final, sql_partA);
strcat(sql_final, sql_partB);
return sql_final; /* allocated memory to be freed at the end of calling function */
} // end of build_sql()
checkTableCount(sqlite3 *db, char *tablename) {
char *sql = build_sql(
build_sql(
build_sql(
build_sql(
"SELECT COUNT(*) AS table_",
tablename),
" FROM sqlite_master WHERE type='table' AND name='"),
tablename),
"';");
sqlite3_exec(db, sql, checkTable_Callback, 0, NULL);
/* error checking sacrificed for brevity of sample */
free(sql);
}// end of checkTableCount()
sqlite3 * openDb(char * db_name){
sqlite3 *db;
int result_code = sqlite3_open(db_name, &db);
if( result_code != 0 )
fprintf(stderr, "\tError: %s\n\n", sqlite3_errmsg(db));
return db;
} // end of openDb()
int main() {
sqlite3 * db = openDb("testing.db"); /* testing.db already has one table 'family'*/
checkTableCount(db, "family");
checkTableCount(db, "fam"); /* no such table exist */
sqlite3_close(db);
return 0;
} // end of main()
Now this quoted 'adapted-for-my-case' code rightly and correctly outputs the COUNT(*) as follows:
OUTPUT
COUNT(*) table_family => 1
COUNT(*) table_fam => 0
Note that I didn't bother to write a for-loop inside my callback function named checkTable_Callback to iterate through columns as shown in the official sample of callback function on this page because of the fact that our expected result row is certainly going to be only one containing only one column with label modified, via 'AS' clause, into 'table_tablename'. If not modified via 'AS clause', the returned column label would be 'COUNT(*)' in the result row.

Casting a Host Structure array into a table

I am trying to tune a piece of large code written in Pro*C, specifically a bottleneck loop and an UPDATE statement inside it. The for loop loops through a "Host Structure array" which may contain several thousand at times millions of records and the update executes too many times. The update could be executed more frequently throughout the program but that would need drastic change to the code and I am not in a liberty to make major changes.
So I have something like this
....
#define NULL_REF_NO 10
#define NULL_ERR 256
....
....
struct s_errors
{
char s_ref_id [NULL_REF_NO];
char s_ref_seq_no [NULL_REF_NO];
char s_err_msg [NULL_ERR];
};
....
....
struct s_errors *ps_errors = NULL;
....
....
/*The part below happens throughout the program to collect all errors*/
/*ls_ref_id, ls_ref_seq_no, and ls_err_msg are local variables of same data type. and i_curr_index is the array index variable*/
strcpy(ls_ref_id, ps_errors[i_curr_index].s_ref_id);
strcpy(ls_ref_seq_no, ps_errors[i_curr_index].s_ref_seq_no);
strcpy(ls_err_msg, ps_errors[i_curr_index].s_err_msg);
.....
/* At this point ps_error contains thousands or even millions of rows*/
/* The final part is to update all these rows back to the table like below*/
/* pl_err_count is a Global var which keeps track of the total number of records in the host structure array*/
int i_curr_index = 0;
char l_ref_id [NULL_REF_NO];
char l_ref_seq_no [NULL_REF_NO];
char l_err_msg [NULL_ERR];
for(i_curr_index = 0; i_curr_index < pl_err_count; i_curr_index++)
{
strcpy(l_ref_id, ps_errors[i_curr_index].s_ref_id);
strcpy(l_ref_seq_no, ps_errors[i_curr_index].s_ref_seq_no);
strcpy(l_err_msg, ps_errors[i_curr_index].s_err_msg);
EXEC SQL
UPDATE some_table
SET status = 'E',
error_message = :l_err_msg
WHERE ref_id = :l_ref_id
AND ref_seq_no = :l_ref_seq_no;
if (SQL_ERROR_FOUND)
{
sprintf(err_data, "Updation failed with sql errors ");
strcpy(table, "some_table");
WRITE_ERROR(SQLCODE,function,"",err_data);
return(FATAL);
}
}
The bottleneck is the for loop above (and it's the last step in the program) which loops too many times causing the program to run long. I was wondering if there is a way to CAST the Host Structure array ps_errors to an Oracle table type so that I can easily do bulk UPDATE or even do a MERGE with some parallel DML without having to loop through each and every record.
Extrapolating from some other code I have seen at work which does something sort of similar, you could do something like this instead of the for loop:
EXEC SQL for :pl_err_count
UPDATE some_table
SET status = 'E',
error_message = :ps_errors.s_err_msg
WHERE ref_id = :ps_errors.s_ref_id
AND ref_seq_no = :ps_errors.s_ref_seq_no;
This might rely on the content added to ps_errors being null terminated. Given the use of strcpy() in the existing code rather that strncpy() (or similar), I'm guessing they already are.
If s_err_msg could be null, you should also consider using indicator variables. eg.
error_message = :ps_errors.s_err_msg INDICATOR :indicator_variable
You could use an array update http://docs.oracle.com/cd/B28359_01/appdev.111/b28427/pc_08arr.htm#i1879 but you'll need to change your array of structs ps_errors to be a struct of arrays
E.g:
EXEC SQL
UPDATE some_table
SET status = 'E',
error_message = :ps_errors.s_err_msg
WHERE ref_id = :ps_errors.s_ref_id
AND ref_seq_no = :ps_errors.s_ref_seq_no;

What does the "in" keyword in Lua do?

I know it's usually combined with a for i,v in pairs() do loop (or ipairs, or even next) but what exactly is in?
Just to clarify, I know how to use it, I just don't know the logic behind it, how does it work/what does it return?
Lua's in is not a function or a variable. It's a part of the syntax for flow control. You can't replace it, you can't copy it, you can't even refer to it. It's rather like parentheses: a syntactic construct which has meaning for how a program is parsed, but which cannot be referred to within the program.
It doesn't "return" anything. It doesn't have "logic." It's more like a placeholder, or punctuation.
It doesn't do anything. It is syntax. It isn't a function. It isn't an opcode. It isn't a language feature. It is purely syntactical.
See the forlist function in lparser.c:
static void forlist (LexState *ls, TString *indexname) {
/* forlist -> NAME {,NAME} IN explist1 forbody */
FuncState *fs = ls->fs;
expdesc e;
int nvars = 0;
int line;
int base = fs->freereg;
/* create control variables */
new_localvarliteral(ls, "(for generator)", nvars++);
new_localvarliteral(ls, "(for state)", nvars++);
new_localvarliteral(ls, "(for control)", nvars++);
/* create declared variables */
new_localvar(ls, indexname, nvars++);
while (testnext(ls, ','))
new_localvar(ls, str_checkname(ls), nvars++);
checknext(ls, TK_IN);
line = ls->linenumber;
adjust_assign(ls, 3, explist1(ls, &e), &e);
luaK_checkstack(fs, 3); /* extra space to call generator */
forbody(ls, base, line, nvars - 3, 0);
}
Create the control variables.
Handle the local variables in the comma list.
Check that the next token is TK_IN which maps to luaX_tokens.

Exception on prepared statement reuse in wxSQLite3

I'm writing an application that uses the wxSQLite3 library, which is a wrapper around libsqlite3 for the wxWidgets cross-platform GUI programming framework. When attempting to reuse a prepared statement, a wxSQLite3Exception is thrown.
This example illustrates the problem:
#include <wx/string.h>
#include <wx/wxsqlite3.h>
int main() {
wxSQLite3Database::InitializeSQLite();
//create in-memory test database & populate it
wxSQLite3Database db;
db.Open(wxT(":memory:"));
db.ExecuteUpdate(wxT("CREATE TABLE SimpleTable (id INT PRIMARY KEY, val INT);"));
db.ExecuteUpdate(wxT("INSERT INTO SimpleTable VALUES (1, 10);"));
db.ExecuteUpdate(wxT("INSERT INTO SimpleTable VALUES (2, 20);"));
//create a prepared statement we can reuse
wxSQLite3Statement stmt;
stmt = db.PrepareStatement(wxT("SELECT * FROM SimpleTable WHERE id = ?;"));
//first use of statement (works)
stmt.Bind(1, 1);
wxSQLite3ResultSet r_set = stmt.ExecuteQuery();
if (r_set.NextRow()) {
wxPrintf(wxT("id: %i value: %i\n"), r_set.GetInt(wxT("id")), r_set.GetInt(wxT("val")));
}
r_set.Finalize();
//reset and reuse statement
stmt.Reset();
stmt.Bind(1, 2); //**EXCEPTION THROWN HERE**
wxSQLite3ResultSet r_set2 = stmt.ExecuteQuery();
if (r_set2.NextRow()) {
wxPrintf(wxT("id: %i value: %i\n"), r_set2.GetInt(wxT("id")), r_set2.GetInt(wxT("val")));
}
r_set2.Finalize();
//cleanup
stmt.Finalize();
db.Close();
wxSQLite3Database::ShutdownSQLite();
return 0;
}
The exception handling was removed for brevity, but the message from the exception is:
WXSQLITE_ERROR[1000]: Statement not accessible
I wrote roughly equivalent code in plain C using libsqlite3 and it ran without problem. Does anyone know what I'm doing wrong, or if this is a bug of some sort in wxSQLite3? Thank you in advance for your help!
In SQLite itself, a statement and a result set actually are the same object.
wxSQLite3 uses reference counting so that the statement is freed only when the last wxSQLite3Statement or wxSQLite3ResultSet object is freed.
This happens automatically in the respective destructors.
However, calling Finalize() explicitly bypasses the reference counting.
While not necessary, if you want to ensure that wxSQLite3ResultSet resources are freed correctly before the next statement execution, just destruct this object:
wxSQLite3Statement stmt = ...;
...
{
wxSQLite3ResultSet r_set = stmt.ExecuteQuery();
... r_set.NextRow() ...
// r_set destructed here
}
...
As long as you intend to reuse a prepared SQL statement, that is, to reset the statement and to bind new values to statement variables, you must not call method Finalize - neither on the prepared statement object itself nor on a result set retrieved from that statement.
As the method name, Finalize, suggests, the method finalizes the underlying SQLite statement object by calling sqlite3_finalize (quotation from the SQLite docs: "The sqlite3_finalize() function is called to delete a prepared statement.") After the underlying SQLite statement object has been deleted, it obviously can't be accessed anymore. Therefore you get the exception.
Usually you don't need to call method Finalize explicitly. wxSQLite3 takes care of finalizing statements through reference counting.

Behaviour of an hashtable using glib

I want to update the Volume to each #IP. So that for example after each 5 s I add V(i) of each #IP(i). Ok Now the hash table works fine it keeps updated after every T seconds. But the problem is that after a certain period I find that sometimes the same ip adress is repeated twice or even a lot of times within the hash table. So that when I close the process I find the same #IP repeated too many times. It is like there is a problem with the hash table or something like that.
Here is the code this funcion "update_hashTable()" is so important it is called every X seconds I suspect in fact a memory leak ... because I always call malloc for IP#.
but it keeps working ... any idea ???
int update_hashTable( ... ) {
u_int32_t *a;
... //declarations
struct pf_addr *as;
as = ks->addr[0];
a = (u_int32_t*)malloc(sizeof(u_int32_t));
*a = ntohl(as->addr32[0]);
sz = value; // no matter it is... an int for example
if (ReturnValue=(u_int32_t)g_hash_table_lookup(hashtable, a)) {
ReturnValue +=sz;
g_hash_table_insert(hashtable, (gpointer)a, gpointer)ReturnValue);
}
else {
g_hash_table_insert(hashtable, (gpointer)a, (gpointer)sz);
}
Indeed, you appear to have a memory leak, but this isn't your problem. The problem is that the true-path of your if statement simply reinserts a second value associated with the same key, which is not what you want.
The typical pattern for this check-if-exists and increment algorithm is usually something like
gpointer val = g_hash_table_lookup(hash_table, key);
if (val == NULL) {
val = g_malloc0(...);
g_hash_table_insert(hash_table, key, val);
}
*val = /* something */;
The important thing to take away from this is that once you have a pointer to the value associated with some key, you can simply modify it directly.
If this code will be executed by multiple threads in parallel, then the entire block should be protected by a mutex, perhaps with GMutex: http://developer.gnome.org/glib/2.28/glib-Threads.html
gcc provides atomic builtin intrinsics, say for atomically incrementing the value, see http://gcc.gnu.org/onlinedocs/gcc/Atomic-Builtins.html

Resources