字符串vs char *和strcpy() [英] Strings vs char* and strcpy()

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

问题描述




我想知道是否有人能指出我讨论的问题

C ++ String类型和char之间的异同*

字符串?


我问的原因是因为我正在使用旧的C套接字使用

来处理遗留应用程序。 h图书馆。 send函数接受消息为

作为char *发送。


我在使用C ++编写char *字符串的深层副本时遇到问题。


请考虑以下代码示例:


class消息{


public:


消息():未发送(NULL){

};


void Add(char *);

void Del();

char * GetMsg();

void ShiftMsg(int);


〜留言(){

Del();

}


私人:


char * wholeMessage;

char * unsentBytes;


};


//创建深层副本inmessage

void Message :: Add(char * inmessage){


wholeMessage = new char [strlen(inmessage)+ 1];


strcpy(wholeMessage,inmessage);


unsentBytes = wholeMessage;


}

//释放分配给wholeMessage的内存

void Message :: Del() {


if(wholeMessage!= NULL){

delete [] wholeMessage;

}

unsentBytes = NULL;


}


//获取未发送的部分消息

char * Message :: GetUnsent(){


if(unsentBytes == NULL){

return"" ;;

}

else {

返回unsentBytes;

}

}


//增加unsentBytes指向指向未发送部分的指示

//消息

void消息:: ShiftMsg(int bytesSent){

unsentBytes = unsentBytes + bytesSent;

}

我有问题。当我尝试使用new运算符分配使用
wholeMessage的内存时,我得到了一个访问冲突,但我确实不知道为什么。谁能解释我做错了什么?


我意识到我可能*应该*使用C ++字符串类型存储

我的消息。我知道我可以将一个char *分配给一个String变量但是

它是否相反?我可以将一个字符串传递给一个函数

期待一个字母*


最后,关于一般风格的问题。我觉得我应该为我正在处理和销毁的每个字符串创建一个新的消息实例

它一旦发送但这将涉及我创建和销毁数百个

实例一秒钟。这样做有多大的开销吗?


非常感谢您的帮助。


Lawrie。

Hi,

I wonder if anyone could point me in the direction of a discussion on
the similarities and differences between the C++ String type and char*
strings?

The reason I ask is because I am working on a legacy application using
the old C socket.h library. The send function accepts the message to be
sent as a char*.

I am having problems making a deep copy of a char* string in C++.

Consider the following code sample:

class Message {

public:

Message () : unsent(NULL) {
};

void Add(char*);
void Del();
char* GetMsg();
void ShiftMsg(int);

~Message() {
Del();
}

private:

char* wholeMessage;
char* unsentBytes;

};

//Create deep copy of inmessage
void Message::Add (char* inmessage) {

wholeMessage = new char[strlen(inmessage) + 1];

strcpy(wholeMessage,inmessage);

unsentBytes = wholeMessage;

}

//Release memory allocated to wholeMessage
void Message::Del() {

if (wholeMessage != NULL) {
delete [] wholeMessage;
}
unsentBytes = NULL;

}

//Get unsent portion of message
char* Message::GetUnsent() {

if (unsentBytes == NULL) {
return "";
}
else {
return unsentBytes;
}
}

//Increment unsentBytes pointer to point at unsent portion of
//message
void Message::ShiftMsg(int bytesSent) {
unsentBytes = unsentBytes + bytesSent;
}
I have a problem. When I attempt to allocate the momory for
wholeMessage using the new operator I get an access violation yet I do
not understand why. Can anyone explain what I am doing wrong?

I realise that I probably *should* be using C++ String types to store
my message. I know that I can allocate a char* to a String variable but
does it work the other way round? Can I pass a String to a function
expecting a char*

Finally, a question about general style. I feel I should be creating a
new instance of Message for each string I am processing and destroying
it once sent but this would involve me creating and destroying hundreds
of instances a second. Is there a large overhead involved in doing
this?

Many thanks for any help.

Lawrie.

推荐答案

>我意识到我可能*应该*使用C ++ String类型来存储
> I realise that I probably *should* be using C++ String types to store
我的消息。我知道我可以将一个char *分配给一个String变量,但
是否可以反过来运行?我可以将String传递给函数
期望char *
my message. I know that I can allocate a char* to a String variable but
does it work the other way round? Can I pass a String to a function
expecting a char*



使用string'的cstr方法从字符串对象中获取C char *。

对不起,我正在回答你的一个问题。我不确定对方

自己回答。


Srini



Use string''s cstr method to get the C char* from a string object.
Sorry I am answering one of your questions. I am not sure of the other
answers myself.

Srini




st*********@hotmail.com 写道:


我想知道是否有人可以指点我讨论C ++ String类型和char *之间的相同点和不同点br />字符串?

我问的原因是因为我正在使用旧的Cocket.h库来处理遗留应用程序。 send函数接受要作为char *发送的消息。

我在使用C ++编写char *字符串的深层副本时遇到问题。

请考虑以下代码示例:

类消息{

public:

Message():unsent(NULL){
};

void Add(char *);
void Add(const char *); // 会更好。 void Del();
char * GetMsg();
const char * GetMsg(); //会更好,防止修改msg

//在这个类之外void ShiftMsg(int);

~Message(){
Del();
}

私人:

char * wholeMessage;
char * unsentBytes;

};

