After you leave myfunc2() you don't have valid access to the string I think
the c_str() method is just a fixed up pointer to the buffer (again, depends
on implementation), and when you leave myfunc2() you really don't have
access to that data and it can be trounced by other functions calls or what
ever allocates to the now released spaces that 'str' had when it goes out of
scope. You are just probabally getting lucky that nothing messes with the
memory until the exec goes to town on it.


char someBuffer[1025];

char* myfunc2(char *buff)
{

char* prefix = "insert into sdb
(id,resclass,type,manufacturer,method,ownership,precision,minrange,maxrange,
capacity)";

        char* str1 = " values (";
        char* suffix = ")";

        sprintf(buff,"%s values (%d, ...) %s", prefix, id, ... ,suffix);

        return(buff);
}

Just make sure your buffer is larger then would ever be or you will die. Or
you can just do something like this at the end of your old function -
        char *ret_ptr;

        ret_ptr = new char[Str.Length() + 1]; // or what ever gets you the 
lenght +
term

        strcpy(ret_ptr, Str.c_str());

        return(ret_ptr);

Rember now you have allocated memory to ret_ptr and later on you will have
to delete it.

By the way C++ strings are slow :-) Hope I got it all right, but you have
the idea...

Sandy

}-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
Sent: Tuesday, December 28, 2004 1:26 PM
To: sqlite-users@sqlite.org
Subject: RE: [sqlite] Running out of memory



Sandy,
Thanks for the help. I used string and then kept

char* myfunc2()
{
char* prefix = "insert into sdb
(id,resclass,type,manufacturer,method,ownership,precision,minrange,maxrange,
capacity)";

char* str1 = " values (";
char* suffix = ")";
string str;

  str += prefix;
  ...
  I filled in the variables here
  ...
  str += suffix;

 return(str.c_str());

}

char* query = myfunc2();

then I passed this query to the function we are talking about.

BTW, both query and newquery look exactly same after I print them before
the exec funciton call.


On Tue, 28 Dec 2004, Sandy Ganz wrote:

> The strcmp might be misleading you, depending on the implementation if a
> parameter is actually NULL it can return 0. But the easiest thing would be
> to print the queries right before you call the exec function and see what
it
> shows. Also how did you allocate the query value that is passed in?
>
> Sandy
>
> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
> Sent: Tuesday, December 28, 2004 12:43 PM
> To: sqlite-users@sqlite.org
> Subject: Re: [sqlite] Running out of memory
>
>
>
> Hi All,
>
> To give some more information
>
> void myfunction(char* query)
> {
>
>   printf(" Query is: %s \n",query);
>
> char* newquery = "insert into sdb
>
(id,resclass,type,manufacturer,method,ownership,precision,minrange,maxrange,
> capacity)
> values (43,'HARDWARE','MONITOR','GE','NA','XYZ',0,0,4,2)"
>
>  int cmpval = strcmp(query,newquery);
>
>   if (cmpval == 0)
>      printf("strings match \n");
>  else
>    printf("Strings dont match\n")
>
> printf("------------------------------------------\n");
>   rc = sqlite3_exec(db,query, NULL, 0, &zErrMsg);
>
>    if( rc!=SQLITE_OK )
>     {
>       fprintf(stderr, "SQL error while registering: %s\n",zErrMsg);
>     }
>
> }
>
> Now if I use sqlite3_exec(db,newquery, NULL, 0, &zErrMsg) it works fine.
> But if I use sqlite3_exec(db,query, NULL, 0, &zErrMsg); it gives me the
> unrecognized token "" error.
>
> Eventhough my strcmp function tells me that the two strings match, but
> sql_exec does not think so. I am bit confused as to why the same
> hard-coded string works but not the other one.
>
> Any help on this would be great.
>
> Regards,
> --Sameer.
>
> On Tue, 28 Dec 2004, D. Richard Hipp wrote:
>
> > Roger Binns wrote:
> >  > [M]ost software does not play well with running out of memory...
> >  >
> >
> > FWIW:  If a malloc() ever fails with SQLite, it will return
> > SQLITE_NOMEM.  It also sets a flag and will forever after
> > continue to return SQLITE_NOMEM even if you free up a bunch
> > of memory so that malloc() would start working again.  There
> > is no way to reset this condition.  Once a malloc() fails,
> > the party is over and you might as well shut down the process.
> >
> > If malloc() fails, SQLite might also leak memory.  (Ironic,
> > isn't it?)
> >
> > The only guarantees that SQLite makes after a malloc failure
> > is that it will not abort() or panic() or segfault.  Basically,
> > it just gives you a chance to shutdown gracefully.  This might
> > not sound like much, but as Roger points out, it is a lot
> > more than most other software packages offer.
> >
> >
>
>

Reply via email to