|
Apologies for the shouting but this is important.
When answering a question please:
- Read the question carefully
- Understand that English isn't everyone's first language so be lenient of bad spelling and grammar
- If a question is poorly phrased then either ask for clarification, ignore it, or mark it down. Insults are not welcome
- If the question is inappropriate then click the 'vote to remove message' button
Insults, slap-downs and sarcasm aren't welcome. Let's work to help developers, not make them feel stupid.
cheers,
Chris Maunder
The Code Project Co-founder
Microsoft C++ MVP
|
|
|
|
|
For those new to message boards please try to follow a few simple rules when posting your question.- Choose the correct forum for your message. Posting a VB.NET question in the C++ forum will end in tears.
- Be specific! Don't ask "can someone send me the code to create an application that does 'X'. Pinpoint exactly what it is you need help with.
- Keep the subject line brief, but descriptive. eg "File Serialization problem"
- Keep the question as brief as possible. If you have to include code, include the smallest snippet of code you can.
- Be careful when including code that you haven't made a typo. Typing mistakes can become the focal point instead of the actual question you asked.
- Do not remove or empty a message if others have replied. Keep the thread intact and available for others to search and read. If your problem was answered then edit your message and add "[Solved]" to the subject line of the original post, and cast an approval vote to the one or several answers that really helped you.
- If you are posting source code with your question, place it inside <pre></pre> tags. We advise you also check the "Encode HTML tags when pasting" checkbox before pasting anything inside the PRE block, and make sure "Ignore HTML tags in this message" check box is unchecked.
- Be courteous and DON'T SHOUT. Everyone here helps because they enjoy helping others, not because it's their job.
- Please do not post links to your question in one forum from another, unrelated forum (such as the lounge). It will be deleted.
- Do not be abusive, offensive, inappropriate or harass anyone on the boards. Doing so will get you kicked off and banned. Play nice.
- If you have a school or university assignment, assume that your teacher or lecturer is also reading these forums.
- No advertising or soliciting.
- We reserve the right to move your posts to a more appropriate forum or to delete anything deemed inappropriate or illegal.
cheers,
Chris Maunder
The Code Project Co-founder
Microsoft C++ MVP
|
|
|
|
|
I am in Microsoft Visual Studio Community 2022 (64-bit).
I noticed that I can use printf even without including stdio.h.
It works if I do this:
#include <iostream>
Any idea why this works?
Thanks.
|
|
|
|
|
The chain of include files can be long and tortuous. In this particular case:
iostream -> istream -> ostream -> ios -> cstdio -> stdio.h
Should you rely on compiler include chains? Depends if you expect your code to be compiled with a different compiler and how long the chain is. In this particular case, there is a strong probability that all C++ iostream operations will end up like C function calls so you are pretty safe.
Mircea
|
|
|
|
|
I'm looking at the ios file, and I don't see where cstdio gets included.
I did a search in the file for cstdio, and it doesn't seem to be there.
Thanks.
|
|
|
|
|
I skipped a step: ios -> xlocnum -> cstdio.
Mircea
|
|
|
|
|
mike7411 wrote: It works if I do this:
C++
include <iostream>
Any idea why this works?
I guess, because there is a declaration of printf or or this is #included
|
|
|
|
|
iostream provides very complex facilities to C++, and requires a great deal of supporting infrastructure. You can get a list of all the include files that a compilation unit needs by using the /showIncludes flag in your compiles (-H for gcc/clang). Using Compiler Explorer, I see over 100 additional files included, including new , string , istream , ostream , atomic and cstdio (which gets you printf), just to name a few. As others have pointed out, you can just go ahead and use printf for example, and not add #include <cstdio> to your source file. However, as compilers evolve, you may find that this is no longer the case, at which point your code will fail to compile. Its good practice to #include all the relevant headers. System headers, at least, will have guards against multiple inclusions, so there's no harm in being explicit about what headers your program, or module, requires.
"A little song, a little dance, a little seltzer down your pants"
Chuckles the clown
|
|
|
|
|
Can someone tell me if this code works to copy a binary file?
while ((ch = fgetc(source)) != EOF) {
fputc(ch, target);
}
What if the file contains a byte that is 255?
Won't it get converted to -1 and break the loop?
Thank you.
|
|
|
|
|
Depends on declaration of ch . If it’s an int (as it should), all is fine as 255 != -1 . If it’s char , it fails, with or without compiler warnings.
Mircea
|
|
|
|
|
|
While your method may work, assuming that ch is an int, and so does not have the 255/-1 issue, your method is horribly inefficient. Richard's suggestion to use fread/fwrite produces much better performance.
For example, given an 8M file, looping through your code 100 times took about 32 seconds. Using a fread/fwrite using a 4K buffer, the same 100 lops took just under 1 second to complete. This was on a PI-3, and successive runs remained stable. I'd expect an 8M file to sit comfortably in the file cache, so the difference is totally down to the difference between extracting a single char at a time, and reading a lot of characters at a time.
"A little song, a little dance, a little seltzer down your pants"
Chuckles the clown
|
|
|
|
|
From CP newsletter
The Myth of Smart Pointers – Logikal Blog[^]
Basically complaining about what unique_ptr<t> is defined as.
Myself reading it and looking at the code it seems to me that the author does not understand the difference between the C++ specification and the C++ compiler/runtime.
Which has always been a problem for some people using C/C++ pointers since C (before C++ existed.)
The author seems to be suggesting that the specification is guaranteeing that the memory will be collected. But without even reading the spec I seriously doubt it says that. I suspect it is noting that the it might be collected so one should not rely on it.
There are multiple other places where one can do similar things and it might or might not work depending on compiler, binary and even execution flow (which is really fun to debug.)
Perhaps I am mistaken in what I am reading?
|
|
|
|
|
Given the fact that he is breaking all the rules by branching off into QML (whatever that is, and by his own comments something to be avoided) and Javscript, then you cannot expect C++ to know what is happening outside.
|
|
|
|
|
jschell wrote: The author seems to be suggesting that the specification is guaranteeing that the memory will be collected. But without even reading the spec I seriously doubt it says that. I suspect it is noting that the it might be collected so one should not rely on it. Of course it doesn't guarantee. Where the memory is coming from and where it is going to, is outside the scope of the language specification. From C++11 to C++23 there was some kind of support for garbage collection using std::declare_reachabl e, but it was deleted[^].
Mircea
|
|
|
|
|
Voluntarily removed - posted in wrong forum...
modified 2 days ago.
|
|
|
|
|
37 Questions asked in 2 months starts leaning towards a "Help Vampire". You have not shown us in your code above what you have tried apart from 4 lines of code, which oddly looks AI generated. Please tell us what you have researched so far on how to add second primary menus. If this errors, show the error and we will gladly assist from there.
|
|
|
|
|
|
I’m trying to display an animated bitmap in a win32 application with GDIplus. Last time when I talked about this here I was told I should invalidate a rectangle to force the App to refresh/draw another frame. I’m trying to understand the theory for now. I will come up with code in a day or two if necessary. My question is do you create and invalidate between beginpaint and endpaint a rectangle that has no particular purpose or does it have to be a rectangle that actually does something.
I’m having a difficult time understanding the connection between a rectangle and the rendering process of a win32 api window
|
|
|
|
|
Roughly speaking, the invalidated rectagle will be redrawn in the next WM_PAINT call (other parts of the client area are not redrawn). So, if you need to update a portion of the client area then invalidate the corresponding rectangle and then call UpdateWindow() .
In simple scenarios you can invalidate the entire client area and then call UpdateWindow() .
See, for instance: Invalidating the Client Area - Win32 apps | Microsoft Learn[^].
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
Basically I need a rectangle taking the whole window for my game, I think I understand, I’ll post a followup if I’ll run into trouble setting things. Thank you.
|
|
|
|
|
You are welcome.
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
The only things you should be doing between BeginPaint and EndPaint is redrawing/rewriting the parts of the client window that may have changed. Outside of the WM_PAINT handler, you decide which part (or all) of the window needs to be repainted. You then set those values into a call to InvalidateRect , which will post a WM_PAINT message to your message queue.
|
|
|
|
|
>The only things you should be doing between beginpaint and endpaint
I was thinking to disregard everything that was rendered in one frame, and then render things all over again. Keeping track of pieces that visually change in my game ( if that’s what you mean) seems like a burden at this point.
So far I’m only looking for a way to trigger a new paint event
modified 3 days ago.
|
|
|
|
|
You are welcome to do that, but it may affect the performance in certain scenarios.
|
|
|
|