buffer options

A

Allen

Hi all,

I want to make a buffer of structs but I don't want to have a static
size; I want it to be as dynamic as possible. Ordinarily, I would use a
list, but in this case I really want to avoid that. Right now, I'm thinking
something along the lines of:

MyStruct mystruct[1];

mystruct = (MyStruct*)LocalAlloc(sizeof(MyStruct)*MAX_BUF); //MS-specific
I know, sorry

But this still limits me to MAX_BUF items in the buffer at once. I was
also thinking about some kind of conditional re-sizing scheme but couldn't
think of a non-messy or fast enough solution along those lines.

Anyone have any suggestions?
--

Best wishes,
Allen

No SPAM in my email !!
 
J

John Harrison

Allen said:
Hi all,

I want to make a buffer of structs but I don't want to have a static
size; I want it to be as dynamic as possible. Ordinarily, I would use a
list, but in this case I really want to avoid that. Right now, I'm thinking
something along the lines of:

MyStruct mystruct[1];

mystruct = (MyStruct*)LocalAlloc(sizeof(MyStruct)*MAX_BUF); //MS-specific
I know, sorry

This is not legal code, even with MS compiler. I think you meant

MyStruct* mystruct;
mystruct = (MyStruct*)LocalAlloc(sizeof(MyStruct)*MAX_BUF);
But this still limits me to MAX_BUF items in the buffer at once. I was
also thinking about some kind of conditional re-sizing scheme but couldn't
think of a non-messy or fast enough solution along those lines.

Anyone have any suggestions?

Yes use a vector. Your situation is exactly what vector was designed for.

#include <vector>

std::vector<MyStruct> mystruct;

Add items to the vector using push_back (for instance)

MyStruct a_struct;
....
mystruct.push_back(a_struct);

the vector will grow dynamically. Or just call resize, if you have a
particular size in mind

mystruct.resize(100);

john
 
M

Mike Wahler

Allen said:
Hi all,

I want to make a buffer of structs but I don't want to have a static
size; I want it to be as dynamic as possible. Ordinarily, I would use a
list, but in this case I really want to avoid that. Right now, I'm thinking
something along the lines of:

MyStruct mystruct[1];

mystruct = (MyStruct*)LocalAlloc(sizeof(MyStruct)*MAX_BUF); file://MS-specific
I know, sorry

But this still limits me to MAX_BUF items in the buffer at once. I was
also thinking about some kind of conditional re-sizing scheme but couldn't
think of a non-messy or fast enough solution along those lines.

Anyone have any suggestions?

std::vector<Mystruct> buffer;

-Mike
 
S

Samuele Armondi

Allen said:
Hi John, Samuele,

Thanks for the help. I've heard of vectors before but have never
bothered reading up on them. I would do so now, but my compiler is rather
old--I'm using MSVC++ 4.0--and doesn't seem to have them. I don't suppose
it's a simple matter of getting a copy of the .h and .lib files is it?
Allen,
I do not think getting the headers would be enough, since you will need a
compiler that supports templates. Maybe it's time for a compiler update...
if you are on a budget, it is worth checking out dev-cpp, it is free to
download and it supports most of the features you will need. Just search for
it using google.
HTH,
S. Armondi
 
J

John Harrison

Allen said:
Hi John, Samuele,

Thanks for the help. I've heard of vectors before but have never
bothered reading up on them. I would do so now, but my compiler is rather
old--I'm using MSVC++ 4.0--and doesn't seem to have them. I don't suppose
it's a simple matter of getting a copy of the .h and .lib files is it?

Thanks and best wishes,
Allen

Try here http://www.stlport.org, not sure if it compiles for VC++ 4 though.
That is a really old compiler and the STL (of which vector is a part) makes
heavy use of templates. An old compiler might struggle.

john
 
J

John Harrison

Try here http://www.stlport.org, not sure if it compiles for VC++ 4 though.
That is a really old compiler and the STL (of which vector is a part) makes
heavy use of templates. An old compiler might struggle.

Just checked and it claims to support VC++ 4.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

C/C++ question about dynamic "static struct" 143
Mixing C++ and C 0
C struct to Python 0
Open-source alternative to mainwp 0
Mixing C and C++ 8
Ring Buffer & templates 8
Buffer 3
Best structure for a "data buffer". 4

Members online

Forum statistics

Threads
474,139
Messages
2,570,805
Members
47,351
Latest member
LolaD32479

Latest Threads

Top