Announcement

Collapse
No announcement yet.

Like coding, try to answer this question.. if you can.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Like coding, try to answer this question.. if you can.

    Obviously this isn't a program and it will not compile it is just a question involving C and I thought some people might enjoy trying to figure it out. Anyway here goes.. and good luck :). btw... yes there is a logical solution.

    void ***q=malloc(8);
    *q=q;
    *((long long *)*q)=0x012345678abcdef;
    what is the value of (int)*((char *)q) ?

  • #2
    That's tricky...

    I think it is 48 but I'm not sure...

    I hope my upcoming programming exam will not be that hard. :D

    Comment


    • #3
      Nope. Let me give you a hint starting it out... Maybe you will be able to figure it out with some clues..

      (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.

      Comment


      • #4
        The answer will be -17, Lemme give explanation (Though I'm not C coder ) :

        void ***q=malloc(8); // not valid in other lang'es , C++ prohibits this converting void* to void***

        *q=q; //changes the values of above alocated bytes (sizeof(void**)) to address of q

        *((long long *)*q)=0x012345678abcdef; //some what cycle pointers, *q will result in address of q, dereferencing it is same as bytes (sizeof(long long*)) chunk allocated, this will assign value 0x78abcdef at same time overflowing the 4 byte memory chunk I guess

        (int)*((char *)q) // this will print 1st byte value which is -17

        But seriously any company will throw you out if you'll write this type of code :D
        Last edited by Mr_Anubis; January 26, 2012, 12:13 PM.

        Comment


        • #5
          correct lol

          Comment

          Working...
          X