473,657 Members | 2,535 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Function expecting char*, does using vector<char> gain me anything?

I'm using a function like this:

char TextBuffer[261];
jGet_DropDown_S elected_Text( cc.ddSex, TextBuffer);

Where the function is filling in the text buffer. I don't have access to
the actual function to change it to a std::string (it's a library function)
so I was thinking, well, I could use a std::vector<cha rinstead of a char
array, but would that actually gain me anything at all? I mean, what's the
difference between calling it like the previous, and calling it like this:

std::vector<cha rTextBuffer;
TextBuffer.resi ze(261);
jGet_DropDown_S elected_Text( cc.ddSex, &TextBuffer[0]);

with any needed casting I may need.

Now, if std::string could be called this way it would be wonderful! I would
just live to do:

std::string TextBuffer;
TextBuffer.resi ze(261); // or similar
jGet_DropDown_S elected_Text( cc.ddSex, TextBuffer.c_st r());

But as I'm sure most of us are aware, c_str() returns a const char * and
can't be used this way (the main reason std::string isn't used instead of
char arrays IMO).

So, should I just stay with the char array?
Nov 14 '06 #1
4 2381

Jim Langston wrote:
Now, if std::string could be called this way it would be wonderful!
std::string has a begin() and end() so you can use it in the same way
as u did for vector<char>

&*str.begin( ) would give you access to the data in str.

Or you could be bad and cast away the constness:
(char*)str.c_st r()

Regards
Vivek

Nov 14 '06 #2
Hi

Jim Langston wrote:
I'm using a function like this:

char TextBuffer[261];
jGet_DropDown_S elected_Text( cc.ddSex, TextBuffer);

Where the function is filling in the text buffer.
I don't like the design of the library. Is there any guarantee that
jGet_DropDown_S elected_Text will only ever write 261 bytes?
What happens if the "selected text" is longer than that?
I would have expected another parameter (the size of the array) for checking
array bounds. But if you have to work with it... make sure that your buffer
is guaranteed to be large enough.
I don't have access to the actual function to change it to a std::string
(it's a library function) so I was thinking, well, I could use a
std::vector<cha rinstead of a char array, but would that actually gain me
anything at all?
No. Nothing. It would make your code harder to read, yes.
I mean, what's the difference between calling it like the previous, and
calling it like this:

std::vector<cha rTextBuffer;
TextBuffer.resi ze(261);
jGet_DropDown_S elected_Text( cc.ddSex, &TextBuffer[0]);

with any needed casting I may need.
The difference is that the above is a line longer, uses another type and
weird syntax (&TextBuffer[0]) while offering no advantages at all.
Now, if std::string could be called this way it would be wonderful! I
would just live to do:

std::string TextBuffer;
TextBuffer.resi ze(261); // or similar
jGet_DropDown_S elected_Text( cc.ddSex, TextBuffer.c_st r());
This would only be a little better, but I wouldn't call it wonderful.
But as I'm sure most of us are aware, c_str() returns a const char * and
can't be used this way (the main reason std::string isn't used instead of
char arrays IMO).
Who told you std::string isn't used? I think the main reason why it isn't
used in the library you have, is that it is probably a C library.
So, should I just stay with the char array?
Yes, I would. If you feel any need, you can still copy the contents into a
std::string afterwards.

Markus

Nov 14 '06 #3
rep_movsd wrote:
Jim Langston wrote:
>Now, if std::string could be called this way it would be wonderful!

std::string has a begin() and end() so you can use it in the same way
as u did for vector<char>

&*str.begin( ) would give you access to the data in str.
Don't do this. The standard library string makes no guarantee that the
buffer is consistent with the plain old char[].
>
Or you could be bad and cast away the constness:
(char*)str.c_st r()
Don't do this either. Both suggestions from rep_movsd produce Undefined
Behavior. Its better to make your program CORRECT; then you can worry
about style.

I guess a better solution is to overload jGet_DropDown_S elected_Text so
it will accept a string:

std::string jGet_DropDown_S elected_Text(wh atever_t whatever)
{
char buff[256];
jGet_DropDown_S elected_Text(wh atever, buff);
return std::string(buf f);
}

