Thread: Why won't this hardcoded dererfence work?

  1. #1
    Old Fashioned
    Join Date
    Nov 2016
    Posts
    137

    Question Why won't this hardcoded dererfence work?

    I am once again experimenting with C syntax and I wrote this:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct person
    {
        char name[50];
        unsigned int age;
        char hc[10];
    } PERSON;
    int main(void)
    {
        PERSON *myPerson = malloc(sizeof(PERSON));
    
        strcpy((char*)myPerson,"Test");
        strcpy((char*)myPerson+56, "Brown");
    
        printf("Name: %s\n",myPerson);
        printf("Hair Color: %s\n", *((char*)myPerson+56));
        getchar();
        return EXIT_SUCCESS;
    
    }
    Basically, 56 bytes into myPerson is the confirmed compiled location where myPerson->hc should be. It would normally be 54 bytes in but apparently there are 2 bytes of padding.

    However, if I replace the myPerson+56 with myPerson->hc it works, if I do myPerson+56 casted to char* then the "Brown" text does not appear in the printf.

    The disassembly (x64) is slightly different between the two but both times it does actually have the same effect of going 56 bytes into the struct. Again, I know this is "not a good idea" but note that I am simply using this as an experiment to play with the memory capabilities of C. Thank you.
    If I was homeless and jobless, I would take my laptop to a wifi source and write C for fun all day. It's the same thing I enjoy now!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Asymptotic
    However, if I replace the myPerson+56 with myPerson->hc it works, if I do myPerson+56 casted to char* then the "Brown" text does not appear in the printf.
    Fix your printf calls:
    Code:
    printf("Name: %s\n", myPerson->name);
    printf("Hair Color: %s\n", myPerson->hc);
    As it stands, *((char*)myPerson+56) is a char, but your printf format specifier says you want to print a string.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why does this work?
    By Syscal in forum C Programming
    Replies: 4
    Last Post: 10-09-2013, 03:46 AM
  2. How's this work?
    By TheNovice in forum C++ Programming
    Replies: 10
    Last Post: 12-24-2009, 08:57 AM
  3. get hardcoded hd serial with c++
    By naughtywizard in forum C++ Programming
    Replies: 16
    Last Post: 01-03-2008, 09:56 AM
  4. Replies: 12
    Last Post: 03-10-2005, 07:48 PM
  5. my function doesn't work! it should work
    By Unregistered in forum C Programming
    Replies: 13
    Last Post: 05-02-2002, 02:53 PM

Tags for this Thread