Hi,
I had a bit of compile problems in casting a pointer.
Could anyone point out my misunderstandin g of the concept here?
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
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
=============== ===========
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
Comment