Jump to content

*EASY* Question for you all


Doc

Recommended Posts

Hello. I recently signed up here and thought I would present a question for my first post. Good Luck. If you have troubles understanding any parts, just ask and I will be glad to explain. This is based on intels x86 architecture. It is NOT a program. You just assign the values and solve.

 

void ***q=malloc(8);

 

*q=q;

 

*((long long *)*q)=0x012345678abcdef;

 

what is the value of (int)*((char *)q) ?

Link to comment
Share on other sites

Originally posted by Doc

Hello. I recently signed up here and thought I would present a question for my first post. Good Luck. If you have troubles understanding any parts, just ask and I will be glad to explain. This is based on intels x86 architecture. It is NOT a program. You just assign the values and solve.

 

void ***q=malloc(8);

 

*q=q;

 

*((long long *)*q)=0x012345678abcdef;

 

what is the value of (int)*((char *)q) ?

 

hmm...i know it has to do with the endian system(little?)

 

isnt it something like 42?

Link to comment
Share on other sites

No the answer is not 42. Here let me break the first part of it down for you.

 

Its cast has an int. (int)

void ***q = pointer to pointer to pointer to void.

malloc(8) = pointer to 8 writable bytes.

"*q=q" = set the first 4 of those bytes to point to themselves.

 

I hope that helped some. :)

Link to comment
Share on other sites

Originally posted by Doc

No the answer is not 42. Here let me break the first part of it down for you.

 

Its cast has an int. (int)

void ***q = pointer to pointer to pointer to void.

malloc(8) = pointer to 8 writable bytes.

"*q=q" = set the first 4 of those bytes to point to themselves.

 

I hope that helped some. :)

 

lol....duh...somtimes im just stupid :D

 

 

what part of houston u from? im from the missouri city/sugarland area.

Link to comment
Share on other sites

like you said...

 

1. The malloc reserves 8 bytes...int64

2. Sets the first pointer equal to the original pointed to value made by malloc.

3. The q pointer is dereferenced 3 times, so I think the malloced memory is set to the value 0x012345678abcdef

returned value:

 

char * just recasts it, * dereferences once, so by statement 2, it contains what ever q contained as a char value. So, taking the last char of the int64 value (ef) and by casting it to (int) at the end, you only take the ef and make it signed... which is EF, or neg 17?

 

AHHHHHHHHHHH...brain freeze...i was reading my political science book while thinking of this at the same time...and it dawned to me..but am I right??

 

correct me if not :)

 

btw, im 20 years old.

Link to comment
Share on other sites

Your very close. Nicey done. Here is the full soultion.

 

(int) - casts an int

void ***q = pointer to pointer to pointer to void.

malloc(8) = pointer to 8 writable bytes.

"*q=q" = set the first 4 of those bytes to point to themselves.

Like if the bytes are at 0x11223344, the list of bytes will go 0x44,0x33,0x22,0x11,?,?,?,?.

"*((long long *)*q)" = retrieve that pointer.

Then cast is as a pointer to a long long, then derefrenced.

At that point, q = *q, because we did *q=q

So it's equivalent to "*((long long*)q)", and the "0x012345678abcdef;" stores that 64-bit hex value into the memory location.

Those 8 bytes are set to 0xef,0xcd,0xab,0x78,0x56,0x34,0x12 or something to that effect.

First Byte = 0xef, so *((char*)q) = (char)0xef

255=-1,255=-2,etc.

0xef=239

 

239=-16

Link to comment
Share on other sites

Originally posted by Doc

Your very close. Nicey done. Here is the full soultion.

 

(int) - casts an int

void ***q = pointer to pointer to pointer to void.

malloc(8) = pointer to 8 writable bytes.

"*q=q" = set the first 4 of those bytes to point to themselves.

Like if the bytes are at 0x11223344, the list of bytes will go 0x44,0x33,0x22,0x11,?,?,?,?.

"*((long long *)*q)" = retrieve that pointer.

Then cast is as a pointer to a long long, then derefrenced.

At that point, q = *q, because we did *q=q

So it's equivalent to "*((long long*)q)", and the "0x012345678abcdef;" stores that 64-bit hex value into the memory location.

Those 8 bytes are set to 0xef,0xcd,0xab,0x78,0x56,0x34,0x12 or something to that effect.

First Byte = 0xef, so *((char*)q) = (char)0xef

255=-1,255=-2,etc.

0xef=239

 

239=-16

 

ah...almost had it... :)

 

hehe..off to bed i go

Link to comment
Share on other sites

  • 4 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.