Hi,
I'm reading kernel codes and saw some strange assigning statements like:
int *p = NULL, n = xxx;
*((int **)&p) = &n;
Why not to directly assign like
p = &n;
Is there any special reason for this?
Why *((type **)&p) = new_pointer is better?
Why *((type **)&p) = new_pointer is better?
I'm writing a toy os and my current goal is to resemble, simplify and understand the linux kernel. In the early stage, it's designed to be a UP/UMA preemptive kernel and will be extended to support SMP/NUMA etc in the future.
- NickJohnson
- Member
- Posts: 1249
- Joined: Tue Mar 24, 2009 8:11 pm
- Location: Sunnyvale, California
Re: Why *((type **)&p) = new_pointer is better?
Perhaps this is an attempt to force p to not be optimized out of the stack frame or to force a write to memory. It's particularly odd that &p is being cast to int**, considering that it already is that type. I'm assuming you found this in Linux: where did you find it? It may be part of a more elaborate hack.
Re: Why *((type **)&p) = new_pointer is better?
I saw this somewhere in Linux kernel some time ago and I can't remember exactly where it is. I need some time to search:-)NickJohnson wrote:Perhaps this is an attempt to force p to not be optimized out of the stack frame or to force a write to memory. It's particularly odd that &p is being cast to int**, considering that it already is that type. I'm assuming you found this in Linux: where did you find it? It may be part of a more elaborate hack.
I'm writing a toy os and my current goal is to resemble, simplify and understand the linux kernel. In the early stage, it's designed to be a UP/UMA preemptive kernel and will be extended to support SMP/NUMA etc in the future.
Re: Why *((type **)&p) = new_pointer is better?
Perhaps just a very bad programmer? =)
Fudge - Simplicity, clarity and speed.
http://github.com/Jezze/fudge/
http://github.com/Jezze/fudge/
Re: Why *((type **)&p) = new_pointer is better?
A possibility never to be ruled out easily.Jezze wrote:Perhaps just a very bad programmer? =)
Every good solution is obvious once you've found it.
Re: Why *((type **)&p) = new_pointer is better?
Maybe...berkus wrote:You might be reading it wrong.
I'll let you know if I find the place.
I'm writing a toy os and my current goal is to resemble, simplify and understand the linux kernel. In the early stage, it's designed to be a UP/UMA preemptive kernel and will be extended to support SMP/NUMA etc in the future.
Re: Why *((type **)&p) = new_pointer is better?
Hi,
always ring alarm bells anyway, even if, as in your example above, it was intended that n is not a pointer.
Cheers,
Adam
For me, declarations like:Jezze wrote:Perhaps just a very bad programmer? =)
Code: Select all
int *p = NULL, n = xxx;
Cheers,
Adam
Re: Why *((type **)&p) = new_pointer is better?
yes, n is a int, so I added & and the focus is actually on the second statement.AJ wrote:Hi,
For me, declarations like:Jezze wrote:Perhaps just a very bad programmer? =)always ring alarm bells anyway, even if, as in your example above, it was intended that n is not a pointer.Code: Select all
int *p = NULL, n = xxx;
Cheers,
Adam
I'm writing a toy os and my current goal is to resemble, simplify and understand the linux kernel. In the early stage, it's designed to be a UP/UMA preemptive kernel and will be extended to support SMP/NUMA etc in the future.
Re: Why *((type **)&p) = new_pointer is better?
Hi,
Cheers,
Adam
I'm not saying your example got it wrong. It may just be a matter of preference, but I was just saying that declaring a type and a pointer to that type in the same statement is not always the clearest way of doing it.AJ wrote:even if, as in your example above, it was intended that n is not a pointer.
Cheers,
Adam