473,386 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Container of buffers

I need a container of char-buffers, for instance, vector of
char-buffers.

vector<string> v; // It is not what I need to

char a[10];
memset (&a[0], 0, sizeof (a));

v.push_back(a);

cout << v.back(); // This displays empty string, not 10 bytes. I need
to get 10 bytes in this example.

Is there any way to create a container of char-buffers?
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Mar 19 '06 #1
2 1740
Alex Vinokur <al****@users.sourceforge.net> wrote:
I need a container of char-buffers, for instance, vector of
char-buffers.

vector<string> v; // It is not what I need to
I would say this would work, if used right. But there are other
solutions.
char a[10];
memset (&a[0], 0, sizeof (a));

v.push_back(a);
Because v is a vector of strings, a temporary std::string object is
constructed based on 'a'. That means 'a' is treated like a C-style
string. Because 'a' contains all '\0' values, the char-array contains an
empty C-string, which is why the std::string object is also empty.
cout << v.back(); // This displays empty string, not 10 bytes. I need
to get 10 bytes in this example. Is there any way to create a container of char-buffers?


std::vector <std::string> v;
v.resize (20, std::string (10, 0));

Now 'v' contains 20 strings. Each of those contains 10 0-chars. Note
that you can dynamically change the size of each string. Instead of
using a std::string, you could also use a std::vector.

Another approach is to create a struct:

struct char_buffer
{
char data [10];
};

std::vector <char_buffer> v;
v.resize (20);
Now 'v' contains 20 'char_buffer' objects. Each of those contains 10
chars. Note that now you cannot dynamically change the size of each
char_buffer anymore.

hth
--
jb

(reply address in rot13, unscramble first)
Mar 19 '06 #2
Alex. Try something like the following:

typedef std::vector<std::string> my_container;

my_container c;

std::string s1(10,' ');

c.push_back( s1 );
Hope this help.

Mar 19 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: doodle4 | last post by:
Hello, I need to create 6 buffers in python and keep track of it. I need to pass this buffer, say buffer 1 as an index to a test app. Has any one tried to do this. Any help with buffer...
18
by: JG | last post by:
Does anyone know a standard (or supported on Linux, Mac, Win32) way to clear a read stream buffer (standard ANSI C file stream)? I would even settle for a platform specific way of doing it. ...
2
by: ldawson | last post by:
From the same C++ source code, I'm attempting to generate both an unmanaged DLL and a managed assembly. This will eliminate interop as the calling code is slowly migrated to .NET. However, C++...
3
by: Sally Sally | last post by:
I have a very basic question on the two parameters shared buffers and effective cache size. I have read articles on what each is about etc. But I still think I don't quite grasp what these settings...
2
by: | last post by:
Hi, we are planning to rewrite an extisting C++ image processing application/library in C#. Now several question arouse where I hope you can help me: So far we allocated a block of memory as...
0
by: Sam Durai | last post by:
Hello, A particular select query took unusually long time to execute hence I took an app.snap to find out what happens internally and I found out that tablequeue buffers are overflowing on a...
6
by: Defcon2030 | last post by:
<bHey, can someone help me with this? I've been working on it for a few days now, and my head's starting to spin... </b> // FILE:ex1_imp.cxx // // // // CLASS IMPLEMENTED: sequence (see ex1.h...
0
by: Lew | last post by:
Hi, I have a database that I am restoring to another database in the same instance using a redirected restore. It seems to work fine but what is troubling to me is in the diag log i see that it...
2
by: chenxinleo | last post by:
Hi, When i use some standard library functions and fields,which return char* type(like ctime in time.h, optarg in getopt.h)and do not have to be freed after calling,i always worry about memory...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.