S
stroker_ace
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:el() {
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.
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:el() {
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.