C Myth !

Hi Folks!

While in Sofitel, sitting in the luxurious and majestic Halls, having the Microsoft Candies & Coffies, Ramnaresh ( a geek looking Indian from Microsoft Core OS ) was in fornt of me and asked me this thing,

Suppose you have a C struct like this one,

struct Example
{
public:
/* members */
Example * ptrExample;
/* members */
};

While he was writing this to a filling pad I thought Huh so easy struct. He then instantly asked me to write a method as follows,

ADDRESS FindStartAddress(ADDRESS pMAddress)
{
ADDRESS startAddressStruct = null;
/*Code*/
return startAddressStruct;
}

the Parameter to this method ADDRESS pMAddress is the in memory address of any member of this struct. Now the problem was to return the starting address of struct in memory when you only know address of 1 of its members (which member we dont know).

At that time i gave a very ambiguous answer and very briskly too and try to indulge him in some pointer arithmetic, at my answer he said "You answer is ambiguous, and doubtful". Till now I have pondered over this question a lot, and now I think it is not solvable. This was last question of his interview with me, he left and I went to ever pathetic general people!

If any one can solve this I will def. rank him higher in C than me 😉

P.S: Me at Microsoft Interviews and Ramnaresh in Core OS Division, wrote the TCP IP Stack for MS IE.

Regards,
I, Haroon

Advertisements
  1. November 20, 2005 at 6:29 am

    I just came across your blog and wanted to
    drop you a note telling you how impressed I was with
    the information you have posted here.

    If you have a moment, please visit my site:
    rent to own computers
    It covers rent to own computers related stuff.
    I send you warm regards and wish you continued success.

  2. November 21, 2005 at 4:41 pm

    Intelligent people may forget their mistakes but they always remember the lesson they learn from their mistakes…

    keep up the good work
    cheers..

  3. ashok pathak
    January 15, 2007 at 10:35 am

    not a big deal just look linux kernal
    container_of comment basically given any member
    of struct find offset of that member and sbtart offset from given address it will return address of strct in whcih this member is embedded

    container_of – cast a member of a structure out to the containing structure
    282 * @ptr: the pointer to the member.
    283 * @type: the type of the container struct this is embedded in.
    284 * @member: the name of the member within the struct.
    285 *
    286 */
    287 #define container_of(ptr, type, member) ({ \
    288 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
    289 (type *)( (char *)__mptr – offsetof(type,member) );})
    290

  4. Touseef
    December 13, 2007 at 11:05 am

    Hi all,

    Ashok thanks for sharing that great trick to get the address of struct.
    But Haroon’s the problem statement mention that only address of the member is passed and we do not know the member itself. I could not find the solution to that and I think there is no solution. What you say people?

    And how is going Haroon?

    Cheers,
    Touseef

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: