|
The issue was /is the relations between QMenu and QAction...I did solved this by looking at many similar examples. But as told many times - posting it here was my mistake, lesson learn.
Thanks.
|
|
|
|
|
If a memory allocation with new fails in C++, should it return NULL or throw an exception?
I have this code:
#include <iostream>
#include <vector>
#include <unistd.h>
using namespace std;
int main() {
try{
char *p = new char[44'321'833'923];
if (!p) {
cout << "memory allocation failed";
exit(1);
}
}
catch(exception&e)
{
cout << "caught it." << e.what();
exit(1);
}
catch (...)
{
cout << "caught exception;";
exit(1);
}
return 0;
}
The class I'm taking says it returns NULL, but the code seems to throw an exception.
Anyone know?
Thanks.
|
|
|
|
|
I don’t know who teaches the class you are taking but I hope you don’t pay much for it. new has been throwing std::bad_alloc for the past 20+ years.
Also, while I don’t mind at all answering your questions, why don’t you try to googling? A search for “c++ new allocation failure” gives you many results and undoubtedly can teach you more than my limited answers.
Edit: Using exactly the search I suggested, I found this good article that discusses the issue: C++ Exceptions and Memory Allocation Failure[^].
Mircea
modified 11-Jan-24 7:50am.
|
|
|
|
|
|
As this is supposed to be a C++ class, the code should be using nullptr rather than the old Windows NULL definition. And what happens if numStudents is only 1?
|
|
|
|
|
Colour me not impressed! As long as you take the free option, I guess you didn't loose much, except maybe some time. Also there is the question of getting bad programming habits, but those can be fixed over time.
These days there are many free online course from very reputable sources like MIT OpenCourseWare | Free Online Course Materials[^]. You might want to take a look at some of them.
Mircea
|
|
|
|
|
|
And also by binary-file I mean the files with the weird characters and all.
Next, Here is a example of what it should look like:
read_4_bytes("file", 0);
Not much else to say...
|
|
|
|
|
1. Use fopen function to open the file
2. Use fseek function to go to specific offset
3. Use fread function to read the 4 bytes
4. Use fclose function to close the file
Mircea
|
|
|
|
|
One more detail - on Windows, you must open the file in "binary" mode using the O_BINARY flag (for open) or "rb" (for fopen).
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows.
-- 6079 Smith W.
|
|
|
|
|
I am trying to catch a divide-by-zero error with the following code:
#include <iostream>
using namespace std;
int main() {
try {
int a = 0;
int x = 5 / a;
}
catch (exception e)
{
cout << e.what() << endl;
}
catch (...)
{
cout << "caught ..." << endl;
}
}
For some reason, neither of my catch blocks gets executed. Anyone know how to catch the exception?
Thank you.
|
|
|
|
|
AFAIK, there's no way to do it.
You'll have to catch it manually and throw.
CI/CD = Continuous Impediment/Continuous Despair
|
|
|
|
|
I've not used it, but maybe take a look at Handling the Divide by Zero Exception in C++[^], similar to what Maximilien said.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
modified 5-Jan-24 13:08pm.
|
|
|
|
|
The article mentioned in the previous post does it by doing divisions in a function that checks for a zero divisor and throws an exception if it detects one. If you don't want to use such a function, it gets rather complicated.
If you're running on Windows, you need to handle the structured exception STATUS_FLOAT_DIVIDE_BY_ZERO or STATUS_INT_DIVIDE_BY_ZERO . If you're running on Linux, you need to handle the POSIX signal SIGFPE . You can also handle the use of a bad pointer with these techniques. The handler that receives the structured exception or POSIX signal can throw a C++ exception, which you can then catch in the usual way.
The following article goes into the details. But because it also describes a thread framework, you'll have to sift through it to extract the specific code that you need. In particular, see the sections "Receiving a Windows Structured Exception" and "Receiving a POSIX Signal".
Robust C++: Safety Net[^]
|
|
|
|
|
To me that was always the 'fun' part of C++ versus Java and C# - figuring out exactly what was being thrown in the first place.
|
|
|
|
|
In Windows, there are additional, O/S-level exceptions, that provide this sort of information. Look for "Structured Exception Handling". If you use MSVC, you may use the _set_se_handler function in main() and in the main function of each thread to convert Structured Exceptions to C++ exceptions.
There may be similar O/S- and compiler-specific methods in other environments.
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows.
-- 6079 Smith W.
|
|
|
|
|
I have the following file code:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
string path = "student/text/readpractice.txt";
try {
ifstream file;
file.open(path); if (!file) {
throw runtime_error("File failed to open.");
}
cout << file.rdbuf(); file.close();
}
catch (exception& e) {
cerr << e.what() << endl;
}
return 0;
}
Can someone help me understand how this line works:
if (!file) {
How can you use the exclamation point operator on what is probably a class?
Is there some type of overloading that has occurred? I looked thru the code, and I couldn't find the ! point being overloaded.
Any ideas?
Thanks.
|
|
|
|
|
|
Interesting.
I tried doing a "Step Into" on that line in the debugger, but it won't go to it.
If I do a "Go to Definition", it goes to this in xiosbase:
_NODISCARD bool __CLR_OR_THIS_CALL operator!() const noexcept {
return fail();
}
However, I put a breakpoint there, and it doesn't break.
|
|
|
|
|
I wrote the following code to output the Greek word "Δέλτα":
#include <iostream>
#include <io.h>
#include <fcntl.h>
using namespace std;
int main() {
_setmode(_fileno(stdout), _O_U16TEXT);
cout << "\u0394\u03AD\u03BB\u03C4\u03B1" << endl;
return 0;
}
For some reason, I get this error:
---------------------------
Microsoft Visual C++ Runtime Library
---------------------------
Debug Assertion Failed!
Program: ...sers\Owner\source\repos\assert_test\x64\Debug\assert_test.exe
File: minkernel\crts\ucrt\src\appcrt\lowio\write.cpp
Line: 659
Expression: buffer_size % 2 == 0
For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.
(Press Retry to debug the application)
---------------------------
Abort Retry Ignore
---------------------------
Any ideas why the code is producing that error?
Thank you.
|
|
|
|
|
First things first, you're ignoring the return value of _setmode. If it's -1, the call failed, but you'll never know.
Second, after reading the documentation on _setmode and associated error links, it appears you cannot use cout with the mode of stdout switched to Unicode. You have to use wprintf instead:
#include <iostream>
#include <io.h>
#include <fcntl.h>
using namespace std;
int main()
{
int r;
r =_setmode(_fileno(stdout), _O_U16TEXT);
if (r == -1)
perror("Cannot set mode");
wprintf(L"\u0394\u03AD\u03BB\u03C4\u03B1\n");
return 0;
}
CORRECTION: You CAN use COUT, but you must use the wide version of it, but note the L in front of the string being output. It MUST be there:
#include <iostream>
#include <io.h>
#include <fcntl.h>
using namespace std;
int main()
{
int r;
r =_setmode(_fileno(stdout), _O_U16TEXT);
if (r == -1)
perror("Cannot set mode");
wcout << L"\u0394\u03AD\u03BB\u03C4\u03B1" << endl;
return 0;
}
|
|
|
|
|
I wrote this code:
class ChessPiece
{
};
int main()
{
cout << "size:" << sizeof(ChessPiece) << endl;
}
For some reason, it prints out 1 instead of 0. Anyone know why?
Thanks.
|
|
|
|
|
Probably because there has to be some minimum size of a class. You could study the C++ standard to see why.
|
|
|
|
|
Perhaps take a look at this FAQ.
Stroustrup: C++ Style and Technique FAQ[^]
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
That is interesting.
That page references a 'better' link. Same answer is there.
Standard C++[^]
Myself though if Stroustrop is the name then I would consider that better.
modified 2-Jan-24 12:43pm.
|
|
|
|