Click here to Skip to main content
16,016,783 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Managing Memory Pin
John R. Shaw13-Aug-05 19:49
John R. Shaw13-Aug-05 19:49 
GeneralRe: Managing Memory Pin
S. Senthil Kumar14-Aug-05 3:45
S. Senthil Kumar14-Aug-05 3:45 
GeneralCAsyncSocket Pin
xSoptik13-Aug-05 10:18
xSoptik13-Aug-05 10:18 
GeneralRe: CAsyncSocket Pin
S Douglas15-Aug-05 1:47
professionalS Douglas15-Aug-05 1:47 
GeneralWindow sizing by actual string and font Pin
xSoptik13-Aug-05 9:53
xSoptik13-Aug-05 9:53 
GeneralRe: Window sizing by actual string and font Pin
PJ Arends13-Aug-05 10:07
professionalPJ Arends13-Aug-05 10:07 
General*char problem Pin
Axonn Echysttas13-Aug-05 9:09
Axonn Echysttas13-Aug-05 9:09 
GeneralRe: *char problem Pin
PJ Arends13-Aug-05 10:04
professionalPJ Arends13-Aug-05 10:04 
Axonn Echysttas wrote:
void Test (long lSomeValue)
{
char *cValue = (char*)malloc(15);
char *cText = (char*)malloc(50);


cText and cValue are pointers. Right now they point to the memory locations returned by the malloc function.

Axonn Echysttas wrote:
cText = "The number is: ";

cText now points to a different memory location then it did before. It now points to a static memory containing the text "The number is: ". To actually copy the text into the memory that was allocated with malloc you have to use strcpy()
strcpy (cText, "The number is: ");



Axonn Echysttas wrote:
_ltoa(lSomeValue, cValue, 10);
strcat(cText, cValue);


Here the strcat function will fail, most likely with an access violation, because the memory pointed to by cText is readonly memory.

To see this happen, run the program in the debugger, and step through the Test() function, watching the values of cValue and CText as you go. The debugger will give you the memory address pointed to be the pointers, followed by the text contained in that memory. Note the addresses that are assigned by the malloc calls. Now see how when you call cText = "The Number is: "; the address that cText points to changes well as the text, but when you call _ltoa(lsomevalue, cValue, 10); the address that cValue points to remains the same, but the text at that address changes.





"You're obviously a superstar." - Christian Graus about me - 12 Feb '03

"Obviously ???  You're definitely a superstar!!!" - mYkel - 21 Jun '04

"There's not enough blatant self-congratulatory backslapping in the world today..." - HumblePie - 21 Jun '05

Within you lies the power for good - Use it!
GeneralRe: *char problem Pin
Axonn Echysttas13-Aug-05 10:51
Axonn Echysttas13-Aug-05 10:51 
GeneralRe: *char problem Pin
John R. Shaw13-Aug-05 20:44
John R. Shaw13-Aug-05 20:44 
GeneralRe: *char problem Pin
Axonn Echysttas14-Aug-05 19:17
Axonn Echysttas14-Aug-05 19:17 
QuestionHow to change the icon of program by Visual C++ 6.0 Pin
Victor Lin13-Aug-05 5:45
Victor Lin13-Aug-05 5:45 
AnswerRe: How to change the icon of program by Visual C++ 6.0 Pin
Achim Klein13-Aug-05 7:43
Achim Klein13-Aug-05 7:43 
AnswerRe: How to change the icon of program by Visual C++ 6.0 Pin
cpprules13-Aug-05 12:55
cpprules13-Aug-05 12:55 
QuestionWhere I make mistake in getting input boxes from html page? Pin
Alex Shapovalov13-Aug-05 4:49
professionalAlex Shapovalov13-Aug-05 4:49 
GeneralHexEncoder class to CString Pin
Member 207257813-Aug-05 4:07
Member 207257813-Aug-05 4:07 
GeneralRe: HexEncoder class to CString Pin
Jose Lamas Rios13-Aug-05 7:14
Jose Lamas Rios13-Aug-05 7:14 
GeneralRe: HexEncoder class to CString Pin
Member 207257813-Aug-05 20:24
Member 207257813-Aug-05 20:24 
GeneralRe: HexEncoder class to CString Pin
Member 207257814-Aug-05 6:59
Member 207257814-Aug-05 6:59 
GeneralRe: HexEncoder class to CString Pin
Member 207257816-Aug-05 3:29
Member 207257816-Aug-05 3:29 
GeneralRe: HexEncoder class to CString Pin
Jose Lamas Rios17-Aug-05 4:50
Jose Lamas Rios17-Aug-05 4:50 
GeneralRe: HexEncoder class to CString Pin
Member 207257819-Aug-05 4:56
Member 207257819-Aug-05 4:56 
GeneralToolbar Pin
BicycleTheif13-Aug-05 3:07
BicycleTheif13-Aug-05 3:07 
Generalproble with hooks Pin
gamitech13-Aug-05 2:18
gamitech13-Aug-05 2:18 
Generaltopic for project Pin
V.G13-Aug-05 2:15
V.G13-Aug-05 2:15 

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.