Doc Posted July 15, 2002 Posted July 15, 2002 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) ?
Adrian Posted July 15, 2002 Posted July 15, 2002 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?
Doc Posted July 15, 2002 Author Posted July 15, 2002 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.
Adrian Posted July 15, 2002 Posted July 15, 2002 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 what part of houston u from? im from the missouri city/sugarland area.
Doc Posted July 15, 2002 Author Posted July 15, 2002 I am closer to the Pasadena area. Got the problem solved yet?
Adrian Posted July 15, 2002 Posted July 15, 2002 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.
Doc Posted July 15, 2002 Author Posted July 15, 2002 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
Adrian Posted July 15, 2002 Posted July 15, 2002 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
Doc Posted July 15, 2002 Author Posted July 15, 2002 If your interested I will come up with some more. I can code better than you!
Adrian Posted July 15, 2002 Posted July 15, 2002 Originally posted by Doc If your interested I will come up with some more. I can code better than you! hehe...sure...i like these challenges. AND IM STILL NOT CHANGING MY QUOTE!!
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now