DDE / random errors
Malcolm Hussain-Gambles (1596) 811 posts |
I particularly like this kind of problem: “c.mhttp”, line 61: Error: <argument 1 to ’close_socket’>: implicit cast of pointer to non-equal pointer The error is in the function read_download_headers which is defined as: int read_download_headers( FILE *debug, struct Download *download_struct ) and the function call line is: close_socket is defined as: int close_socket( struct Download *download_struct ) OK so… sometimes you have to love C compilers! |
Rick Murray (539) 13840 posts |
Yeah, I hate complicated pointer stuff… But I think the compiler might have a point. …some rubbish deleted as it was wrong… I think that’s what is going on. I reserve the right to be utterly wrong. Entire books have been written in the intricacies of C’s concept of pointers. :-) PS: DISCLAIMER: This message represents the official view of the voices in my head. |
Chris Mahoney (1684) 2165 posts |
Confession: When I got these sorts of errors, I would first try swapping As time has gone on I’ve become better at figuring it out myself, but the problem in this thread has me baffled :) |
Matthew Phillips (473) 721 posts |
Given that read_download_headers and close_socket have exactly the same declaration of “struct Download *whatever” I fail to see your argument Rick. I always typedef things like this to keep down the confusion. I never have “struct” appearing in my function declarations. |
Malcolm Hussain-Gambles (1596) 811 posts |
What’s more bizarre I’m using identical function definition configurations and calling it in those functions in exactly the same way in other libraries. They work and compile fine. I think anyone who deals with pointers has to have voices in their head, isn’t that what pointers do (create voices in your head)? Appreciate the help! Especially as I’m going to have a lot more structures and pointers to structures. |
Colin (478) 2433 posts |
I wouldn’t expect the compiler to be wrong which means that you haven’t given us enough information to diagnose the problem – or typed it into your post wrong. If you didn’t copy and paste it’s easy to go code blind with your own code and see what you think you wrote rather than what you actually wrote.
Forget about that he’s wrong.
typedefs are a good idea but won’t help if your confusion is with pointers. Like Matthew I always typedef my structures it avoids accidentally missing the struct keyword in declarations – and it makes the code easier to read I think. |
Rick Murray (539) 13840 posts |
I did say I might be. Okay, fine, moving on… What is clear is that the compiler is objecting to the type of struct in the definition and what is being passed. I considered it might be a pointer to pointer issue. Clearly not. The next suggestions:
Just some ideas… |
GavinWraith (26) 1563 posts |
Pointers in C. You need them to express complex datatypes in C, of course. But actually there are lots of languages where those datatypes, any datatypes, can be described just as adequately, and a lot more easily, without pointers (or explicit memory references) of any kind. Lua is a case in point. I mention this because I suspect that over-exposure to C gets people to assume that the C way is the only way. |
Malcolm Hussain-Gambles (1596) 811 posts |
OK, that explains what typedef is. My point was more to laugh at some compiler/interpreter errors etc. Do people still use lint? I particularly like this error when using ansible: We could be wrong, but …. |
Colin (478) 2433 posts |
From what you told us that doesn’t follow at all. If you have:
Then you will get the error you describe. Without seeing the rest of your code we can’t know where you have gone wrong. |
Malcolm Hussain-Gambles (1596) 811 posts |
Hi Colin, It’s pretty much as you describe above. I assume the problem is I’m about 1/2 way through abstracting the code. e.g. if you changed read_download_headers to read_socket, write_data_to_file, open_socket amongst many functions then it compiles and works fine. So that seems to indicate a=b and a!=b depending on how the compiler “feels” at the time. I could largely accept that one is not the same as one, that would suggest I’m misunderstanding something. The conclusion I draw is that I’m totally misunderstanding how you pass pointers to a function. I’m moving to use pointers to functions everywhere at the moment and my code needs simplifying. So I’m very confused why virtually the same code works in one place and not in another, but given my programming skill not totally suprised. |
Rick Murray (539) 13840 posts |
Aha… The ampersand raises its ugly head. (struct)
So, no, the compiler is right. (^_^) |
Colin (478) 2433 posts |
I’ve numbered the lines in the above program to make it easy to refer to. At line 25, &download_struct is not a struct Download* because line 25 specifies the second parameter of read_download_headers to be a struct Download* but because line 24 declares download_struct to be a struct Download. If the second parameter to read_download_headers was a char* then you would get the error you get because a struct Download* passed to the read_download_header isn’t the char* the function expects. At line 16 of the program I’ve declared a new variable download_struct to be an int* so when it is used in close socket download_struct is no longer the struct Download* that close_socket expects and so gives an error. The easiest way to find the problem is to search for download_struct and see if you have declared a new variable with that name before calling close_socket. The problem you have is the download_struct you pass to close_socket isn’t the download_struct parameter to read_download_header. |
Colin (478) 2433 posts |
ie a pointer is the address of a structure. fred and download_struct_pointer are both struct Download* and can be used without casting in any function which has a struct Download* parameter. |
Rick Murray (539) 13840 posts |
For fun and chocolate…
|
Colin (478) 2433 posts |
No it wouldn’t you would get a compiler error because a mehdef structure isn’t a mehdef pointer. Compiler errors are there to catch this kind of error that can be detected at compile time. It would be interesting to know which is the most common bug, dereferencing a NULL pointer or dereferencing a freed pointer. Zeropain will only catch the former. |
Rick Murray (539) 13840 posts |
He’s right (obviously, duh me).
Given some of the code I’ve seen, I would suspect dangling pointers are the most common.
The irony of the situation is, funnily enough, that ROOL could probably have made their high vector change without most people noticing by simply stealing 32K and mapping it in at &0 as junk memory. Few things should be looking at the kernel data, nothing should be directly altering the processor vectors, and stuff that does dodgy reads and writes where it shouldn’t will carry on instead of going crashy-crashy. |
Malcolm Hussain-Gambles (1596) 811 posts |
Thanks Colin, that does explain it. Although it is kind of weird. It’s understandable though, but it is essentially the compiler saying 1=1 and 1!=1 – but only because I’ve changed the definition of 1 somewhere..no wonder I was having trouble in my head! |