Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > confused: vector<char*> and malloc

Reply
Thread Tools

confused: vector<char*> and malloc

 
 
Richard
Guest
Posts: n/a
 
      08-16-2005
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);
}
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?



 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-16-2005
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(strlen(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
 
Reply With Quote
 
 
 
 
Kyle
Guest
Posts: n/a
 
      08-16-2005
Richard wrote:
> vector<char*> m_Text;
> m_Text.resize(1);
> char* foo = "FOO";
> char* bar = "BAR";
> char* foobar = (char*)malloc(strlen(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_back( foobar );
 
Reply With Quote
 
=?iso-8859-1?Q?Ali_=C7ehreli?=
Guest
Posts: n/a
 
      08-16-2005
"Kyle" <(E-Mail Removed)> wrote in message
news:ddtj91$c16$(E-Mail Removed)...
> Richard wrote:
>> vector<char*> m_Text;
>> m_Text.resize(1);
>> char* foo = "FOO";
>> char* bar = "BAR";
>> char* foobar = (char*)malloc(strlen(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

 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      08-16-2005

"Kyle" <(E-Mail Removed)> wrote in message
news:ddtj91$c16$(E-Mail Removed)...
> Richard wrote:
> > vector<char*> m_Text;
> > m_Text.resize(1);
> > char* foo = "FOO";
> > char* bar = "BAR";
> > char* foobar = (char*)malloc(strlen(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_back( 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.


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      08-16-2005
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(char const*);
...
my_function(v[i].c_str());

V
 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      08-16-2005

"Victor Bazarov" <(E-Mail Removed)> wrote in message
news:X%sMe.30145$(E-Mail Removed)01.us.t o.verio.net...
> 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(char 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(10000000);
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?


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      08-16-2005
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(10000000);
> 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<char*>) +
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<string>) +
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
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      08-16-2005
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(10000000);
> 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(10000000);
char* tmp = "TEST 123";

for(int t = 0; t != 10000000; ++t)
{
test[t] = new char[9];
strcpy(test[t], tmp);
}




Brian
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      08-17-2005
Victor Bazarov wrote:
> Richard wrote:


> > vector<char*> m_Text;
> > m_Text.resize(1);


<snip>

> > char* foo = "FOO";
> > char* bar = "BAR";
> > char* foobar = (char*)malloc(strlen(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>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
to malloc or not to malloc?? Johs32 C Programming 4 03-30-2006 10:03 AM
porting non-malloc code to malloc micromysore@gmail.com C Programming 3 02-19-2005 05:39 AM
Malloc/Free - freeing memory allocated by malloc Peter C Programming 34 10-22-2004 10:23 AM
free'ing malloc'd structure with malloc'd members John C Programming 13 08-02-2004 11:45 AM
Re: free'ing malloc'd structure with malloc'd members ravi C Programming 0 07-30-2004 12:42 PM



Advertisments