473,467 Members | 1,472 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How is "static buffers" defined?


Hi,
When i use some standard library functions and fields,which return
char* type(like ctime in time.h, optarg in getopt.h)and do not have to
be freed after calling,i always worry about memory leaking(thoug i
konw i just donot have to).Then i look inside in time.h
file(mingw) ,and i found notes say"These functions write to and return
pointers to static buffers that may be overwritten by other function
calls".So how is the"static buffers" defined?What about its size?Is
memory leaking possible when returning long enough string?Is it a part
of ANSI c?

Thanks.
Oct 20 '08 #1
2 11665
ch********@gmail.com wrote:
Hi,
When i use some standard library functions and fields,which return
char* type(like ctime in time.h, optarg in getopt.h)and do not have to
be freed after calling,i always worry about memory leaking(thoug i
konw i just donot have to).Then i look inside in time.h
file(mingw) ,and i found notes say"These functions write to and return
pointers to static buffers that may be overwritten by other function
calls".So how is the"static buffers" defined?What about its size?Is
memory leaking possible when returning long enough string?Is it a part
of ANSI c?
<nitI think your space bar is malfunctioning; there's a lot of
mandatory spaces missing from the above message. It makes your message
hard to read.</nit>

If the function is defined in C, then it probably uses the same methods
any other C function would use to define a static buffer:

static char file_scope_buffer[SOME_SIZE];

or

char *myfunc()
{
static char block_scope_buffer[SOME_SIZE];
/* rest of myfunc */
return block_scope_buffer;
}

Note: the 'static' on file_scope_buffer was not needed to give the
buffer static storage duration; it already had that just by reason of
being declared with file scope. Instead, it gives the buffer internal
linkage, which is what I would choose for this kind of usage.

If ctime() is defined in some other way, such as assembly, some entirely
different method of defining the buffer would be used.

The only thing you need to know about the size of the buffer is that it
must be big enough to hold the string that you've requested. You should
never write to these buffers, so it should not matter to you how much
bigger they are than the string.

There is one anomalous case: asctime(). It is the only function in the
standard library whose behavior is defined in terms of a reference
implementation. However, the actual code that implements asctime() isn't
required to be the same as the reference implementation, it's just
supposed to produce the same behavior, when that behavior is defined;
when the behavior is undefined, the standard imposes no requirements on
asctime().

That reference implementation gives a length of 26. The behavior of the
reference implementation is undefined if the arguments given to
asctime() would cause that buffer to overrun. This is possible for
values of timeptr->year 8099, among other possibilities. This is a
prime example of why reference implementations should not be used to
define the behavior; it would have been much clearer to explicitly state
that timeptr->year must be <= 8099.

Since the behavior of the function is undefined in those cases, it's
permissible for it to be the same as the behavior of a function that
uses some method of preventing the buffer overrun, such as making the
buffer longer. So even for asctime(), you don't really know how big the
buffer is.

Memory leakage is only an issue for memory with dynamic storage
duration. Since this memory has static storage duration, memory leakage
from calling these functions is something you should never have to worry
about.
Oct 20 '08 #2
>When i use some standard library functions and fields,which return
>char* type(like ctime in time.h, optarg in getopt.h)and do not have to
be freed after calling,i always worry about memory leaking(thoug i
konw i just donot have to).
You can't free() that, so don't try.

The string pointed at by optarg after a return from getopt() is a
pointer into the middle of data *YOU* supplied: the same happens
for strchr(), strrchr(), and similar functions.
>Then i look inside in time.h
file(mingw) ,and i found notes say"These functions write to and return
pointers to static buffers that may be overwritten by other function
calls".So how is the"static buffers" defined?
It's a variable that is declared static (function or file scope),
and "buffer" sort of implies a character array, and the type of the
pointer used to point into it also implies the type of the buffer.
For ctime(), the buffer is a character array. In the case of
localtime(), however, the buffer is a struct tm.
>What about its size?Is
If a function chooses this method of returning something, it must
think that the buffer is sufficiently big for all results. ctime(),
for example, is supposed to return a string the same length all the
time (26 bytes, including the \0 terminator), although that function
does have a serious Y10K problem. localtime() returns a pointer to
a struct, and the struct is a fixed size.
>memory leaking possible when returning long enough string?
When talking about *LONG* strings, think *BUFFER OVERFLOW*, not
memory leak. Buffer overflows are much more serious than memory
leaks.
>Is it a part
of ANSI c?
Is what a part of ANSI C? ctime()? Yes. Buffer overflow? ANSI
C doesn't define that, but you can certainly make it happen with
ANSI C. Memory leak? ANSI C doesn't define that, but you can
certainly make it happen with ANSI C.

Oct 20 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

29
by: Alexander Mahr | last post by:
Dear Newsgroup, I'm somehow confused with the usage of the static keyword. I can see two function of the keyword static in conjunction with a data member of a class. 1. The data member...
3
by: Ajax Chelsea | last post by:
can not the "static const int" be replaced by "static enum" anywhere? is it necessary that define special initialization syntax for "static const int"?
12
by: cppaddict | last post by:
Hi, I know that it is illegal in C++ to have a static pure virtual method, but it seems something like this would be useful when the following 2 conditions hold: 1. You know that every one...
2
by: Lu | last post by:
Hello, I am wondering how to protect a global variable in a header file from external access. So I googled and found: "The keyword 'static' has two different uses, depending on whether it is...
9
by: Neil Kiser | last post by:
I'm trying to understand what defining a class as 'static' does for me. Here's an example, because maybe I am thinking about this all wrong: My app will allows the user to control the fonts...
11
by: comp.lang.php | last post by:
function blah($item) { if (!isset($baseDir)) { static $baseDir = ''; $baseDir = $item; print_r("baseDir = $baseDir\n"); } $dirID = opendir($item); while (($fyl = readdir($dirID)) !== false)...
3
by: Steve Folly | last post by:
Hi, I had a problem in my code recently which turned out to be the 'the "static initialization order fiasco"' problem (<http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12>) The FAQ...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.