472,958 Members | 2,592 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

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 11517
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...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.