Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > char* dynamic strings - the right way

Reply
Thread Tools

char* dynamic strings - the right way

 
 
drj0nson@gmail.com
Guest
Posts: n/a
 
      11-08-2005
What is the right way of creating a string/char array and assigning to
a char* which is then used in a function call. Thought it would be
quite nice to avoid a memory leakage /core dump.

1 Header with char* x

2 Class includes header

3 In Class member function I want to :
a)conditionally create a string ie populate x
b)pass (populated) x on as a function parameter.

What is the right way of doing this for :
i) assigning string variable
ii) assigning a literal.

i) Since it's a conditional create I guess it is not a good idea to use
malloc or new.

ii) Is it ok to just have x = "some string";

 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      11-08-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> What is the right way of creating a string/char array and assigning to
> a char* which is then used in a function call. Thought it would be
> quite nice to avoid a memory leakage /core dump.


The right way, the easy way is to use std::string.

>
> 1 Header with char* x
>
> 2 Class includes header
>
> 3 In Class member function I want to :
> a)conditionally create a string ie populate x
> b)pass (populated) x on as a function parameter.
>
> What is the right way of doing this for :
> i) assigning string variable
> ii) assigning a literal.
>
> i) Since it's a conditional create I guess it is not a good idea to use
> malloc or new.
>
> ii) Is it ok to just have x = "some string";
>


I think you need to post the actual code you have written, it quite hard
to work out exactly what you mean, and if you really want to do it this
way then the details matter. But as I said the easy way is to drop char*
and use std::string instead.

std::string x;

x = "some string";
some_function(x);

What could be easier? No memory leaks, no core dumps.

john
 
Reply With Quote
 
 
 
 
drj0nson@gmail.com
Guest
Posts: n/a
 
      11-08-2005

John Harrison wrote:
> (E-Mail Removed) wrote:
> > What is the right way of creating a string/char array and assigning to
> > a char* which is then used in a function call. Thought it would be
> > quite nice to avoid a memory leakage /core dump.

>
> The right way, the easy way is to use std::string.
>
> >
> > 1 Header with char* x
> >
> > 2 Class includes header
> >
> > 3 In Class member function I want to :
> > a)conditionally create a string ie populate x
> > b)pass (populated) x on as a function parameter.
> >
> > What is the right way of doing this for :
> > i) assigning string variable
> > ii) assigning a literal.
> >
> > i) Since it's a conditional create I guess it is not a good idea to use
> > malloc or new.
> >
> > ii) Is it ok to just have x = "some string";
> >

>
> I think you need to post the actual code you have written, it quite hard
> to work out exactly what you mean, and if you really want to do it this
> way then the details matter. But as I said the easy way is to drop char*
> and use std::string instead.
>
> std::string x;
>
> x = "some string";
> some_function(x);
>
> What could be easier? No memory leaks, no core dumps.
>
> john


Dealing with existing code i.e.starting point is that I have a char*
and wish to either populate this in different ways : may call a func
that returns a char* or may wish to assign a string literal. So.

x = f(); being char* f() {...
x = "some string";

First though was to malloc/new to size of string/char array but it is
conditional as to whethr x gets populated.

 
Reply With Quote
 
peter koch
Guest
Posts: n/a
 
      11-08-2005

(E-Mail Removed) skrev:

> John Harrison wrote:
> > (E-Mail Removed) wrote:
> > > What is the right way of creating a string/char array and assigning to
> > > a char* which is then used in a function call. Thought it would be
> > > quite nice to avoid a memory leakage /core dump.

> >
> > The right way, the easy way is to use std::string.
> >
> > >

[snip]
> > std::string x;
> >
> > x = "some string";
> > some_function(x);
> >
> > What could be easier? No memory leaks, no core dumps.
> >
> > john

>
> Dealing with existing code i.e.starting point is that I have a char*
> and wish to either populate this in different ways : may call a func
> that returns a char* or may wish to assign a string literal. So.
>
> x = f(); being char* f() {...
> x = "some string";
>
> First though was to malloc/new to size of string/char array but it is
> conditional as to whethr x gets populated.

The easy way still is to use std::string. You really can't rely on
functions returning char* unless you really know what is going on
inside. You do not know how much memory (if any) has been allocated for
the array, you don't know if you have to free the pointer and you do
not know how to free it. Just start using std::string and encapsulate
your existing code (or rewrite it, if that is easier).

/Peter

 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      11-08-2005
(E-Mail Removed) wrote:
> John Harrison wrote:
>
>>(E-Mail Removed) wrote:
>>
>>>What is the right way of creating a string/char array and assigning to
>>>a char* which is then used in a function call. Thought it would be
>>>quite nice to avoid a memory leakage /core dump.

>>
>>The right way, the easy way is to use std::string.
>>
>>
>>>1 Header with char* x
>>>
>>>2 Class includes header
>>>
>>>3 In Class member function I want to :
>>>a)conditionally create a string ie populate x
>>>b)pass (populated) x on as a function parameter.
>>>
>>>What is the right way of doing this for :
>>>i) assigning string variable
>>>ii) assigning a literal.
>>>
>>>i) Since it's a conditional create I guess it is not a good idea to use
>>>malloc or new.
>>>
>>>ii) Is it ok to just have x = "some string";
>>>

>>
>>I think you need to post the actual code you have written, it quite hard
>>to work out exactly what you mean, and if you really want to do it this
>>way then the details matter. But as I said the easy way is to drop char*
>>and use std::string instead.
>>
>>std::string x;
>>
>>x = "some string";
>>some_function(x);
>>
>>What could be easier? No memory leaks, no core dumps.
>>
>>john

