Home > Back-end >  C pointer release problem
C pointer release problem

Time:09-18

the great spirit is good, have so a member function:
 
Char * CppsFile: : GetDeleteCmd (QString strFileName)
{
Char * CMD=(char *) malloc (sizeof (char) * 200);

QString strCmd=QString (" rm ") + "" + strFileName;
String cstrCmd=strCmd. ToStdString ();
Strcpy (CMD, cstrCmd c_str ());

Return CMD.
}

want to ask, I directly to the CMD when the return value to return, the pointer will also need to release, if needed, how to release?

CodePudding user response:

In this function does not need to release the
In the function of the caller, no longer use the release when the return value

CodePudding user response:

In c + +, why not use share_ptr
Why not use string

CodePudding user response:

You release in this function, the return is a null pointer, in a timely manner when you use them to release

CodePudding user response:

After the function call, the release of
Char * PTR=GetDeleteCmd ();
The delete PTR.
PTR=nullptr;

CodePudding user response:

The building Lord have realized the design function is bad!

CodePudding user response:

All have already used a QString, why not simply point? Send a QString reference into,
Void CppsFile: : GetDeleteCmd (QString strFileName, QString & amp; Out);

Before the building this kind of writing, the c language library seems to be pretty much

CodePudding user response:

In this class CppsFile, increase pointer to member variables, the memory allocated to you, when using direct take this variable is ok,

CodePudding user response:

Returns a QString can!
QString support implicit sharing, the efficiency is good

CodePudding user response:

Who is also very intuitive idea created earlier who released, [modular also convenient]
So, some very basic function is to accept like this a memory region, into his own prepare the data returned

As university canteen meal, did you fill dish aunt, she just to give you up a bowl of rice, you don't pass a bowl can not eat,

Aunt, of course, also can be like you with design, apply for a plate/limited resources, and rice bowl to you, you've finished eating don't put the dishes/release it somewhere, but back to the dormitory, or still somewhere in the casual, the dining room dish [resources has been in use cannot be applied for]

CodePudding user response:

1. Is a pointer to pointer declaration marked it as far as possible, it is recommended that statement into a char * pCmd;
2. Even if the malloc a memory space, the best initialization to this space, you can use memset: memset (CMD, 0 x00, sizeof (char) * 200);
3. The function definition there is no problem, I just said the release of memory operations are done by the caller, just free CMD can, and the caller must remember to release, or a memory leak occurs,

CodePudding user response:

The building Lord coding habits is very bad
1, remember to use malloc place must be used to release free (' quickly understand: borrow money (memory), ' 'remember also),
2, the parameter list suggest using const QString & amp; StrFileName, promote efficiency,
3, if a member function is to construct a delete command why not write a delete function, this function module is clear,
Carefully understand the scope and Pointers, have time to see the effective c + +,

CodePudding user response:

In your internal don't need to release this function, use the return value, confirming the string will not continue to use it needs to release,
2020, why not use from

CodePudding user response:

Where calls the function return value using the up and release, not within the function release, or it has no return value

CodePudding user response:

Be, otherwise cause a memory leak

CodePudding user response:

Need to release, on the outside of the function after using the return value, you can call free to release the allocated memory space,
Recommend the CMD to function as a parameter to obtain a result, instead of return value returns, so better

CodePudding user response:

The function returns a QString type data need not make so trouble
  • Related