// use the overloaded function like
std::string str = jGet_DropDown_S elected_Text(cc .ddSex);
>
Regards
Vivek
Ben
Nov 14 '06 #4

Jim Langston wrote:
I'm using a function like this:

char TextBuffer[261];
jGet_DropDown_S elected_Text( cc.ddSex, TextBuffer);

Where the function is filling in the text buffer. I don't have access to
the actual function to change it to a std::string (it's a library function)
so I was thinking, well, I could use a std::vector<cha rinstead of a char
array, but would that actually gain me anything at all? I mean, what's the
difference between calling it like the previous, and calling it like this:

std::vector<cha rTextBuffer;
TextBuffer.resi ze(261);
jGet_DropDown_S elected_Text( cc.ddSex, &TextBuffer[0]);

with any needed casting I may need.
You don't need any. In your case there is no real gain but that's
because you have hard-coded in a magic number that is 261. If your
number is a variable then you would need to use vector, although
programmers in C99 can dynamically create arrays. However C99
programmers don't have vector available to them.
Now, if std::string could be called this way it would be wonderful! I would
just live to do:

std::string TextBuffer;
TextBuffer.resi ze(261); // or similar
jGet_DropDown_S elected_Text( cc.ddSex, TextBuffer.c_st r());

But as I'm sure most of us are aware, c_str() returns a const char * and
can't be used this way (the main reason std::string isn't used instead of
char arrays IMO).
std::string is not writable in this way. So use vector albeit that it
brings in 2 inefficiencies (that might not be critical) in that (1) it
initialises the buffer and (2) you then have to copy it into a string
to be able to use string functions (wouldn't be necessary if much of
the string functionality were free-functions and could work with
vectors of characters too).
So, should I just stay with the char array?
With the char array you gain 2 advantages: (1) no runtime allocation
(2) no initialisation.
So if you really can guarantee that you want a size of 261 then char
array may be the best option. You might want to "enumerate" your magic
number though.

Nov 14 '06 #5

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

Similar topics

5
1882
by: Steven Reddie | last post by:
I have a large char which I'd like to copy a portion of into a vector<char>. I can't seem to locate a simple method for doing so. I assume involving iterators will not result in memcpy not being used and therefore will be less than optimal. Can anyone help me out? Regards, Steven
3
2796
by: Chris | last post by:
Hi, I'm playing/developing a simple p2p file sharing client for an existing protocol. A 2 second description of the protocol is Message Length = int, 4 bytes Message Code = int, 4 bytes Data
2
4764
by: Chris Thompson | last post by:
Hi I'm writing a p2p client for an existing protocol. I used a std::vector<char> as a buffer for messages read from the server. The message length is the first 4 bytes. The message code the second 4. The total message length is therefore 4 + message length. A number of messages work fine/as expected but there are consistant errors occuring. After a period
6
4131
by: me | last post by:
Hi guys - the question is in the subject line. I thought of one quick way: std::ifstream input("myfile.dat") ; std::istreambuf_iterator beg(input), end ; std::vector DataFile(beg,end) ;
13
4641
by: Richard | last post by:
vector<char*> m_Text; m_Text.resize(1); char* foo = "FOO"; char* bar = "BAR"; char* foobar = (char*)malloc(strlen(foo) + strlen(bar) + 1); if (foobar) { strcpy(foobar, foo); strcat(foobar, bar); }
8
4319
by: Marco Costa | last post by:
Hello all, I wrote a simple ODBC wrapper class that used code like this ( not real code, added types for clarification ): char** type bufs = new char* for( int i = 0 ; i < numberOfColumns ; i++ ) { size = getSizeOfColumn(); bufs = new char;
6
2756
by: JackC | last post by:
Hi, If i have a vector<charmaybe 15mb in size, whats the most efficient way to write out all the elements to an output file? I have tried this: for(int z = 0; z < Output_Buffer.size(); z++) { write(ostr, (char *) Output_Buffer, 1);
0
8324
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8842
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8740
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8617
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7352
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6176
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.