Skip to main content
gamedev.net
🔒 Locked

Dynamic Buffer

Started by ARC inc Jul 9, 2009 at 9:55 PM 4 replies 1.2k views
Original Post
ARC inc
ARC inc
Ok, so I am working on a Dynamic Buffer to hold data for a project I am working on but I am unsure if the code I am working on will work properly.
char *dataBuffer = NULL;
dataBuffer = new char[header],[length],[string_of_words];
//I'll then do all the reading, or writing to the dataBuffer here.
delete[] buffer;
So basically could I use all the [header],[length],[string_of_words] or would I need to add these differently? Like on by one?
Aardvajk
Aardvajk
No, that won't work. I'm not sure it will even compile.

What exactly is the data you are trying to store in this buffer? I'm guessing here but assuming that header and string_of_words are text data and length is an int:

#include <string>#include <vector>#include <iostream>#include <sstream>class entry{public:    entry(const std::string &h,int l,const std::string &s)        : header(h),length(l),string_of_words(s) { }    std::string header;    int length;    std::string string_of_words;};void f(){    std::vector<entry> v;    v.push_back(entry("bleh",4,"one two three four"));    v.push_back(entry("woo",2,"apple orange"));    for(std::vector<entry>::iterator i=v.begin();i!=v.end();++i)        {        std::cout << i->string_of_words << "\n";        // or to output each word        std::istringstream is(i->string_of_words);        std::string s;        while(is >> s) std::cout << s << "\n";        }}


Provide more info about what you are trying to store if I am barking up the wrong tree.
ARC inc
ARC inc
Basicly what I am doing here is the data is for my packet structure(didnt post it in Networking because it is not about any networking code just normal stream buffering)

Basicly as you can see the packet would be structured like so.

[header]//contains packet ID ie- ID_CHA, ID_CAN etc etc.[length]//contains the packet length in bytes every header is 6bytes+sizeof[string_of_text]or [mapid, x,y] etc etc.[string_of_text]//contains chat msgs. also can be replaced with [mapid,x,y]etc etc.


Basicly I am trying to place all this data into a Dynamic buffer then write the data then send the data then read the data. I was told by a friend to use Dynamic buffers but I am now kinda thinking something else xD
swiftcoder
swiftcoder
Quote:
Original post by ARC inc
So basically could I use all the [header],[length],[string_of_words] or would I need to add these differently? Like on by one?
The correct syntax would be to add the lengths yourself:
dataBuffer = new char[header + length + string_of_words];
Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]
ARC inc
ARC inc
Quote:
Original post by swiftcoder
Quote:
Original post by ARC inc
So basically could I use all the [header],[length],[string_of_words] or would I need to add these differently? Like on by one?
The correct syntax would be to add the lengths yourself:*** Source Snippet Removed ***


Thanks the info with that.

@Kor-Thanks I'll look into that.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.