//创建inmessage的深层副本
void Message :: Add(char * inmessage){

wholeMessage = new char [strlen(inmessage)+ 1];
inmessage需要检查它是否为NULL。

因为strlen没有检查。如果inmessage为NULL,它会崩溃。
strcpy(wholeMessage,inmessage);

unsentBytes = wholeMessage;

}

/ /释放分配给wholeMessage的内存
void Message :: Del(){
if(wholeMessage!= NULL){
delete [] wholeMessage;
}
unsentBytes = NULL;

}
//获取未发送的部分消息
char * Message :: GetUnsent(){

if(unsentBytes == NULL){
return"" ;;
}
else {
返回unsentBytes;
}
}
//增加unsentBytes指向
//消息的未发送部分的指示
void Message :: ShiftMsg(int bytesSent){
unsentBytes = unsentBytes + bytesSent;
}

我有问题。当我尝试使用new运算符分配
wholeMessage的内容时,我得到了访问冲突,但我不明白为什么。谁能解释我做错了什么?

我意识到我可能*应该*使用C ++ String类型存储我的消息。我知道我可以将一个char *分配给一个String变量,但
是否可以反过来运行?我可以将一个String传递给一个函数
期待一个char *

最后,关于一般风格的问题。我觉得我应该为我正在处理和销毁的每个字符串创建一个新的Message实例,但这将涉及我创建和销毁数百个实例。做这个有很大的开销吗?

非常感谢任何帮助。

Lawrie。
Hi,

I wonder if anyone could point me in the direction of a discussion on
the similarities and differences between the C++ String type and char*
strings?

The reason I ask is because I am working on a legacy application using
the old C socket.h library. The send function accepts the message to be
sent as a char*.

I am having problems making a deep copy of a char* string in C++.

Consider the following code sample:

class Message {

public:

Message () : unsent(NULL) {
};

void Add(char*); void Add(const char *); // would be better. void Del();
char* GetMsg(); const char* GetMsg(); // would be better, to prevent modifying msg
//outside this class void ShiftMsg(int);

~Message() {
Del();
}

private:

char* wholeMessage;
char* unsentBytes;

};

//Create deep copy of inmessage
void Message::Add (char* inmessage) {

wholeMessage = new char[strlen(inmessage) + 1]; inmessage need to be checked whether it''s NULL or not.
Because strlen doesn''t check. and it would crash if inmessage is NULL.
strcpy(wholeMessage,inmessage);

unsentBytes = wholeMessage;

}

//Release memory allocated to wholeMessage
void Message::Del() {

if (wholeMessage != NULL) {
delete [] wholeMessage;
}
unsentBytes = NULL;

}

//Get unsent portion of message
char* Message::GetUnsent() {

if (unsentBytes == NULL) {
return "";
}
else {
return unsentBytes;
}
}

//Increment unsentBytes pointer to point at unsent portion of
//message
void Message::ShiftMsg(int bytesSent) {
unsentBytes = unsentBytes + bytesSent;
}
I have a problem. When I attempt to allocate the momory for
wholeMessage using the new operator I get an access violation yet I do
not understand why. Can anyone explain what I am doing wrong?

I realise that I probably *should* be using C++ String types to store
my message. I know that I can allocate a char* to a String variable but
does it work the other way round? Can I pass a String to a function
expecting a char*

Finally, a question about general style. I feel I should be creating a
new instance of Message for each string I am processing and destroying
it once sent but this would involve me creating and destroying hundreds
of instances a second. Is there a large overhead involved in doing
this?

Many thanks for any help.

Lawrie.






st ********* @ hotmail.com 写道:

我有一个问题。当我尝试使用new运算符分配
wholeMessage的内容时,我得到了访问冲突,但我不明白为什么。任何人都可以解释我做错了吗?

inmessage NULL终止了吗?

我意识到我可能*应该*使用C ++ String类型存储
我的信息。我知道我可以将一个char *分配给一个String变量,但
是否可以反过来运行?我可以将一个String传递给一个函数
期待一个char *

string.c_str()这样做。

最后,关于一般风格的问题。我觉得我应该为我正在处理和销毁的每个字符串创建一个新的Message实例,但这将涉及我创建和销毁数百个实例。这样做会有很大的开销吗?

I have a problem. When I attempt to allocate the momory for
wholeMessage using the new operator I get an access violation yet I do
not understand why. Can anyone explain what I am doing wrong?
Is inmessage NULL terminated?
I realise that I probably *should* be using C++ String types to store
my message. I know that I can allocate a char* to a String variable but
does it work the other way round? Can I pass a String to a function
expecting a char*
string.c_str() does this.
Finally, a question about general style. I feel I should be creating a
new instance of Message for each string I am processing and destroying
it once sent but this would involve me creating and destroying hundreds
of instances a second. Is there a large overhead involved in doing
this?



会有一个开销,由你决定它是否过高。

尝试最简单的工作,如果有必要进行优化。


Ian


There will be an overhead, it''s up to you to decide if it is too high.
Try the most simple thing that will work, optimise if you have to.

Ian


这篇关于字符串vs char *和strcpy()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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