0

I am pretty new programming on Mac (osx: Sierra 10.12.6) so it could be a very stupid question. I was working on couple Linked List operations, everything works as expected unless sometimes when I do not restart Mac for couple days then some type casting errors show up. Although casting type can remove the errors but normally I do not need to cast types for malloc() while using C language. Even those programs I have compiled earlier without error will show up with some errors like the following:

error: assigning to 'node *' (aka 'node_t *') from incompatible type 'void *' 
head = malloc(sizeof(head));
error: cannot initialize a variable of type 'node *' (aka 'node_t *') with an rvalue of type 'void *' 
node *current = malloc(sizeof(current));

After restarting the terminal and completely shutting down the computer and starting again can get rid of this issue and those programs can compile again without error. I will appreciate if anyone can help me with this problem so even if I do not restart Mac for some weeks or long I would not get these errors.

rIz
  • 1
  • 2
  • 4
    Are you using `c++` compiler? – Eugene Sh. Oct 27 '17 at 21:34
  • 3
    C doesn't require using a typecast from `void *`, but C++ does. It sounds like you're using the compiler in C++ mode. – Barmar Oct 27 '17 at 21:34
  • 1
    What file-ending does your source file have, is it `.c`, or is it `.cpp`? Are you using XCode? Otherwise, can you show with which arguments you call the compiler? – Stephan Lechner Oct 27 '17 at 21:39
  • 2
    Apart from compiling in C++ mode (the cause of your error message) the usage `head = malloc(sizeof(head))` is incorrect, and should be `head = malloc(sizeof(*head))`. Getting that wrong will (unless your `struct` type is smaller than a pointer, which is possible but uncommon in practice) mean less memory allocated than needed and result in undefined behaviour when attempting to use the dynamically allocated object. – Peter Oct 27 '17 at 22:04
  • @Barmar, I just use "gcc myprog.c -o myprog" to compile the C file from command line on osx terminal. Sometimes I use on the same terminal "g++ myprog.cpp -o myprog" to compile other C++ programs. Do you think they can conflict each other while using on the same terminal? PS: Although I never had problems doing these simultaneously last 4/5 days AND after restarting the same way as I mentioned in the above question, the same C files are compiling now without any error. – rIz Oct 28 '17 at 03:49
  • @Peter, I understand what you mean. I just found this (https://stackoverflow.com/questions/7545365) where the first answer explains what you said. Thanks anyways although its nothing to do with my question. – rIz Oct 28 '17 at 03:55

1 Answers1

-2

you must ensure to cast your call as such:

head = (node*) malloc(sizeof(node))
Patrick Sturm
  • 393
  • 1
  • 13
  • 4
    This is only required in C++, not C. – Barmar Oct 27 '17 at 21:35
  • https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – Barmar Oct 27 '17 at 21:35
  • @Barmar But you yourself noted that OP's probably actually using C++... – Charles Srstka Oct 27 '17 at 21:36
  • So the solution is to explain how to use the compiler in C mode if that's intended. – Barmar Oct 27 '17 at 21:37
  • @CharlesSrstka The question is about C. – Eugene Sh. Oct 27 '17 at 21:37
  • 1
    The programmer believes the question is about C. Does not make it so. – Patrick Sturm Oct 27 '17 at 21:38
  • @PatrickSturm If the op would ask "why can't I drive this nail with a hammer", but actually using a zucchini, will you tell him to grab a larger zucchini or you will tell him to use an actual hammer? – Eugene Sh. Oct 27 '17 at 21:40
  • That very statement goes both ways. – Patrick Sturm Oct 27 '17 at 21:47
  • @EugeneSh. How do you know that C++ isn't the appropriate language for his project? For all we know, the use of the `c` tag was a mistake. – Charles Srstka Oct 27 '17 at 21:47
  • Aaaand we have friction – Patrick Sturm Oct 27 '17 at 21:48
  • 1
    @CharlesSrstka It's not only the `c` tag but the question text. – Eugene Sh. Oct 27 '17 at 21:48
  • I admit, in an effort to end this, I could have provided a better answer instead of a blatant, contextual fix. – Patrick Sturm Oct 27 '17 at 21:50
  • 2
    @CharlesSrstka This whole question could be a mistake and the actual text could be "Hello humans, I am a whale from Sirius, we came in peace". – Eugene Sh. Oct 27 '17 at 21:50
  • @EugeneSh. Is that more likely than the OP getting C and C++ mixed up? Occam's Razor, dude. – Charles Srstka Oct 27 '17 at 21:51
  • 1
    @CharlesSrstka And this is exactly what we are talking about here - we are letting him know that he is mixing them up, not telling how to fix what he believes is C in a way not appropriate for C. My Occam's razor is cutting off the possibility that the OP doesn't know which language he is using. Compiler perhaps. – Eugene Sh. Oct 27 '17 at 21:52
  • @EugeneSh. Mine tells me that it's easier to leave the ++ off of the tag than to accidentally add pp to the filename extension, but eh, whatever. – Charles Srstka Oct 27 '17 at 21:58
  • Ok, I guess this discussion is pretty useless. We made our points. @PatrickSturm, I guess you can improve the answer with a minimal effort and satisfy both. – Eugene Sh. Oct 27 '17 at 21:58
  • Unnecessary, we are just border-line spamming by now – Patrick Sturm Oct 27 '17 at 22:02
  • It is absurd to see these many useless comments on SO under a useless answer, none of which has anything to do with the main issue in the question. Nex time, please read the question text before starting any guess game and do your own research before posting any answer or just go over a FB page to have your chit-chat. – rIz Oct 28 '17 at 14:43