Connecting Tech Pros Worldwide Forums | Help | Site Map

buffer options

Allen
Guest
 
Posts: n/a
#1: Jul 19 '05
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 !!





John Harrison
Guest
 
Posts: n/a
#2: Jul 19 '05

re: buffer options



"Allen" <allen-terri-ng@att.SPAM.net> wrote in message
news:FouZa.95078$0v4.6535548@bgtnsc04-news.ops.worldnet.att.net...[color=blue]
> 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[/color]
thinking[color=blue]
> something along the lines of:
>
> MyStruct mystruct[1];
>
> mystruct = (MyStruct*)LocalAlloc(sizeof(MyStruct)*MAX_BUF);[/color]
//MS-specific[color=blue]
> I know, sorry[/color]

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

MyStruct* mystruct;
mystruct = (MyStruct*)LocalAlloc(sizeof(MyStruct)*MAX_BUF);
[color=blue]
>
> But this still limits me to MAX_BUF items in the buffer at once. I[/color]
was[color=blue]
> 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?[/color]

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


Mike Wahler
Guest
 
Posts: n/a
#3: Jul 19 '05

re: buffer options



Allen <allen-terri-ng@att.SPAM.net> wrote in message
news:FouZa.95078$0v4.6535548@bgtnsc04-news.ops.worldnet.att.net...[color=blue]
> 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[/color]
thinking[color=blue]
> something along the lines of:
>
> MyStruct mystruct[1];
>
> mystruct = (MyStruct*)LocalAlloc(sizeof(MyStruct)*MAX_BUF);[/color]
file://MS-specific[color=blue]
> I know, sorry
>
> But this still limits me to MAX_BUF items in the buffer at once. I[/color]
was[color=blue]
> 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?[/color]

std::vector<Mystruct> buffer;

-Mike



Samuele Armondi
Guest
 
Posts: n/a
#4: Jul 19 '05

re: buffer options


"Allen" <allen-terri-ng@att.SPAM.net> wrote in message
news:9TuZa.92754$3o3.6463181@bgtnsc05-news.ops.worldnet.att.net...[color=blue]
> 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?
>[/color]
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


John Harrison
Guest
 
Posts: n/a
#5: Jul 19 '05

re: buffer options



"Allen" <allen-terri-ng@att.SPAM.net> wrote in message
news:9TuZa.92754$3o3.6463181@bgtnsc05-news.ops.worldnet.att.net...[color=blue]
> 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
>[/color]

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


John Harrison
Guest
 
Posts: n/a
#6: Jul 19 '05

re: buffer options


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

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

john


Closed Thread