>
>
> Dealing with existing code i.e.starting point is that I have a char*
> and wish to either populate this in different ways : may call a func
> that returns a char* or may wish to assign a string literal. So.
>
> x = f(); being char* f() {...
> x = "some string";
>
> First though was to malloc/new to size of string/char array but it is
> conditional as to whethr x gets populated.
>


Whose responsibility is it to free the memory returned from f()?

Still dont see why you can't ise std::string

std::string x;

if (something)
x = f();
else
x = "some string";

john
 
Reply With Quote
 
Eckhard Lehmann
Guest
Posts: n/a
 
      11-09-2005
(E-Mail Removed) schrieb:
> What is the right way of creating a string/char array and assigning to
> a char* which is then used in a function call. Thought it would be


First of all, remember: char* is a _pointer_ to a char.
Not a string, not an array or anything else goofy. Just a pointer.

> quite nice to avoid a memory leakage /core dump.


If you want to use char*, memory management is up to you. However, if
you use functions that return char*, you may rely on the fact, that the
memory is properly allocated there.

>
> 1 Header with char* x
>
> 2 Class includes header
>
> 3 In Class member function I want to :
> a)conditionally create a string ie populate x
> b)pass (populated) x on as a function parameter.
>
> What is the right way of doing this for :
> i) assigning string variable
> ii) assigning a literal.
>
> i) Since it's a conditional create I guess it is not a good idea to use
> malloc or new.
>
> ii) Is it ok to just have x = "some string";
>


What is the problem here? Hard to figure out what you mean, can you post
any code?


Eckhard
 
Reply With Quote
 
drj0nson@gmail.com
Guest
Posts: n/a
 
      11-20-2005

Eckhard Lehmann wrote:
> (E-Mail Removed) schrieb:
> > What is the right way of creating a string/char array and assigning to
> > a char* which is then used in a function call. Thought it would be

>
> First of all, remember: char* is a _pointer_ to a char.
> Not a string, not an array or anything else goofy. Just a pointer.
>
> > quite nice to avoid a memory leakage /core dump.

>
> If you want to use char*, memory management is up to you. However, if
> you use functions that return char*, you may rely on the fact, that the
> memory is properly allocated there.
>
> >
> > 1 Header with char* x
> >
> > 2 Class includes header
> >
> > 3 In Class member function I want to :
> > a)conditionally create a string ie populate x
> > b)pass (populated) x on as a function parameter.
> >
> > What is the right way of doing this for :
> > i) assigning string variable
> > ii) assigning a literal.
> >
> > i) Since it's a conditional create I guess it is not a good idea to use
> > malloc or new.
> >
> > ii) Is it ok to just have x = "some string";
> >

>
> What is the problem here? Hard to figure out what you mean, can you post
> any code?
>
>
> Eckhard


Ok here is some code but in other places I would deal with char* /
char[] and not a string.

void f2(string &s)
{
s="qwerty";
}

void f1(myStruct &ms)
{
string s;

f2(s);
// Error 203: # Cannot assign 'char *' with 'const char *'.
//ms.cp = s.c_str();

ms.cp = (char*)s.c_str();
}


int main () {
myStruct mystruct;

f1(mystruct);
cout << mystruct.cp << endl;
return 0;
}

Note:
1) am stuck with using a char* - non-negotiable.
2) That I get Error 203 and need to cast seems to indicate this isn't
the correct way.
3) I did wonder if the s.c_str() would go out of scope leaving f1()
i.e. is this dangerous?
4) I would have thought it would be best to allocate memory to the
char* in main() but at that point the string size is unknown.

Sorry for the delay in responding.

 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      11-21-2005
(E-Mail Removed) wrote:

> 2) That I get Error 203 and need to cast seems to indicate this isn't
> the correct way.


Do you know what const means?

> 3) I did wonder if the s.c_str() would go out of scope leaving f1()
> i.e. is this dangerous?


The return of c_str() ceases to be valid when a non-const method
(including the destructor) is run on the object.

> 4) I would have thought it would be best to allocate memory to the
> char* in main() but at that point the string size is unknown.
>

So how would you do it?
 
Reply With Quote
 
drj0nson@gmail.com
Guest
Posts: n/a
 
      11-21-2005
I do not know the correct way of doing this - that is why I am posting
here!

1. You have a char *
2. You need to go off and populate this with a string or char array but
you do not know the size of the string., so....
3. string seems to be 'the' way of doing this but
a) c_str() returns a const and needs casting using char*
b) although the c_str() casting works and it is possible to print
the string out later - is this a case of something that will work
but not always?

 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      11-21-2005
(E-Mail Removed) wrote:
> I do not know the correct way of doing this - that is why I am posting
> here!
>
> 1. You have a char *
> 2. You need to go off and populate this with a string or char array but
> you do not know the size of the string., so....
> 3. string seems to be 'the' way of doing this but
> a) c_str() returns a const and needs casting using char*
> b) although the c_str() casting works and it is possible to print
> the string out later - is this a case of something that will work
> but not always?
>


You need to allocate memory

string s = "qwerty";
char* ptr = new char[s.size() + 1];
strcpy(ptr, s.c_str());
....
// sometime later when you are done
delete[] ptr;

It's because of this mess (particularly the need to delete[]) that you
should use strings instead of char, if you possibly can.

john
 
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
Dynamic Menu Items is not right aligned with Right to Left documen =?Utf-8?B?QmlzaG95?= ASP .Net 0 12-28-2006 11:39 AM
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
Tool to enable Right click on pages where Right click is disabled tsteinke@gmail.com Computer Support 4 08-28-2005 11:53 PM
Tool to right click image in windows explorer and rotate image right or left 90 degrees siliconpi Digital Photography 2 11-29-2004 12:56 PM
pass the right form input to the right control Tom ASP .Net 0 12-11-2003 03:07 AM



Advertisments