Jump to content

*EASY* Question for you all

Featured Replies

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) ?

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?

  • Author

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. :)

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.

  • Author

I am closer to the Pasadena area. Got the problem solved yet? :D

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.

  • Author

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

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

  • Author

If your interested I will come up with some more. I can code better than you! :P

Originally posted by Doc

If your interested I will come up with some more. I can code better than you! :P

 

hehe...sure...i like these challenges.

 

AND IM STILL NOT CHANGING MY QUOTE!! :cool:

  • 4 years later...

Archived

This topic is now archived and is closed to further replies.

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.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.