char *动态字符串 - 正确的方法 [英] char* dynamic strings - the right way

查看:94
本文介绍了char *动态字符串 - 正确的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建字符串/字符数组并将

分配给char *的正确方法是什么,然后在函数调用中使用。认为这将是很好的避免内存泄漏/核心转储。


1带字符的标头* x


2班级包括标题


3在班级成员函数中我想:

a)有条件地创建一个字符串,即填充x

b)传递(填充)x作为函数参数。


这样做的正确方法是什么:

i)分配字符串变量

ii)指定一个文字。


i)因为它是有条件的创建我想这不是一个好主意使用

malloc或new。


ii)只有x =一些字符串是否可以;

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";

推荐答案

dr******@gmail.com 写道:
创建字符串/字符数组并分配给char *然后在函数调用中使用的正确方法是什么?认为避免内存泄漏/核心转储会非常好。


正确的方法,简单的方法是使用std :: string。

1标头带字符* x

2类包括标题

3在类成员函数中我想:
a)有条件地创建一个字符串,即填充x
b)传递(填充)x作为函数参数。

这样做的正确方法是什么:
i)分配字符串变量
ii)分配字面值。

i)因为它' '有条件的创造我认为使用
malloc或者新的不是一个好主意。

ii)只需要x =some string;
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";




我认为你需要发布你写的实际代码,这很难

来弄清楚你的意思,如果你真的想要这样做

然后细节很重要。但正如我所说,简单的方法是删除char *

并改为使用std :: string。


std :: string x;


x =" some string" ;;

some_function(x);


什么可以更容易?没有内存泄漏,没有核心转储。


john



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




John Harrison写道:

John Harrison wrote:
dr******@gmail.com 写道:
创建字符串/字符数组并分配给char *然后在函数调用中使用的正确方法是什么?认为避免内存泄漏/核心转储是非常好的。
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.



正确的方法,简单的方法是使用std :: string。



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


1个带有字符的标题* x

2类包含标题

3在类成员函数中我想:
a)有条件地创建一个字符串,即填充x
b)传递(填充)x作为函数参数。

这样做的正确方法是什么:
我)分配字符串变量
ii)分配文字。
i)因为它是有条件的创建,我想使用
malloc或new不是一个好主意。
ii)是否可以只有x =some 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";



我认为你需要发布你写的实际代码,它相当很难确切地解决你的意思,如果你真的想这样做,那么细节就很重要了。但正如我所说,简单的方法是删除char *
并使用std :: string。

std :: string x;

x ="一些字符串;
some_function(x);

什么可以更容易?没有内存泄漏,没有核心转储。

john



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




处理现有代码即启动点是我有一个char *

并希望以不同的方式填充它:可以调用函数

返回char *或者可能希望分配字符串文字。那么。


x = f();是char * f(){...

x =" some string" ;;


首先是malloc / new到string / char的大小数组,但它是

有条件的,因为x被填充了。



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.



dr ****** @ gmail.com skrev:

dr******@gmail.com skrev:
John Harrison写道:
John Harrison wrote:
dr******@gmail.com 写道:
什么是创建字符串/字符数组并分配给char *的正确方法,然后在函数调用中使用它。认为避免内存泄漏/核心转储是非常好的。
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.



正确的方法,简单的方法是使用std :: string。



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


[snip] std :: string x;

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

什么可以更容易?没有内存泄漏,没有核心转储。

约翰

[snip] std::string x;

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

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

john



处理现有代码即启动点是我有一个char *
并希望要么以不同的方式填充它:可以调用一个函数
返回一个char *或者可能希望分配一个字符串文字。所以。

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

首先是malloc / new为string / char数组的大小,但它是
有条件的,因为whethr x被填充。



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.



简单的方法仍然是使用std :: string。你真的不能依赖

函数返回char *,除非你真的知道

里面发生了什么。你不知道已经为阵列分配了多少内存(如果有的话)

数组,你不知道你是否必须释放指针而你做了

不知道如何释放它。刚开始使用std :: string并封装

您现有的代码(或者重写它,如果这更容易)。


/ Peter


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


这篇关于char *动态字符串 - 正确的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