Re: Simple C containers, std::vector analog

From: Ross A. Finlayson (raf_at_tiki-lounge.com)
Date: 03/07/05


Date: 7 Mar 2005 08:48:44 -0800

Stop intimidating me!

Yeah, there are bugs in that. The macro has a block which declares its
own variable, and there is a missing closing brace on the block. All
those ##'s are not needed.

Today the compiler will readily tell you exactly where the bug is, and
is that close to correcting it, steps toward automatic computing or
rather automatic programming. Thirty years ago, the compiler will
readily give you the object file to execute it on the thirty million
dollar supercomputing mainframe. Today, the computing power of thiry
years ago's thirty million dollar supercomputing mainframe is fit into
an MP3 player that plays MP3 files into headphones and straps to your
arm. Thirty years ago it was 1975 and computers ran in Kilohertz
(kilohertz, kiloHertz, KiloHertz). Today they run in Gigahertz, as
Moore approaches physical silicon limits, leading to reversible
processors that run cold on a trickle of picoamps. Having no truck
with school, the computer still runs a million times faster.

The realloc function doesn't always deallocate the previous block of
memory and copy the contents, er... sometimes there is still room on
the heap contiguous to the originally allocated block. It still
returns NULL on failure. It's at the implementation's discretion how
to do that. I'm concerned that some C implementations don't support
nested blocks.

It's probably safer to use malloc(sizeof(word_t)) in the initializer
than malloc(0), or rather:

#define vector_init(vector, T) vector.data =
s_cast(T)(malloc(sizeof(T))

and some implementations do choke on realloc'ing a null pointer.

I think RAII is a decent concept, a good concept, but like everything
else, good in moderation.

(Some things are good in lots of moderation.)

That C vector analog is kind of a castrated C++ standard library
container, that's because in this case for its use there is no need for
many of the functions except those listed. It can return begin and end
pointers for use with std::algorithm, obviously enough implemented with
std::vector it does. There's no implementation of push_front or
pop_front, but it could pop_front by incrementing the pointer, storing
the original pointer for allocation, and stuff. There no insertion
into the array, only push_back, or vector push_back(vector, value).

Basically the idea here is to design the functions as good ISO C, and
ANSI C, yet have them essentially be template functions for use with
C++, with old or new style I/O and containers.

#ifdef __cplusplus
template f <voidpT> void f(voidpT buffer){
#elif
void f(void* buffer){
#endif

For example, they work on buffers, and these vector type dealies, but
again I am working on simple software where the full complexity of C++
is underappreciated. The #elif thing there is not universally
supported. So, I'm trying to figure out how to work in basically
istream and ostream, but only as they look like a file-mapped memory
buffer, just readT and writeT, * and ++. I'm out of my depth here.

Thank you,

Ross F.

--
"It's the smallest infinitesimal, Russell,
there are smaller infinitesimals."


Relevant Pages

  • [net-next PATCH 4/5] igb: Add support for enabling VFs to PF driver.
    ... pointer to the hardware struct ... under the terms and conditions of the GNU General Public License, ... * @msg: The message buffer ... * returns SUCCESS if it successfully copied message into the buffer ...
    (Linux-Kernel)
  • Re: The coming death of all RISC chips.
    ... different area of memory than the "stack", ... Any arbitrarily indirect pointer to code that can be ... overwritten by a buffer overflow is a potential hole (and an actual ... ways to exploit buffer overflows. ...
    (comp.arch)
  • Re: some unanswered questions on C
    ... A pointer variable that's never been given a value. ... you don't know what memory you're modifying. ... >what i want to ask is that when i declare my buffer for fgets as ... "char *buffer" creates a pointer, ...
    (comp.unix.programmer)
  • [patch] x86, ptrace: PEBS support
    ... BTS and PEBS recording. ... * - buffer overflow handling ... (interrupt occurs when write pointer passes interrupt pointer) ... * guarding context and buffer memory allocation. ...
    (Linux-Kernel)
  • Algorithm to compute critical values of Durbin-Watson statistic?
    ... Can anyone give me a pointer to a site that provides an algorithm for ... computing the critical values of the Durbin-Watson statistic? ... http://www.LogRover.com (Web statistics analysis) ...
    (sci.stat.math)