Click here to Skip to main content
15,894,896 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: point reference VS value Pin
Luc Pattyn19-Apr-09 4:37
sitebuilderLuc Pattyn19-Apr-09 4:37 
GeneralRe: point reference VS value Pin
Stuart Dootson19-Apr-09 4:56
professionalStuart Dootson19-Apr-09 4:56 
GeneralRe: point reference VS value Pin
Luc Pattyn19-Apr-09 5:00
sitebuilderLuc Pattyn19-Apr-09 5:00 
GeneralRe: point reference VS value Pin
Stuart Dootson19-Apr-09 5:04
professionalStuart Dootson19-Apr-09 5:04 
GeneralRe: point reference VS value Pin
sharion19-Apr-09 5:25
sharion19-Apr-09 5:25 
GeneralRe: point reference VS value Pin
Stuart Dootson19-Apr-09 7:15
professionalStuart Dootson19-Apr-09 7:15 
GeneralRe: point reference VS value Pin
sharion20-Apr-09 18:30
sharion20-Apr-09 18:30 
GeneralRe: point reference VS value Pin
Stuart Dootson20-Apr-09 19:40
professionalStuart Dootson20-Apr-09 19:40 
sharion wrote:
The value of point str is changed when second it is allocated memory. Do you agree?


str is only assigned to once - the other time, it is p, which is a copy of str which is assigned.


sharion wrote:
Actually, str is allocated successfully after GetMemory.When you count the memory size of str between memory allocated two times you can draw this conclusion.


Again, NO. str is assigned ONCE.

Let's go through the code, in the order that the code is executed:


int main()
{
char* str = NULL; // str is assigned the value NULL
str = new char[100]; // str is assigned a value that points to allocated memory
GetMemory(str); // The value of str is COPIED and we call GetMemory
        void GetMemory(char* p) // The parameter p is now a COPY of str
	{
		if(p != NULL) // p is not NULL, so we execute the if statement's body
		{
			delete []p; // delete p. This does not alter the VALUE of p, but it deallocates the memory pointed to by both p AND str.
			p = NULL; // Assign NULL to p. This DOES NOT ALTER THE VALUE OF str.
		}
		if(p == NULL) // p is now NULL, so we execute the if statement's body
		{
			p =  new char[1000]; // Allocate memory and assign its address to p. WE DO NOT ASSIGN THE VALUE TO str. str DOES NOT CHANGE.
			*(p+10) = 'a'; // Set a value within the allocated block of memory
		}
	} // Return to main, DISCARDING p

// We're now back in main. The value of str HAS NOT CHANGED. It now points to an unallocated area in memory
} // End


Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

GeneralRe: point reference VS value Pin
sharion21-Apr-09 18:05
sharion21-Apr-09 18:05 
GeneralRe: point reference VS value Pin
Stuart Dootson21-Apr-09 21:39
professionalStuart Dootson21-Apr-09 21:39 
GeneralRe: point reference VS value Pin
sharion22-Apr-09 16:00
sharion22-Apr-09 16:00 
QuestionProblem with headers in VS 2008 [modified] Pin
mass8518-Apr-09 10:07
mass8518-Apr-09 10:07 
AnswerRe: Problem with headers in VS 2008 Pin
Luc Pattyn18-Apr-09 10:33
sitebuilderLuc Pattyn18-Apr-09 10:33 
GeneralRe: Problem with headers in VS 2008 Pin
mass8518-Apr-09 11:08
mass8518-Apr-09 11:08 
GeneralRe: Problem with headers in VS 2008 Pin
Stuart Dootson18-Apr-09 11:36
professionalStuart Dootson18-Apr-09 11:36 
GeneralRe: Problem with headers in VS 2008 Pin
mass8518-Apr-09 11:45
mass8518-Apr-09 11:45 
GeneralRe: Problem with headers in VS 2008 Pin
Stuart Dootson18-Apr-09 11:49
professionalStuart Dootson18-Apr-09 11:49 
GeneralRe: Problem with headers in VS 2008 Pin
Luc Pattyn18-Apr-09 11:57
sitebuilderLuc Pattyn18-Apr-09 11:57 
GeneralRe: Problem with headers in VS 2008 Pin
mass8518-Apr-09 12:00
mass8518-Apr-09 12:00 
Question[Message Deleted] Pin
marcusab18-Apr-09 9:16
marcusab18-Apr-09 9:16 
AnswerRe: How to copy/convert System::Object^(unsigned char) to local variable Pin
CPallini18-Apr-09 9:54
mveCPallini18-Apr-09 9:54 
Question880129 - class of matrix of complex numbers Pin
ilostmyid218-Apr-09 9:15
professionalilostmyid218-Apr-09 9:15 
AnswerRe: 880129 - class of matrix of complex numbers Pin
CPallini18-Apr-09 9:52
mveCPallini18-Apr-09 9:52 
GeneralRe: 880129 - class of matrix of complex numbers Pin
ilostmyid218-Apr-09 16:22
professionalilostmyid218-Apr-09 16:22 
GeneralRe: 880129 - class of matrix of complex numbers Pin
CPallini19-Apr-09 8:44
mveCPallini19-Apr-09 8:44 

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.