Compile errors on casting

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • philuu12
    New Member
    • Oct 2011
    • 14

    Compile errors on casting

    Hi,

    I had a bit of compile problems in casting a pointer.
    Could anyone point out my misunderstandin g of the concept here?

    Code:
         1  #include <stdio.h>
         2  #include <stdlib.h>
         3
         4  typedef struct
         5  {
         6     char charArray[64];
         7  } mystruct;
         8
         9  int main(int argc, char* argv[])
        10  {
        11     mystruct *p1 ,*p2;
        12
        13     // Overallocate to have alignment somewhere
        14     if((p1 = (mystruct*) malloc(64 + 128 + sizeof(size_t))) == NULL)
        15          return NULL;
        16
        17     size_t   address   = (size_t)p1 + 128 + sizeof(size_t);
        18
        19     p2 = (mystruct *)(address - (address % 128));
        20
        21     *((mystruct *)p2-1) = (mystruct ) p1;   // compile errors
    Compile errors:
    =============== ===========
    52 unix[**]/hw2% g++ hw2.cc
    hw2.cc: In function 'int main(int, char**)':
    hw2.cc:21: error: no matching function for call to 'mystruct::myst ruct(mystruct*& )'
    hw2.cc:7: note: candidates are: mystruct::mystr uct()
    hw2.cc:7: note: mystruct::mystr uct(const mystruct&)
    -------------------------

    Why does the casting at line 21 have problems?

    Phil
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Code:
    *((mystruct *)p2-1) = (mystruct ) p1;
    OK so p1 is a pointer to a mystruct. Therefore, (mystruct)p1 is a mystruct.

    Now how does the compiler convert a single address into a mystruct variable with a char array inside?? Answer: it can't.

    In C++ you could write a mystruct conversion constructor to do this but since you are in C, you are dead.

    Comment

    • philuu12
      New Member
      • Oct 2011
      • 14

      #3
      How do I resolve this in C? Could I type-cast the p1 (at line 21) to (mystruct *).

      I tried, but it didn't work either.

      P.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Code:
        *((mystruct *)p2-1) = (mystruct ) p1;
        Let's look deeper. p2 is a concocted integer value so that

        Code:
        (mystruct*)p2
        tells the compiler to treat this as a mystruct variable address. Then this:

        Code:
        (mystruct *)p2-1
        to subtract the sizeof 1 mystruct variable from that concocted integer value. Then this:

        Code:
        *((mystruct *)p2-1)
        dereferences the address to the location pointed at by the integer value in p2 that is beign treated as an address. Then this:

        Code:
        *((mystruct *)p2-1) = (mystruct ) p1;
        says to assign the mystruct variable on the right to the mystruct variable on the left.

        Now we know that te compiler can't make a mystruct vriable out of an address. It also cannot assign one mystruct varable to another. All it can do is cpy the memory of one mystruct variable to the other. All members are now duplicated. C does not do memberwise assignment.

        S you need to write a function that assigns one mystruct variable to another:

        Code:
        void Assign(mystruct* left, mustruct* right);
        And inside tthis function you assign the members of the right argument to the eft argument. Note: now there is no cast required.

        Like this:

        Code:
        Assign(&p1[3], &p1[2]);

        Comment

        • philuu12
          New Member
          • Oct 2011
          • 14

          #5
          Thanks for the explanation, weaknessforcats .
          Regards,

          PL

          Comment

          Working...