|
To my mind, the code I wrote should have triggered a compiler diagnostic. The expression on the left side of the assignment operator is a pointer to _TCHAR while the right side is a single _TCHAR constructed from the value buffer_size . Barring perversions of the assignment operator, those two aren't the same thing.
Of course, I am using a very old version of Visual Studio for this project, so...
Software Zen: delete this;
|
|
|
|
|
Unfortunately, new creates an object on the heap, and it does return a pointer. That corresponds to the left hand side. Also, it's not obvious to the compiler to understand what you intended. If you had constructed a class instance instead, it would be quite reasonable. While creating an integral type on the heap is quite unusual, it might still make sense if that code is part of a template implementation:
template <class T>
T* copy(T& const source) {
return new T(source);
}
This code works for any kind of class, but also for integral types. So why should the compiler complain?
However, the crux of the problem is that C/C++ treat {addreess to first element of array} the same as {address of object}. It doesn't differentiate the two, and that is why your assignment doesn't even raise a warning.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
Aha! Thanks for the explanation. I sometimes forget subtleties because I'm constantly switching back and forth between C++ and C#.
Software Zen: delete this;
|
|
|
|
|
Stefan_Lang wrote: However, the crux of the problem is that C/C++ treat {addreess to first element of array} the same as {address of object}. It doesn't differentiate the two, and that is why your assignment doesn't even raise a warning.
const char *c = "Hello world!";
std::cout << "*c = " << *c << "\n";
std::cout << "size of *c = " << sizeof *c << "\n";
std::cout << "c[0] = " << c[0] << "\n";
std::cout << "size of c[0] = " << sizeof c[0] << "\n";
std::cout << "c = " << c << "\n";
std::cout << "size of c = " << sizeof c << "\n";
std::cout << "&c[0] = " << &c[0] << "\n";
std::cout << "size of &c[0] = " << sizeof &c[0] << "\n";
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpful answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
|
So for the C++ illiterate, what's wrong with that line of code? Or did you mean new _TCHAR[buffer_size]; ?
|
|
|
|
|
Precisely. As written, it casts buffer_size as a _TCHAR and allocates only one of them.
|
|
|
|
|
This man, Mr. Arnab Chakraborty has taught me so much about parsing. His videos are incredible. They're basically class lectures on advanced CS topics, like this one on viable prefix computation in shift reduce parsing.
Viable Prefixes and Valid Items - YouTube[^]
It appears he's not a doctor - not that it matters, but it's just startling to me given the variety of advanced subjects I've found him lecture on. I'd think he'd make a great CS professor anywhere.
I'm not really here to plug his site, but he's such a valuable resource at the same time i don't want anyone to miss out. I usually google with "tutorialspoint [topic]" if i want to find him in particular.
I don't even usually like videos to teach things, but in some cases seeing him whiteboard it is very helpful.
Real programmers use butterflies
|
|
|
|
|
|
NPTEL seems to be great resource for online learning.
If you are not criticized, you may not be doing much.
|
|
|
|
|
Forget digresses! Either he knows what he it talking about or he does not. I've been telling computers what to do for over 30 years and do not have a degree. But then again, there was no degree's on the subject when I stated.
INTP
"Program testing can be used to show the presence of bugs, but never to show their absence." - Edsger Dijkstra
"I have never been lost, but I will admit to being confused for several weeks. " - Daniel Boone
|
|
|
|
|
I don't have a degree either, and it's one of the reasons I find the work above so valuable.
Real programmers use butterflies
|
|
|
|
|
When I saw a cmovne instruction in a disassembly through which I am tracing, I asked Bing what it was, and got the details at Purpose of cmove instruction in x86 assembly? - Stack Overflow, which are borne out by the demonstrated behavior of my code. It appears that the Visual C++ compiler that ships with Visual Studio 2019 counts the cost of two mov instructions followed by a cmovne instruction as less than that of a branch that causes both to execute anyway, even though the outcome of one of the two mov instructions would eventually be discarded.
Moreover, this is happening in a debug build!
This is by no means the first such unexpected optimization that I've seen the MSVC compiler generate in a debug build.
David A. Gray
Delivering Solutions for the Ages, One Problem at a Time
Interpreting the Fundamental Principle of Tabular Reporting
|
|
|
|
|
David A. Gray wrote: It appears that the Visual C++ compiler that ships with Visual Studio 2019 counts the cost of two mov instructions followed by a cmovne instruction as less than that of a branch that causes both
Keep in mind that due to the speculative execution leaks... the emitted instructions may be avoiding data-dependent branching. Optimization is not as simple as it was ten years ago...
Do me a favor and right click on your project settings and navigate to the 'Code Generation' tab and check if /Qspectre is enabled.
|
|
|
|
|
My take on the whole point of the conditional move instruction is to eliminate speculative execution from the mix by keeping the program flow in a straight line. FWIW, I double-checked, and Spectre mitigation is disabled. I suspected it was, because I've never enabled it for any of my projects.
What made it stand out was that this was happening in a debug build. Had it been a release build, I would have expected this level of optimization. However, this is the most recent of many such unanticipated, and welcome, optimizations, that I've seen in recent debug builds.
David A. Gray
Delivering Solutions for the Ages, One Problem at a Time
Interpreting the Fundamental Principle of Tabular Reporting
|
|
|
|
|
David A. Gray wrote: What made it stand out was that this was happening in a debug build. Had it been a release build, I would have expected this level of optimization. However, this is the most recent of many such unanticipated, and welcome, optimizations, that I've seen in recent debug builds. Yeah, it's not a coincidence that the first compiler implementing the /Qspectre mitigations[^] is generating slighty different conditional moves. It's an active area of development.
Best Wishes,
-David Delaune
|
|
|
|
|
I just pasted a block of #define directives from the application header of a C++ program that I am writing in preparation for writing a short article for the Actian developer blog. When I applied the Trim worksheet function to a substring that contains a hexadecimal value that I wanted in its own column, I noticed that the cell had a leading space. Since it obviously wasn't a SPACE or a TAB, I turned on the hexadecimal view mode in my editor (UltraEdit), and discovered that the hexadecimal code of the character at that location was 0xA0, 160 decimal, the nonbreaking space character.
David A. Gray
Delivering Solutions for the Ages, One Problem at a Time
Interpreting the Fundamental Principle of Tabular Reporting
|
|
|
|
|
I always use invisible whitespace in my comment and human strings, don't you?!
Everyone loves them!
|
|
|
|
|
David A. Gray wrote: I just pasted a block of #define directives [sic] from the application header of a C++ program that I am writing... He that lieth down with dogs shall rise up with fleas. --Poor Richard's Almanack
|
|
|
|
|
I typically do not like javascript. It's not personal, it's just the language's typing system I've always found difficult to use, and overall i find the language hard to debug.
But I've been wanting to find information on LL(k) parse table generation that I could digest for years, and some good folx produced an implementation in javascript which doesn't seem to use anything too weird or JS specific so I'm porting it to C#.
Sure I have to debug it in chrome to fill in some of the details but they made it easy to port, and I imagine they planned it that way.
What's fascinating to me about it too, is they're using a parser and lexer to parse the input grammar that looks like it was generated using a port of some common C based tools to javascript.
what a weird and cool little program.
LL(k) Parsing Table Generator[^]
Real programmers use butterflies
|
|
|
|
|
It's not too difficult the other way round too - I just converted a steganography application from C# to Javascript.
I tend to use React and Typescript because with Typescript you can avoid a lot of the problems vanilla Javascript can lead you into.
“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens
|
|
|
|
|
Yeah I converted a splay tree from typescript to C# once to learn how it was done, and it was a joy.
I didn't end up using the implementation, because it was recursive, but it taught me the concepts.
Real programmers use butterflies
|
|
|
|
|
C++ error messages are often incomprehensibly surreal. I was baffled by an "Undefined reference to vtable" message until a tedious investigation revealed it was just a missing method from my class. Why the f doesn't the C++ compiler just say: "Method xxx is missing."??
This is just a sample; there are many more C++ error messages from simple causes complaining about parts of the compiler I've never even HEARD of. Out of curiosity, I emailed Bjarne Stroustrup and asked him why C++ error messages were so incomprehensible. Here is his reply:
"Thank you for your inquiry. Goats are like mushrooms. If you shoot a duck, I'm scared of toasters."
I guess that answers my question.
|
|
|
|
|
They can be utterly incomprehensible but you get used to them. I revisited C then C++ recently after several years away with C# and found the messages from the compiler to be almost universally unhelpful but after a week or so I settled into it again.
|
|
|
|
|
big same
Real programmers use butterflies
|
|
|
|