Re: Is C binary flag atomic?
I didn't make any assumptions. I was always avoiding using flags in the way I described but decided today to ask what you guys thought about it purely out of curiousity.
In my applications I tend to use mutex locking as a multi-threaded resource protection mechanism.
Thats what I thought too which is why I've been naturally avoiding using them in that way, but it would have been so convinient and elegant if it did. Thus is life in the world of the compiled.
Is C binary flag atomic?
Imagine that the Flags instance is a shared resource in a multi-threaded application. Say thread #1 decides to read the value of Flags->A but thread #2 decides to set or clear Flag->B at the exact same time. How isolated is Flag->A from Flag->B such that any changes to Flag->B does not affect the value of Flag->A? In other words, are set or clear operations in this structure atomic and therefore thread safe?
Re: Pointer Operation Went Wrong !!!
Just a quick word of advice. It is more efficient to use fwrite() or fputc() instead of fprintf() for writing characters to a file one at a time. This is because fprintf() needs to parse the format string whereas fwrite() and fputc() does not.
This only matters if you plan on dealing with very large files.
I just tested the code and it agrees with Ancient Dragon's explanation. Sorry about that.
Integer has nothing to do with pointer size. The pointer is the size of the memory address as dictated by the Operating System based on the CPU's capabilities. A 32 bit CPU will have a 4 byte address whereas a 64 bit CPU will have an 8 byte address. Some Operating Systems will have an emulation layer that would change this address size.
Its funny to think that c=(--a)+(--a); is not any more efficient than --a;--a;c=a+a; in terms of how it compiles. Its just that the first version is ambiguous in design as (--a) acts as a function with a side effect rather than as a variable in an algebraic formula.
Ever since I learned about the ++ operator I felt it was like cheating and could confuse people on this very thing. It would make C much more logical if you avoided ++ altogether in place of inline function calls achieving the same thing.
Re: Is this a typedef problem?
I would classify your question a platform specific issue since again the definition of SOCKET is not your responsability. As the programmer of the PIC32 and user of your compiler you need to have certain expectations of compliance with the standard libraries as perscribed by the C standard. If there are bugs with its functionality thats the platform's fault, not yours.
And unless you are reinventing the wheel you should not be actually defining a typedef for SOCKET. Its defined in your socket library headers. In Windows its winsock2.h and linux its sys/socket.h. Find out which is right for your system. You are not responsible for defining this structure unless you are correcting a conflicting definition which a situation you want to avoid as much as you can for scalability sake.
Long story short just include the correct header. The compiler error you mentioned implies that you are not.
Correction: The fix appears to be that I need to use the extern keyword instead of static, then declare it in a CPP file. The problem seems to be with the structure being created in different contexts or address spaces for each loaded CPP file or something. Might be a Borland C++ Builder specific issue.
Do keep in mind that my actual code is composed of literally thousands of lines of code spanning many cpp/h files. Apparently Borland C++ Builder 5 does not like working with structures defined in headers unless its anchored down with the static keyword.
I guess thats good information.
In this case its being called by a VCL button OnClick event handler. The code itself is in a cpp file compiled with the rest of the project. As an event handler, I doubt it is a constructor.
Thank you for your quick reply deceptikon. The problem with your theory is that the TempPage variable worked fine using malloc(). The problem appears to be in copying the pointer to the actual Stock.Page pointer. Also, I use malloc() in other places of the code (such as creating space for strings and arrays) and that works fine.
Other parts of the code work fine in the C++ compiler. So far this seems to be the main incompatibility.
In GCC, the Stock.Page pointer will be non-NULL at the end of the somerfunct() function. This is not the case for Borland C++ Builder 5. In that case Stock.Page continues to be NULL.
// Borland C++ Builder 5 likes this syntax. TempPage is a non-NULL.
// But I still cannot copy the pointer from TempPage.
// Stock.Page is still NULL at this point!
If you have any specific questions from the book, you should ask them here. We would be happy to help you. It is not the nature of DaniWeb to give out answers, especially instructor's manuals.
Well, since you know English, why are you posting this code?
There is a right way to post code snippets. This is the wrong way, primarily because he doesn't describe the application of the code he is posting.
There is no excuse not to say anything in a help forum. We are not robots: we don't communicate in code. If you are not comfortable with the English language, its okay. We will not criticise your speech. But you must explain yourself, otherwise you are just spamming the site.
... and that's okay too. Welcome to Daniweb people. When posting a new thread on this forum, it helps to say something. With nothing to say, a thread can easily become derailed, such as this one.
It's obvious that [B]neeraj goswami[/B] did not want you to know what this is about, otherwise he would have said something by now. So use this rare opportunity to use your imagination!
I have attached a visual aid of the concept of imagination which may help your interpretation of this thread.
Ok, thats a different question. When you compile your code in C, it gets converted to machine code (assembly). That code is not a direct representation of C.
That is not compiled. That's because its only useful for the compiler. The computer itself does not care if "a" is an integer, only the compiler cares.
It is putting the value of 10 to some memory space, probably a register.
Code in C generally executes one line at a time, and if you use a "switch", the program will jump to wherever you are "switching" to.
It will only pay attention to "case 2"
That was not a tutorial on how to run the code. I was trying to explain why the code is giving garbage value.
A "switch" is a more elegant "goto." A "goto" is a command that jumps over code. Your switch is jumping over x=10. It never gets to be initialised, thus, it has no useful information in it, thus it is garbage.
And since x is not initialised with any value, it will print garbage.
I have had situations where I needed to graph a complex mathematical model for some application, such as compression, encryption, image, and number theory analysis. I honestly didn't think there were programs out there that do this kind of thing. What I would do instead was just make a GUI application in C to plot the specific nature of my analysis which takes some time to develop but in the end its quite flexible and powerful. Either way this software you mentioned intrigues me.
If IDE A has a tab spacing length of 2 and IDE B has a tab spacing of 4, as long as you are consistent with the tabs, and as long as its only used to indent the code, that alone should be fine. Mixing tabs and spaces is the primary issue.
I have also seen IDE's where you press enter and it auto-inserts tabs for you rather than spaces, so being consistent requires looking at the IDE settings before you begin your programming. And as far as copying code from other sources, that needs to be checked for proper indentation as well.
His indentation looks horrid, but notice that some of his lines are tabed and some are spaced. I have this problem too when copying code from an IDE where tabs are set to allocate an unconventional specific number of fixed width spaces. It makes copying terrible. On that kind of IDE, your better off using spaces exclusively or tabs exclusively.