Click here to Skip to main content
15,881,139 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionPassing / modifying char* to/ by function. Pin
Vaclav_16-Dec-18 16:33
Vaclav_16-Dec-18 16:33 
AnswerRe: Passing / modifying char* to/ by function. Pin
CPallini16-Dec-18 21:23
mveCPallini16-Dec-18 21:23 
AnswerRe: Passing / modifying char* to/ by function. Pin
Richard MacCutchan16-Dec-18 22:30
mveRichard MacCutchan16-Dec-18 22:30 
GeneralRe: Passing / modifying char* to/ by function. Pin
CPallini16-Dec-18 22:56
mveCPallini16-Dec-18 22:56 
GeneralRe: Passing / modifying char* to/ by function. Pin
Richard MacCutchan16-Dec-18 23:01
mveRichard MacCutchan16-Dec-18 23:01 
GeneralRe: Passing / modifying char* to/ by function. Pin
Vaclav_17-Dec-18 3:26
Vaclav_17-Dec-18 3:26 
GeneralRe: Passing / modifying char* to/ by function. Pin
Richard MacCutchan17-Dec-18 3:50
mveRichard MacCutchan17-Dec-18 3:50 
GeneralRe: Passing / modifying char* to/ by function. Pin
leon de boer17-Dec-18 18:48
leon de boer17-Dec-18 18:48 
Personally code in this form I would not allow in our code base as it will lead to bleeding memory when used by others.

It is not clear that the function allocates memory, to anyone other than yourself and not being aware the function setups to use the char* pointer return and lose it ... take this basic code
CLASS_LCM1602 myCLASS_LCM1602 = .. some initialization;

/* now use your function */
std::cout << myCLASS_LCM1602.BuildMessage("Demo my Title", 10) << "\n";

That bleeds memory and the whole setup of the function encourages it because people will be tempted to feed the return char* value into functions.

For my 2 cents I would force you to use this interface for what you wanted to do .. Joe below has made the same comment.
int CLASS_LCM1602::BuildMessage(const char *title, int value, char* buffer, int bufSize)

It is much cleaner in that they pass you a buffer to fill and it's size and you return how many characters you put in the buffer.
Both you and they can be safe about handling the string becasue all sizes are known both directions.
If they want to allocate a buffer they can but they then know they have responsibility to dispose it.
It is also more flexible because you can use it on temporary stack allocated buffers .. something like this is a perfectly valid temp buffer with no allocate required.
char tempBuf[256];

As an example the actual Windows framework MFC is copying does exactly that Smile | :)
GetWindowTextA function | Microsoft Docs[^]
In vino veritas


modified 18-Dec-18 0:59am.

AnswerRe: Passing / modifying char* to/ by function. Pin
Joe Woodbury17-Dec-18 8:20
professionalJoe Woodbury17-Dec-18 8:20 
GeneralRe: Passing / modifying char* to/ by function. Pin
Vaclav_17-Dec-18 11:20
Vaclav_17-Dec-18 11:20 
Questioneliminating lag from a chess clock Pin
Alexander Kindel16-Dec-18 4:23
Alexander Kindel16-Dec-18 4:23 
AnswerRe: eliminating lag from a chess clock Pin
Daniel Pfeffer16-Dec-18 4:43
professionalDaniel Pfeffer16-Dec-18 4:43 
GeneralRe: eliminating lag from a chess clock Pin
Alexander Kindel16-Dec-18 7:09
Alexander Kindel16-Dec-18 7:09 
GeneralRe: eliminating lag from a chess clock Pin
Randor 16-Dec-18 11:56
professional Randor 16-Dec-18 11:56 
GeneralRe: eliminating lag from a chess clock Pin
Alexander Kindel16-Dec-18 14:51
Alexander Kindel16-Dec-18 14:51 
GeneralRe: eliminating lag from a chess clock Pin
Randor 16-Dec-18 23:09
professional Randor 16-Dec-18 23:09 
GeneralRe: eliminating lag from a chess clock Pin
Alexander Kindel18-Dec-18 14:41
Alexander Kindel18-Dec-18 14:41 
GeneralRe: eliminating lag from a chess clock Pin
Daniel Pfeffer18-Dec-18 20:27
professionalDaniel Pfeffer18-Dec-18 20:27 
QuestionWiFi Notifications [SOLVED] Pin
Richard Andrew x6414-Dec-18 16:13
professionalRichard Andrew x6414-Dec-18 16:13 
QuestionVC++ Word automation issues on Win 10 Pin
narasingubhanu14-Dec-18 2:25
narasingubhanu14-Dec-18 2:25 
AnswerRe: VC++ Word automation issues on Win 10 Pin
Richard MacCutchan14-Dec-18 4:47
mveRichard MacCutchan14-Dec-18 4:47 
GeneralRe: VC++ Word automation issues on Win 10 Pin
narasingubhanu14-Dec-18 19:04
narasingubhanu14-Dec-18 19:04 
QuestionInitializing struct (in C++) Pin
Vaclav_12-Dec-18 6:22
Vaclav_12-Dec-18 6:22 
AnswerRe: Initializing struct (in C++) Pin
leon de boer12-Dec-18 6:28
leon de boer12-Dec-18 6:28 
QuestionList HDD files in CListView Pin
_Flaviu11-Dec-18 1:22
_Flaviu11-Dec-18 1:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.