This is something I have noticed and I do not have the answer to it and it bothers me.
Let's say we have two simple functions.
void foo()
{
std::cout << "Rainbows are cute!" << std::endl;
return;
}
int main()
{
foo();
return 0;
}
Now these two functions are all part of the same cpp file.
If I compile this cpp file on gcc the file will cout "Rainbows are cute!"
but if I were to do it on Xcode or Visual Studio, the cout statement will not display. I mention VS and Xcode because these are two common compilers, used by many.
My question is why does this happen? What is going on in the compilers were one will display the cout statement in the void functions and the others will not?
The printouts will display in VS and Xcode as well. The difference is in how you run this. When you execute your program from Visual Studio, console window briefly pops up, displays the message, and promptly disappears.
To prevent this from happening, you can set breakpoint on return 0 line, and run in debug mode. When the breakpoint is hit, switch to the console window to see the message:
Related
I have a DLL containing multiple functions that can fastly perform arithmetic operations on extremely large integers. My test program runs smoothly in my Visual Studio 2019, as follows.
int main()
{
HINSTANCE myDDL = LoadLibrary(L".\\BigIntDLL.dll");
typedef string (*func)(string a, string b);
func expBigInt = (func)GetProcAddress(myDDL, "expBigInt");
string y= expBigInt("2", "10000");//it can calculate 2^10000 and return it as a string
cout << y;
}
So, I moved the code directly into my Qt project as a part in widget.cpp, and also placed the BigIntDLL.dll and .lib in the same directory of the project. The compilation was successful, but when debugging my interface, the program broke with a Segmentation fault error due to a call to the expBigInt function.
void Widget::on_expButton_clicked()
{
getTextEditNum();
Output=expBigInt(Input1,Input2);//crashed here
writeResult(Output);
}
I am not really sure where the real problem is, but I now suspect that I have not successfully called the functions in this DLL, causing some memory issues.
I have the following code:
#include <iostream>
using namespace std;
Sum (int a, int b)
{
int x = a - b;
//cout << x << " \n";
return x;
}
int main()
{
int s1 = Sum(3, 6);
cout << s1;
return 0;
}
System info:
Win 7 Sp1 x64 Ultimate/Professional or Win 8.1 x64
Code Blocks 16.01 MinGW
Debugger name and version: GNU gdb (GDB) 7.6.1
compiler: GNU GCC Compiler
This code compiles with no problems, but this IS the problem, there should be errors.
1) Function Sum, has no return value, on http://cpp.sh/ it doesn't let me compile because of this.
2) Variable's s1 value is -3 whether I write "return x" or not.
It somehow passes the value of x everytime BUT if I uncomment the cout statement above the "return x" everything starts to work out as expected, what the hell :) --> s1 will have a random value when no return statement is in place (because it was not initialized prior to being used for the function call) and -3 when the return is there.
I've tried this on 3 separate computers and they all exhibit the same behaviour. So I don't think the machine is the problem.
I also tried using a different compiler but I don't know if I configured them correctly and they don't have a debugger right ? I tried Borland c++ and Digital Mars. Borland has a new version 10.1 instead of the 5.5 that codeblocks supports and I couldn't make the new one work.
I don't even know if this is a compiler or program issue ?
I'm trying to learn C++ and this is very annoying. Our teacher is using the same software in class but on Linux and it works perfectly.
Off topic: Is there a way to insert code with line numbers here ? First post here so i'm still new at this :).
Thank you !
Go to "Project" -> "Build Options" -> "Compiler Settings" tab -> "Compiler Flags"
And disable -fpermissive
-fpermissive Downgrade some diagnostics about nonconformant code from errors to warnings. Thus, using -fpermissive will allow some
nonconforming code to compile.
Or disable it using pragma on the top of your code:
#pragma GCC diagnostic ignored "-fpermissive"
Also you could try to add the flag "-pedantic" in "Compiler Flags" tab
BTW:
If you try online:
#pragma GCC diagnostic error "-fpermissive"
using namespace std;
Sum (int a, int b)
{
int x = a - b;
//cout << x << " \n";
return x;
}
int main()
{
int s1 = Sum(3, 6);
cout << s1;
return 0;
}
You got exactly same behavior you described!
As Rama said, you might have enabled -fpermissive in your codeblock.
Go to "Project" -> "Build Options" -> "Compiler Settings" tab -> "Other options" and delete -fpermissive.
The debugger in Visual Studio 2010 is recently pointing at the wrong lines and/or skipping lines and I have no idea why this is. This is a CUDA project and only happens in CUDA files. I've noticed the following:
It always happens at the same part of the program.
The lines it points to are always the same, i.e. not random.
Putting extra code after the culprit lines changes which lines it points to.
It only happens in .cu-files. Moving the code to a .cpp-file does not recreate the problem.
What I have tried:
Clean and rebuilt the solution.
Install SP1 for MSVC10 and do all possible updates via Windows Updates
Set the compiler to not use optimizations in debug mode for both C/C++ and CUDA C/C++
Manually delete all created files and then rebuild from the solution folder.
Deleting the folder C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files
Recreating the solution only using the source files.
Disabling my extensions.
I've managed to reduce the code to the following which might reproduce the problem. Mind that this code has to be inside a .cu-file and most probably needs to be compiled with the cuda compiler for C/C++. Including boost is not really necessary, but this example does show what problems I'm having. A shorter example is at the back.
#include <boost/numeric/ublas/matrix.hpp>
using boost::numeric::ublas::matrix;
struct foo {
foo() : mat(NULL) {}
matrix<float>* mat;
};
bool func(foo *data) {
bool status; // <- skipped line
status = false;
if (status) {
std::cout << "test\n";
return (status); // <- error reported here
}
int size = data->mat->size1(); // instead of here
return status;
}
int main(int args, char* argv[]) {
func(NULL); // force error by passing NULL pointer
return 0;
}
Does anyone have any idea how to solve this or how this could be happening? It's pretty annoying having to debug this way.
Shorter example only showing the skipping lines. No external libraries necessary.
bool func() {
bool status; // <- skipped line
status = false;
return status;
}
int main(int args, char* argv[]) {
func();
return 0;
}
Since the program only contains CPU instructions and variable declarations of types that have no construction contain no instructions, the debugger will not stop there. It just executes instructions and then uses the debugging information that the compiler provided to find the relevant line of source code.
I have a program that I'd like to debug by setting a breakpoint in a non-default constructor, but the breakpoint I set is never hit. Below is an example program where this problem comes up. There is no problem hitting breakpoints set in the main function, but any breakpoints set in the Domain.cpp file are ignored:
Main.cpp:
#include <iostream>
#include "Domain.h"
int main()
{
Domain y;
std::cout << y.x << std::endl; // <- No problem setting breakpoint here
return 0;
}
Domain.cpp:
#include "Domain.h"
Domain::Domain()
{
x = 4; // <- A breakpoint here is skipped
}
Domain.h:
#ifndef DOMAIN_H_
#define DOMAIN_H_
class Domain
{
public:
int x;
public:
Domain();
};
#endif /* DOMAIN_H_ */
However, the problem does not exist if I put everything into a single file:
Main2.cpp:
#include <iostream>
int main()
{
class Domain
{
public:
int x;
Domain()
{
x = 4; // <- No problem setting breakpoint here now!
};
};
Domain y;
std::cout << y.x << std::endl;
return 0;
}
Why is this the case? How can I change this so that I'm able to set breakpoints when I use multiple files?
I can confirm that the breakpoints aren't working both when I run the debugger manually in a terminal and when I run it through Eclipse CDT, where I get the same error discussed in this question which was apparently never answered:
Why does Eclipse CDT ignore breakpoints?
I am using:
Eclipse Kepler
Mac OSX 10.8.4
gdb 6.3.5 (Apple version)
gcc 4.2.1 with -O0 and -g3 flags
Please be patient with me. I'm still learning the ropes.
You are likely hitting this GDB bug.
This bug has long been fixed, but your version of GDB is very old (and Apple is unlikely to update it).
This is a very interesting anomaly that I would like to explore further, but I suspect it's related to Eclipse's default GCC settings. Many super basic functions like these get optimized out when they hit the compiler. (one time I tried to track a simple for loop, but the viable was removed entirely on GCC's highest optimization settings)
I am working on VS 2008. I wish to get the following information for all my methods:
1) Time at call entry
2) Time at call exit and the return value.
GDB allows me to set a break point at each function entry and exit and run a script at the breakpoint and then continue debugging. I am tired of looking for solutions to do something similar on VS. I even thought of writing a script to parse my entire code and write fprintf's at entry and exit but this is very complex. Desperately looking for help.
using windbg, you can also set at each function entry and run a script.
For instance the following command will add a breakpoint on all functions of your module, display the name of the function, the current time, run until the function exit, display the time and continue.
bm yourmodule!* "kcL1;.echotime;gu;.echotime;gc"
Basically this is a function level Time-Based Profiling (TBP). Several tools can help you on this:
Visual Studio Profiling Tools: which is available with Visual Studio Ultimate and Premium version only. http://msdn.microsoft.com/en-us/library/z9z62c29.aspx
Intel vTune: It can do lots of things, including function level profiling. http://software.intel.com/en-us/articles/intel-vtune-amplifier-xe/
AMD CodeAnalyst: it is a free tool. It can work with Intel CPU as well (with limited function, but enough for your purpose). It can do source code level profiling: http://developer.amd.com/cpu/codeanalyst/codeanalystwindows/pages/default.aspx
I suggest you to try with AMD CodeAnalyst first. If you don't have Visual Studio Premium or Ultimate edition.
I assume you are suing c++. You can define a time trace class which display the timestamps
/* define this in a header file */
class ShowTimestamp {
private:
static int level_; // nested level for function call tree
private:
const char *func_;
public:
ShowTimestamp(const char* f) : func_(f) {
std::cout << func_ << ":" << (level_++) << ":begin\t" << GetTickCount64() << std::endl;
}
~ShowTimestamp() {
std::cout << func_ << ":" << (--level_) << ":end\t" << GetTickCount64() << std::endl;
}
};
#ifndef NO_TRACE_TIMER
#define TIMESTAMP_TRACER ShowTimestamp _stt_(__FUNCTION__);
#elif
#define TIMESTAMP_TRACER
#endif
The level_ should be declared in a CPP file separately.
// You need to define the static member in a CPP file
int ShowTimestamp::level_ = 0;
In your code, you can do
int Foo(int bar) {
TIMESTAMP_TRACER
// all the other things.
......
return bar;
}
If you don't want to trace timer any longer, you can just define NO_TRACE_TIMER
Visual Studio is not suited for this; you would have to use WinDbg. It has its own scripting language that would allow you to do what you are seeking. Unfortunately I don't know the first thing about it's scripting language; you will have to read the help file (which is actually more-or-less useful, for once).