c++ Copy String Questions

Asked 2 years ago, Updated 2 years ago, 39 views

Header File Mystring.h

class CMyString

{

public:

CMyString(void);

~CMyString(void);

private:

char* m_pszdata; // memory storage string

intm_nlength; // string length

public:

int SetString(const char* pszparam);
const char* GetString();
void Release();

};

Class definition cpp

CMyString::CMyString(void)

: m_pszdata(NULL)
, , m_nlength(0)

{ }

CMyString::~CMyString(void)

{

Release();

}

int CMyString::SetString(const char* pszparam)

{

Release();
static char str[7] = "Error!"; // Declare string variables to output on error
m_nlength = strlen(pszparam); // Save string length value OK
m_pszdata = new char[m_nlength+1]; // Dynamic memory allocation as long as string (+1)

If(m_nlength==0 || pszparam == NULL) m_pszdata = str; // Load Error statement when string length is 0 or string is NULL
else //strcpy_s(m_pszdata, sizeof(char)*(m_nlength+1),pszparam); 
    // Copy the string of the parameter to m_pszdata dynamic memory (m_nlength + 1 because you need to copy up to NULL characters)
    m_pszdata = (char*)pszparam; // const char* → char* forced conversion followed by storing memory address in char* format m_pszdata
return 0;

}

const char* CMyString::GetString()

{

return m_pszdata;

}

void CMyString::Release() {

if(m_pszdata!= NULL)
delete[] m_pszdata;

m_pszdata = NULL;
m_nlength = 0;

}

Main cpp

int _tmain(int argc, _TCHAR* argv[]) {

CMyString strData;
strData.SetString("hello");
cout << strData.GetString() << endl;
strData.SetString("byegg4");
cout << strData.GetString() << endl;
return 0;

Copy a string received as a parameter from a class-defined cpp code to m_pszdata I tried to implement it.

strcpy_s(m_pszdata, sizeof(char)*(m_nlength+1), pszparam) as shown in the correct part;
With this implementation, there is no problem calling the Release method

The way I implemented it

m_pszdata = (char*)pszparam; // const char* → char* After forced conversion, save memory address in char* format m_pszdata

If implemented in this way, errors such as photos will occur...

I checked if the string didn't contain NULL characters properly, but I don't think that's the problem. What is the problem with the way I implemented it?

c++ string

2022-09-21 12:14

1 Answers

strcpy_s(m_pszdata, sizeof(char)*(m_nlength+1),pszparam); With this implementation, there is no problem calling the Release method.

In the way I implemented it, m_pszdata = (char*)pszparam; // const char* → char* After forced conversion, save memory address in char* format m_pszdata

==> m_pszdata has already been allocated memory as new, but I think it will be a problem because I allocated a pointer. I think I need to copy the memory with the m_pszdata assigned by the pszparam pointer. If only pointer addresses are obtained from SetString, data loss may occur when the called function is terminated or deleted, and normal data cannot be imported into GetString, and segment errors may occur


2022-09-21 12:14

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.