vector<char*> m_Text;
m_Text.resize(1 );
char* foo = "FOO";
char* bar = "BAR";
char* foobar = (char*)malloc(s trlen(foo) + strlen(bar) + 1);
if (foobar)
{
strcpy(foobar, foo);
strcat(foobar, bar);
}
m_Text[0] = foobar;
// Will m_Text[0] get freed when m_Text goes out of scope? If not, should I
call free(m_Text[0]) in the destructor? 13 4640
Richard wrote: vector<char*> m_Text; m_Text.resize(1 );
This makes 'm_Text' to be 1 element long. And since 'm_Text' was empty
prior to that, it adds 1 pointer to it and makes it null.
char* foo = "FOO"; char* bar = "BAR"; char* foobar = (char*)malloc(s trlen(foo) + strlen(bar) + 1); if (foobar) { strcpy(foobar, foo); strcat(foobar, bar); }
At this point 'foobar' is a pointer to 7 character array, allocated in
the free store. The array has 'F', 'O', 'O', 'O', 'B', 'A', 'R', '\0'
in it, in sequence.
m_Text[0] = foobar;
This makes the value of the only element in the vector 'm_Text' to be the
same as the pointer to that 7-character array.
// Will m_Text[0] get freed when m_Text goes out of scope?
No.
If not, should I call free(m_Text[0]) in the destructor?
Probably. Assuming you're talking about the destructor of the class in
which 'm_Text' is a data member.
V
Richard wrote: vector<char*> m_Text; m_Text.resize(1 ); char* foo = "FOO"; char* bar = "BAR"; char* foobar = (char*)malloc(s trlen(foo) + strlen(bar) + 1);
malloc is C, why dont you use new ?
if (foobar) { strcpy(foobar, foo); strcat(foobar, bar); } m_Text[0] = foobar;
// Will m_Text[0] get freed when m_Text goes out of scope? If not, should I call free(m_Text[0]) in the destructor?
try this if you dont want to manage memory on your own
vector<string> m_Text;
char* foo = "FOO";
char* bar = "BAR";
string foobar(foo);
foobar.append( bar );
m_Text.push_bac k( foobar );
"Kyle" <in*****@e.mail .com> wrote in message
news:dd******** **@nemesis.news .tpi.pl... Richard wrote: vector<char*> m_Text; m_Text.resize(1 ); char* foo = "FOO"; char* bar = "BAR"; char* foobar = (char*)malloc(s trlen(foo) + strlen(bar) + 1); malloc is C, why dont you use new ?
malloc is C++ too.
It is used to allocate raw memory in C++. Since new (and new[]) allocates
space and constructs object(s), new (and new[]) is not suitable when there
is not enough information to construct the object(s).
try this if you dont want to manage memory on your own
vector<string> m_Text;
Great advice!
Ali
"Kyle" <in*****@e.mail .com> wrote in message
news:dd******** **@nemesis.news .tpi.pl... Richard wrote: vector<char*> m_Text; m_Text.resize(1 ); char* foo = "FOO"; char* bar = "BAR"; char* foobar = (char*)malloc(s trlen(foo) + strlen(bar) + 1);
malloc is C, why dont you use new ?
if (foobar) { strcpy(foobar, foo); strcat(foobar, bar); } m_Text[0] = foobar;
// Will m_Text[0] get freed when m_Text goes out of scope? If not,
should I call free(m_Text[0]) in the destructor?
try this if you dont want to manage memory on your own
vector<string> m_Text;
char* foo = "FOO"; char* bar = "BAR";
string foobar(foo); foobar.append( bar );
m_Text.push_bac k( foobar );
That leads me to another question. Is it common practice to pass std::string
as a function argument? Is there much overhead?
void Foo(string& str)
If I went with vector<string>, would I need to covert all my functions that
currently use char* as an argument?
Thanks.
Richard wrote: [...] Is it common practice to pass std::string as a function argument?
By value, no. By a reference, or by a reference to const, yes.
Is there much overhead?
There can be. Just like with any other UDT.
void Foo(string& str)
If I went with vector<string>, would I need to covert all my functions that currently use char* as an argument?
Yes, most likely. BTW, if your functions that currently use 'char*' do
not change the characters in the arrays, you should declare 'char const*'
as the argument type. Then you don't have to change much, but you will
need to use 'c_str' member:
void my_function(cha r const*);
...
my_function(v[i].c_str());
V
"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:X%******** ***********@new sread1.mlpsca01 .us.to.verio.ne t... Richard wrote: [...] Is it common practice to pass std::string as a function argument?
By value, no. By a reference, or by a reference to const, yes.
> Is there much overhead?
There can be. Just like with any other UDT.
void Foo(string& str)
If I went with vector<string>, would I need to covert all my functions
that currently use char* as an argument?
Yes, most likely. BTW, if your functions that currently use 'char*' do not change the characters in the arrays, you should declare 'char const*' as the argument type. Then you don't have to change much, but you will need to use 'c_str' member:
void my_function(cha r const*); ... my_function(v[i].c_str());
V
My array of strings is very large. I did a small test using char* vs string
and the results were really bad. I must be doing something wrong:
vector<char*> test;
test.resize(100 00000);
for(int t = 0; t != 10000000; ++t)
{
test[t] = "TEST 123";
}
That takes up a minimal amount of memory if using char*. But if you change
it to vector<string> test; it takes up around 1000 times more memory! What
am I doing wrong?
Richard wrote: My array of strings is very large. I did a small test using char* vs string and the results were really bad. I must be doing something wrong:
vector<char*> test; test.resize(100 00000); for(int t = 0; t != 10000000; ++t) { test[t] = "TEST 123"; }
That takes up a minimal amount of memory if using char*.
Yeah... You got a vector all elements of which are the same. No extra
memory is allocated, just 10 million pointers. The consumption of memory
(not considering the overhead for dynamic memory management) is quite easy
to calculate:
sizeof(vector<c har*>) +
sizeof(char*) * test.size() + sizeof("TEST 123")
(which should give about 40000000, give or take a few bytes)
But if you change it to vector<string> test; it takes up around 1000 times more memory! What am I doing wrong?
I am not sure, to be honest with you. Every 'string' maintains its own
array of char internally. When you resize the 'test' vector to contain
ten millions of 'string' objects, it first puts a bunch of empty ones
there, and then when you in the loop assign those values, every string
in the vector needs to allocate its own small array (and possibly a bit
larger than asked for), which may lead to severe fragmentation of memory,
especially considering that a temporary is probably created to accommodate
your "TEST 123" literal... The final memory cost should be around
sizeof(vector<s tring>) +
sizeof(string) * test.size() +
sizeof("TEST 123") * test.size()
It is hard to believe it takes "around 1000 times more memory". The
string objects themselves are not that big, so you might be looking at
four, maybe ten, times the memory consumption, but definitely not the
thousand times. Are you running on a 64-bit machine? 1000 times more
with a vector of 10 million pointers is beyond what a 32-bit machine can
handle, that's for sure.
V
Richard wrote: My array of strings is very large. I did a small test using char* vs string and the results were really bad. I must be doing something wrong:
vector<char*> test; test.resize(100 00000); for(int t = 0; t != 10000000; ++t) { test[t] = "TEST 123"; }
That takes up a minimal amount of memory if using char*. But if you change it to vector<string> test; it takes up around 1000 times more memory! What am I doing wrong?
You stuff 100 million copies of a pointer to the same piece of memory
into the vector. In the real application, you would presumably have a
different string in each slot in the vector.
To do a fairer comparison:
vector<char*> test;
test.resize(100 00000);
char* tmp = "TEST 123";
for(int t = 0; t != 10000000; ++t)
{
test[t] = new char[9];
strcpy(test[t], tmp);
}
Brian
Victor Bazarov wrote: Richard wrote: vector<char*> m_Text; m_Text.resize(1 );
<snip>
char* foo = "FOO"; char* bar = "BAR"; char* foobar = (char*)malloc(s trlen(foo) + strlen(bar) + 1); if (foobar) { strcpy(foobar, foo); strcat(foobar, bar); }
At this point 'foobar' is a pointer to 7 character array, allocated in the free store. The array has 'F', 'O', 'O', 'O', 'B', 'A', 'R', '\0' in it, in sequence.
'F', 'O', 'O', 'B', 'A', 'R', '\0'
<snip> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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
|
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
|
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) ;
|
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;
| |
by: Jim Langston |
last post by:
I'm using a function like this:
char TextBuffer;
jGet_DropDown_Selected_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<charinstead of a char
array, but would that actually gain me anything at all? I mean, what's the
difference between calling it like the...
|
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);
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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,...
|
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...
|
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...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |