Writing a callback function?
-
I am trying to write a simple callback function for my own purpose, not to pass into Windows API as callback arguments. What I did below gets access violation. Where did I do wrong in the below code? Thanks.
typedef void (*fC)(int* p);
bool Test(int n, fC* p)
{
int* pN = new int;
*pN = 3;
(*p)(pN); // ---- access violation 0xc0000005.
delete pN;return true;
}
void fc(int* p)
{
*p;
}void main()
{
Test(3, (fC*)&fc);
}Maxwell Chen
-
I am trying to write a simple callback function for my own purpose, not to pass into Windows API as callback arguments. What I did below gets access violation. Where did I do wrong in the below code? Thanks.
typedef void (*fC)(int* p);
bool Test(int n, fC* p)
{
int* pN = new int;
*pN = 3;
(*p)(pN); // ---- access violation 0xc0000005.
delete pN;return true;
}
void fc(int* p)
{
*p;
}void main()
{
Test(3, (fC*)&fc);
}Maxwell Chen
-
I am trying to write a simple callback function for my own purpose, not to pass into Windows API as callback arguments. What I did below gets access violation. Where did I do wrong in the below code? Thanks.
typedef void (*fC)(int* p);
bool Test(int n, fC* p)
{
int* pN = new int;
*pN = 3;
(*p)(pN); // ---- access violation 0xc0000005.
delete pN;return true;
}
void fc(int* p)
{
*p;
}void main()
{
Test(3, (fC*)&fc);
}Maxwell Chen
Maxwell Chen wrote:
bool Test(int n, fC* p)
Change to
bool Test(int n, fC p)
Maxwell Chen wrote:
Test(3, (fC*)&fc);
Change to
Test(3, fc);
:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Maxwell Chen wrote:
bool Test(int n, fC* p)
Change to
bool Test(int n, fC p)
Maxwell Chen wrote:
Test(3, (fC*)&fc);
Change to
Test(3, fc);
:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]Thanks! ;P
Maxwell Chen
-
change
bool Test(int n, fC* p)
to
bool Test(int n, fC p)
Also remove the cast.
void main()
{
Test(3, (fC*) &fc);
}He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus
Ooops! My bad ... :laugh:
Maxwell Chen
-
I am trying to write a simple callback function for my own purpose, not to pass into Windows API as callback arguments. What I did below gets access violation. Where did I do wrong in the below code? Thanks.
typedef void (*fC)(int* p);
bool Test(int n, fC* p)
{
int* pN = new int;
*pN = 3;
(*p)(pN); // ---- access violation 0xc0000005.
delete pN;return true;
}
void fc(int* p)
{
*p;
}void main()
{
Test(3, (fC*)&fc);
}Maxwell Chen
I've been looking at callback stuff of my own, and if you your code to:
typedef void (*fC)(int* p);
bool Test(int n, fC p)
{
int* pN = new int;
*pN = 3;
(p)(pN); // ---- access violation 0xc0000005.
delete pN;return true;
}
void fc(int* p)
{
*p;
}void main()
{
Test(3, (fC)fc);
}Then it works just fine... But your code doesn't look bad to me. Just more indirection than you need. fC is already a type of "pointer to a function", so why make things work with "pointer to a pointer to a function"? In the interests of science, I added these two lines to main:
fC p0 = fc; fC \*p1 = &fc;
And on the second line I get: "error C2440: 'initializing' : cannot convert from 'void (__cdecl *)(int *)' to 'void (__cdecl ** )(int *)'". This leads me to suggest that you're seeing the same behaviour as when you try to get a pointer to an array - you get the first element of the array back. This is probably a C thing that's biting you. And then you manually cast it in your original main and things blow up later... That's my theory, anyway! Iain.
In the process of moving to Sweden for love (awwww). If you're in Scandinavia and want an MVP on the payroll (or happy with a remote worker), give me a job!
-
Ooops! My bad ... :laugh:
Maxwell Chen
-
I've been looking at callback stuff of my own, and if you your code to:
typedef void (*fC)(int* p);
bool Test(int n, fC p)
{
int* pN = new int;
*pN = 3;
(p)(pN); // ---- access violation 0xc0000005.
delete pN;return true;
}
void fc(int* p)
{
*p;
}void main()
{
Test(3, (fC)fc);
}Then it works just fine... But your code doesn't look bad to me. Just more indirection than you need. fC is already a type of "pointer to a function", so why make things work with "pointer to a pointer to a function"? In the interests of science, I added these two lines to main:
fC p0 = fc; fC \*p1 = &fc;
And on the second line I get: "error C2440: 'initializing' : cannot convert from 'void (__cdecl *)(int *)' to 'void (__cdecl ** )(int *)'". This leads me to suggest that you're seeing the same behaviour as when you try to get a pointer to an array - you get the first element of the array back. This is probably a C thing that's biting you. And then you manually cast it in your original main and things blow up later... That's my theory, anyway! Iain.
In the process of moving to Sweden for love (awwww). If you're in Scandinavia and want an MVP on the payroll (or happy with a remote worker), give me a job!
Thanks! :-D
Maxwell Chen
-
I've been looking at callback stuff of my own, and if you your code to:
typedef void (*fC)(int* p);
bool Test(int n, fC p)
{
int* pN = new int;
*pN = 3;
(p)(pN); // ---- access violation 0xc0000005.
delete pN;return true;
}
void fc(int* p)
{
*p;
}void main()
{
Test(3, (fC)fc);
}Then it works just fine... But your code doesn't look bad to me. Just more indirection than you need. fC is already a type of "pointer to a function", so why make things work with "pointer to a pointer to a function"? In the interests of science, I added these two lines to main:
fC p0 = fc; fC \*p1 = &fc;
And on the second line I get: "error C2440: 'initializing' : cannot convert from 'void (__cdecl *)(int *)' to 'void (__cdecl ** )(int *)'". This leads me to suggest that you're seeing the same behaviour as when you try to get a pointer to an array - you get the first element of the array back. This is probably a C thing that's biting you. And then you manually cast it in your original main and things blow up later... That's my theory, anyway! Iain.
In the process of moving to Sweden for love (awwww). If you're in Scandinavia and want an MVP on the payroll (or happy with a remote worker), give me a job!
Iain Clarke wrote:
In the interests of science
:-D
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]