Click here to Skip to main content
15,913,669 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
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 
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 
sharion wrote:
The first fact,you will find the value of *(str+10) is equal to 'a'.


That is because you first deallocated the memory that str pointed at, then allocated a block that you assigned to p. When you deallocated the memory, you handed back the block that str pointed at to the C run-time. Then, when you allocated the new memory block, it just so happened that the first free address with enough storage was the one you just gave back to the run-time.

Now consider this code example - it's the same as yours with one line of code added. That line of code, however, is enough to stop the behaviour you've seen. If you run this, the memory that str points at will not be modified after calling GetMemory.

#include <iostream>

void GetMemory(char* p)
	{
		if(p != NULL)
		{
			delete []p;
			p = NULL;
		}
		if(p == NULL)
		{
			p =  new char[1000];
			*(p+10) = 'a';
		}
	}
int main()
{
char* str = NULL;
str = new char[100];
char* str2 = new char[100];
GetMemory(str);
/* */
}


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

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 
GeneralRe: 880129 - class of matrix of complex numbers Pin
ilostmyid219-Apr-09 18:54
professionalilostmyid219-Apr-09 18:54 
GeneralRe: 880129 - class of matrix of complex numbers Pin
CPallini19-Apr-09 21:25
mveCPallini19-Apr-09 21:25 

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.