|
#include <stdio.h><br />
<br />
int *pPointer;<br />
<br />
void SomeFunction()<br />
{<br />
int nNumber;<br />
nNumber = 25; <br />
<br />
pPointer = &nNumber;<br />
}<br />
<br />
void main()<br />
{<br />
SomeFunction();
<br />
printf("Value of *pPointer: %d\n", *pPointer);<br />
}
Hi friends,
the above code is working without any problems in VC6.
But it should fail at the line printf("Value of *pPointer: %d\n", *pPointer);
because pPointer is pointing to a local variable,which is already deleted.
Appu..
"If you judge people, you have no time to love them."
|
|
|
|
|
the pointer points to an address that is not overwritten yet, so you still see 25 displayed. however, this code is not safe because if the system had given this memory area to another process, you would have accessed a memory area you don't own (which results in an access violation) !!!
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
|
|
|
|
|
toxcct wrote: the pointer points to an address that is not overwritten yet, so you still see 25 displayed.
But when we observe the value of nNumber ,it is not showing any value.
But a pointer(pPointer) is a variable which refers another variable(nNumber),
So it should crash at that point..But it is not .Why ??
Appu..
"If you judge people, you have no time to love them."
|
|
|
|
|
HI NiceNaidu
I think the reason why this is working fine is that you have declared the integer pointer (int *pPointer ) globally. So once you point it to some memory location it will keep pointing to that location untill the program terminates. So whenever you try to print its value it will print it without any problem.
Rakesh
|
|
|
|
|
no !
the pointer variable is global, that mean its content only (address stored), but not the memory pointed...
read my 1st answer to undertand the reason why it's working.
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
|
|
|
|
|
Whats the difference detween address stored and memory pointed?
Because once you assign the address of any variable to any pointer then its contents are the memory location of that variable.
Rakesh
|
|
|
|
|
Rakesh_Thakur wrote: Whats the difference detween address stored and memory pointed?
the pointer stores the memory pointed...
pointer int pointed
----- ------
| * | ------> | 25 |
----- ------
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
|
|
|
|
|
Thats what I am trying to say.
Contents of pointer are the memory location.
Contents of variable are the actual value.
nNumber is global which means it can be access only within its scope.
Once you come out of that function, scope of that variable ends which means you cannot access it or it is undeclared.
But the value stored at that memory location remains untill it has been over written.
In other words 'Memory is taken away from the variable but the momory contents are unchanged'. And as you are having a pointer to point at that mmory location, you can always get the content at that location.
Rakesh
|
|
|
|
|
Rakesh_Thakur wrote: nNumber is global which means it can be access only within its scope
that's totally wrong...
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
|
|
|
|
|
Sorry I wanted to write "The nNumber is local"
i.e.
nNumber is local which means it can be access only within its scope.
Once you come out of that function, scope of that variable ends which means you cannot access it or it is undeclared.
But the value stored at that memory location remains untill it has been over written.
In other words 'Memory is taken away from the variable but the momory contents are unchanged'. And as you are having a pointer to point at that mmory location, you can always get the content at that location
Rakesh
|
|
|
|
|
And as you are having a pointer to point at that mmory location, you can always get the content at that location
- ONLY if your process owns that memory location and it is readable. Otherwise, you get an access violation error of type 5.
People that start writing code immediately are programmers (or hackers), people that ask questions first are Software Engineers - Graham Shanks
|
|
|
|
|
toxcct wrote: the pointer variable is global, that mean its content only (address stored),
So, what does u mean by dying of a local variable??
Appu..
"If you judge people, you have no time to love them."
|
|
|
|
|
Dying of local variable means you its scope ends.
That varible is like undeclared outside its scope.
When you declare a variable, some memory location is assiged to it.
That memory location is now belongs to that variable.
And when that variable goes out of scope, that memory location is taken back from that variable i.e. the contents at that memory are still there but that memory location is now doesn't belongs to that variable.
Rakesh
|
|
|
|
|
Pointer is a global one.
But nNumber is a local one,which dies as soon as it come out of the function scope.
So, whenever we point to a variable which is already deleted,it should give an error.
Is this a problem with Microsoft compiler ??
Appu..
"If you judge people, you have no time to love them."
|
|
|
|
|
nNumber is global which means it can be access only within its scope.
Once you come out of that function, scope of that variable ends which means you cannot access it or it is undeclared.
But the value stored at that memory location remains untill it has been over written.
In other words 'Memory is taken away from the variable but the momory contents are unchanged'. And as you are having a pointer to point at that mmory location, you can always get the content at that location.
Rakesh
|
|
|
|
|
The pointer points to an address on the stack (local declared variable)
this address still exist after the function returns. When you immediatly
use the pointer to get its value there is no problem.
But if there are serveral function calls happend in between or a large object
is created on stack the data at the specific address will be lost. When
your stack size is decreased you could also get an access violation.
codito ergo sum
|
|
|
|
|
U will get some consideration in a debug build that is the allocated space and pointer accessing etc. it wont fail in some cases ( but it should fail in such scenarios).
Just try the same code in release build. it wont definitely behave like debug build.
In release build too the application error is not occuring because the mapped address in its process address space.
SaRath
"D on't blindly follow rules you read somewhere without verifying that it makes sense for your situation!"
|
|
|
|
|
SaRath C wrote: In release build too the application error is not occuring because the mapped address in its process address space
yes..u r absolutely right. It is giving some other value in release build.
Now i understood the reason.
Thanks buddies.
Appu..
"If you judge people, you have no time to love them."
|
|
|
|
|
After this statement
NiceNaiduprintf("Value of *pPointer: %d\n", *pPointer);
do this
BOOL b = IsBadReadPtr(pPointer,sizeof(int));
check the value of b... it will be false because of the invalid pointer.
SaRath
"D on't blindly follow rules you read somewhere without verifying that it makes sense for your situation!"
|
|
|
|
|
The local variable nNumber is allocated on the thread's stack. When SomeFunction() returns, its stack variables are not erased (or "deleted"), the stack pointer is just moved back to a higher address. The stack data will still be there in memory until some later code reuses that area of memory for its stack.
--Mike--
Visual C++ MVP
LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ
|
|
|
|
|
I'm currently reading some articals about Singleton.
It mentioned the following data structure is for single thread only
Class Singleton
{
public:
static Singleton * GetInstance()
{
static Singleton instance;
return &instance;
}
protected:
Singleton();
~Singleton();
}
Instead of using local static variable; using new operator to create an object will be thread-safe.
Class Singleton
{
private:
Singleton * m_pInstance;
public:
static Singleton * GetInstance()
{
if (! m_pInstance)
m_pInstance = new Singleton();
return m_pInstance;
}
protected:
Singleton();
~Singleton();
}
I can't see the real difference between these two implementation. Why the one with new operator is thread-safe?
Thanks
|
|
|
|
|
you are correct, the second sample is NOT threadsafe. Its worse there can also be a memory leak
You should protect the creation with a critical section.
But beware, it is not because the creation of your singleton is threadsafe that the data the singleton wraps will be threadsafe.
codito ergo sum
|
|
|
|
|
None of them are thread-safe, and actually chances are you would experience a threading problem with the second version sooner than with the first one.
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
|
|
|
|
|
Hi again
I hava a problem again executing an sql statment(INSERT) in my program
can anyone help me find whats wrong?
hr = S_OK;<br />
CoInitialize(NULL);<br />
<br />
try<br />
{<br />
_ConnectionPtr m_pConn;<br />
HRESULT hr =m_pConn.CreateInstance(__uuidof(Connection));<br />
if (FAILED( hr ))<br />
cout<<"Can't create an intance of ADO.Connection"<<endl;<br />
<br />
if (FAILED( m_pConn->Open(_bstr_t("Provider=Microsoft.Jet.OLEDB.4.0;Data Source =ADOTestDB.MDB"),<br />
_bstr_t( "" ),<br />
_bstr_t( "" ), <br />
adModeUnknown )))<br />
<br />
cout<<"Can't open datasource"<<endl;<br />
<br />
<br />
<br />
<br />
_bstr_t strDepId="12";<br />
_bstr_t strName="Art";<br />
_bstr_t strOfficeNo="256";<br />
_bstr_t strTel="22491303";<br />
_bstr_t strDepHead="Dr.Antonis Marcou";<br />
<br />
_bstr_t strSQL("Insert into Dept(DeptID,Name,OfficeNo,Phone,DeptHead) Values(");<br />
strSQL += strDepId + strName + strOfficeNo + strTel + strDepHead ;<br />
<br />
printf("%s\n",(LPCSTR)strSQL);<br />
<br />
m_pConn->Execute(strSQL,NULL,adExecuteNoRecords);<br />
<br />
printf("Data Added Successfully\n",(LPCSTR)strSQL);<br />
<br />
m_pConn->Close();<br />
<br />
}catch( _com_error &ce )<br />
{<br />
printf("Error:%s\n",ce.Description);<br />
<br />
}<br />
<br />
CoUninitialize();
I wrote the program almost the same way as the sample code but still is not working
the fiels in the db are
DeptID: number
Name: Text
OfficeNo: number
Phone: text
DeptHead: text
Any ideas?
thanks a lot for all the help you gave me so far
|
|
|
|
|
I Think, u r going wrong in framing the Insert Statement.
antonaras wrote: printf("%s\n",(LPCSTR)strSQL);
What is the output of the above line ???
Appu..
"If you judge people, you have no time to love them."
|
|
|
|