Why *((type **)&p) = new_pointer is better?

Programming, for all ages and all languages.
Post Reply
harvey
Posts: 23
Joined: Tue Nov 01, 2011 11:07 am

Why *((type **)&p) = new_pointer is better?

Post by harvey »

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?
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.
User avatar
NickJohnson
Member
Member
Posts: 1249
Joined: Tue Mar 24, 2009 8:11 pm
Location: Sunnyvale, California

Re: Why *((type **)&p) = new_pointer is better?

Post by NickJohnson »

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.
harvey
Posts: 23
Joined: Tue Nov 01, 2011 11:07 am

Re: Why *((type **)&p) = new_pointer is better?

Post by harvey »

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 saw this somewhere in Linux kernel some time ago and I can't remember exactly where it is. I need some time to search:-)
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.
User avatar
Jezze
Member
Member
Posts: 395
Joined: Thu Jul 26, 2007 1:53 am
Libera.chat IRC: jfu
Contact:

Re: Why *((type **)&p) = new_pointer is better?

Post by Jezze »

Perhaps just a very bad programmer? =)
Fudge - Simplicity, clarity and speed.
http://github.com/Jezze/fudge/
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Why *((type **)&p) = new_pointer is better?

Post by Solar »

Jezze wrote:Perhaps just a very bad programmer? =)
A possibility never to be ruled out easily. 8)
Every good solution is obvious once you've found it.
harvey
Posts: 23
Joined: Tue Nov 01, 2011 11:07 am

Re: Why *((type **)&p) = new_pointer is better?

Post by harvey »

berkus wrote:You might be reading it wrong.
Maybe...
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.
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Why *((type **)&p) = new_pointer is better?

Post by AJ »

Hi,
Jezze wrote:Perhaps just a very bad programmer? =)
For me, declarations like:

Code: Select all

int *p = NULL, n = xxx;
always ring alarm bells anyway, even if, as in your example above, it was intended that n is not a pointer.

Cheers,
Adam
harvey
Posts: 23
Joined: Tue Nov 01, 2011 11:07 am

Re: Why *((type **)&p) = new_pointer is better?

Post by harvey »

AJ wrote:Hi,
Jezze wrote:Perhaps just a very bad programmer? =)
For me, declarations like:

Code: Select all

int *p = NULL, n = xxx;
always ring alarm bells anyway, even if, as in your example above, it was intended that n is not a pointer.

Cheers,
Adam
yes, n is a int, so I added & and the focus is actually on the second statement.
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.
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Why *((type **)&p) = new_pointer is better?

Post by AJ »

Hi,
AJ wrote:even if, as in your example above, it was intended that n is not a pointer.
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.

Cheers,
Adam
Post Reply