Subject | Place for VARCHAR (was:API: isc_detach_database, release of resources.) |
---|---|
Author | Dimitry Sibiryakov |
Post date | 2003-01-29T06:25:22Z |
On 28 Jan 2003 at 15:36, nitaligavino Dan.Crea@... wrote:
VARCHARs (unlike CHARs) don't pad values with spaces. Because of
this a "real length" of the content must be stored somewhere. IB
allows VARCHARs up to 32K (let alone multibyte charsets). It shows
that signed 16bit integer is used as length storage. You also may
look into ibase.h for paramvary and found that vary_length is
unsigned. I don't know how to explain this fact, but anyway: length
of length is two bytes.
So, if you declare a field of type VARCHAR(40), your application
must allocate enough space to keep up to 40 chars plus two bytes for
length. At the same time XSQLVAR.sqllen for such field = 40. This
leads us to the code below:
case SQL_VARYING:
var->sqldata = new char[var->sqllen+2];
If you allocate only 40 bytes, everything will be allright until
anybody put a value with the length 39 chars into this field. AV is
the lesser evil that you can get.
SY, Dimitry Sibiryakov.
>In your response you mentioned a 2 byte padding, if you will, for realI'm not sure if my English is enough for this, but I'll try...
>string length. I'm curious about this, can you explain this in more
>detail?
VARCHARs (unlike CHARs) don't pad values with spaces. Because of
this a "real length" of the content must be stored somewhere. IB
allows VARCHARs up to 32K (let alone multibyte charsets). It shows
that signed 16bit integer is used as length storage. You also may
look into ibase.h for paramvary and found that vary_length is
unsigned. I don't know how to explain this fact, but anyway: length
of length is two bytes.
So, if you declare a field of type VARCHAR(40), your application
must allocate enough space to keep up to 40 chars plus two bytes for
length. At the same time XSQLVAR.sqllen for such field = 40. This
leads us to the code below:
case SQL_VARYING:
var->sqldata = new char[var->sqllen+2];
If you allocate only 40 bytes, everything will be allright until
anybody put a value with the length 39 chars into this field. AV is
the lesser evil that you can get.
SY, Dimitry Sibiryakov.