Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am brushing my C++ skills and typed a demo program as below, but I got different length of source text value and target text value so that my demo throws an exception. I write this demo in Visual Studio 2019. please help me on this question.

C++
#include <iostream>
#include <string.h>
#include <stdlib.h>     
using namespace std;

class CMessage {
private:
    char* pmessage=0;

public:
    void ShowIt()
    {
        cout << endl << pmessage;
    }

    //constructor
    CMessage(const char* text = "default message")
    {
        pmessage = new char[strlen(text) + 1];
        cout << " the length of text is:" << strlen(text) << endl;
        cout << " the length of pmessage is:" << strlen(pmessage) << endl;

        strcpy_s(pmessage,sizeof pmessage,text);
    }

    //destructor prototype
    ~CMessage();
};

CMessage::~CMessage()
{
    cout << "destructor called."
        << endl;

    delete[] pmessage;
}

int main()
{
    std::cout << "Hello World!\n";
    CMessage Motto("a miss is as good as a smile.");
    Motto.ShowIt();
}


What I have tried:

I think it is related with wide character functions.
Posted
Updated 17-Jan-20 3:47am
v2

You need to use the length of the source message, not the size of the pointer:
C++
int msglen = strlen(text) + 1; // the length required to store the message
if (pmessage != nullptr)
    delete[] pmessage; // delete any previous buffer
pmessage = new char[msglen];
cout << " the length of text is:" << strlen(text) << endl;
// the length of pmessage is the same + 1 byte for the null character
strcpy_s(pmessage, msglen, text);
 
Share this answer
 
Comments
[no name] 12-Jan-20 4:41am    
Makes sense to me, +5
Southmountain 12-Jan-20 12:06pm    
Highly appreciated your help.it really cleared my clouds...
Richard MacCutchan 12-Jan-20 12:19pm    
You are welcome.
Just change from sizeof to strlen(), sizeof is evaluated in compile time. sizeof a pointer either gives 4 bytes(32-bit) or 8 bytes(64-bits) and length of your source text is longer than 8 bytes. strlen() is done during runtime. And set the 1st char of pmessage to null character to mean zero length because pmessage is point to a new array with random values. Or you can memset to zero-initialize the whole array. strlen() is checking for null character to determine the string length.

C++
//constructor
CMessage(const char* text = "default message")
{
    if(pmessage) delete [] pmessage; // deallocate pmessage if it is not null, else mem-leak
    pmessage = new char[strlen(text) + 1];
    pmessage[0] = '\0'; // set 1st char to null character to signify 0 length.
    cout << " the length of text is:" << strlen(text) << endl;
    cout << " the length of pmessage is:" << strlen(pmessage) << endl;

    strcpy_s(pmessage,strlen(pmessage)+1,text);
}
 
Share this answer
 
v8
Comments
Richard MacCutchan 12-Jan-20 3:03am    
This is wrong, the length given by pmessage will be 1 byte so strcpy_s will fail.
I could explain why your C-style coding is not working, but I will just provide a simpler C++ version instead. (this is off the top of my head - I did not compile this, so I expect at least one error)

#include <iostream>
#include <string>
using namespace std;

class CMessage {
private:
    std::string message;

public:
    void ShowIt()
    {
        cout << endl << message.c_str();
    }

    //constructor
    CMessage(const std::string& text = "default message")
    {
        cout << " the length of text is:" << text.size() << endl;
        cout << " the length of message is:" << message.size() << endl;

        message = text;
    }

    //destructor prototype
    ~CMessage();
};

CMessage::~CMessage()
{
    cout << "destructor called."
        << endl;
}



int main()
{
    std::cout << "Hello World!\n";
    CMessage Motto("a miss is as good as a smile.");
    Motto.ShowIt();
}


Take note that if you are using the keyword 'new' in C++, then you are probably doing something wrong. There are valid reasons for using pointers and allocating memory with 'new' in C++, but your simple program is not one of them.

There are reasons why you might want to pass a constant c-string pointer to methods in C++, but I leave that up to you to figure out.
 
Share this answer
 
std::string together with std::cout provide (correctly) all the functionality your code should.
Hence, in order to brush your C++ skills, give a look at their documentation.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900