By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,175 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

buffer options

P: n/a
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 !!


Jul 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a

"Allen" <al************@att.SPAM.net> wrote in message
news:Fo*********************@bgtnsc04-news.ops.worldnet.att.net...
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
Jul 19 '05 #2

P: n/a

Allen <al************@att.SPAM.net> wrote in message
news:Fo*********************@bgtnsc04-news.ops.worldnet.att.net...
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

Jul 19 '05 #3

P: n/a
"Allen" <al************@att.SPAM.net> wrote in message
news:9T*********************@bgtnsc05-news.ops.worldnet.att.net...
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
Jul 19 '05 #4

P: n/a

"Allen" <al************@att.SPAM.net> wrote in message
news:9T*********************@bgtnsc05-news.ops.worldnet.att.net...
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
Jul 19 '05 #5

P: n/a
>
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
Jul 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.