Different variable address between g++ -std=c++11 and visual studio 2013 - c++

When running this code
#include <cstdlib>
#include <cstdio>
int main() {
char b;
char c;
printf("%d\n", &b - &c);
return 0;
}
I got 12 using Microsoft visual studio 2013, and -1 using g++ -std=c++11 under Ubuntu 14.04. What is the reason for this difference?
Or did I make a mistake when testing variables' memory addresses?

It is actually implementation specific. For the given code:
#include <cstdlib>
#include <cstdio>
int main() {
char b;
char c;
printf("%p %p %u\n", &b, &c, &b - &c);
return 0;
}
GCC and MinGW gave an output of 1 for me. Judging by the values of &b and &c, we can say that memory was allocated continuously for two chars b and c. However, other compilers like VS-2013 and Intel C++ compiler will give some other values depending on how the allocation of memory is done.

Related

how to resolve Error 193:%1 is not a win32 application in dev c++?

unable to compile even the hello world programe
#include <iostream>
using namespace std;
int main(){
cout <<"hello world";
system("pause");
return 0;
}
Dev ++ clearly compiles to 16 bit architectures and they are deprecated and unsupported since exactly Windows Vista. So you clearly have to use a different compiler.

CUDA - Simple adder program always gives zero

I have a problem with a simple CUDA program which just adds two numbers. I run it on a laptop with a Geforce GT320M GPU on Windows 7. I compile this program with Visual Studio 2013 (I don't know if it means something). The problem is that I always get 0 as a result. I tried to check the given parameters (just return with all the parameters given to the method in an array) and they all seemed to be 0. I run this program in an other computer (at university) and there it runs completely fine, and returns the correct result. So I think there should be some setting problem, but I am not sure of it.
#include <cuda.h>
#include <stdio.h>
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
__global__ void add(int a, int b, int* c)
{
*c = a + b;
return;
}
int main(int argc, char** argv)
{
int c;
int* dev_c;
cudaMalloc((void**)&dev_c, sizeof(int));
add << <1, 1 >> >(1, 2, dev_c);
cudaMemcpy(&c, dev_c, sizeof(int), cudaMemcpyDeviceToHost);
printf("a + b = %d\n", c);
cudaFree(dev_c);
return 0;
}
I also run this code snippet that I found somewhere.
cudaSetDevice(0);
cudaDeviceSynchronize();
cudaThreadSynchronize();
This isn't returning anything.
If you are using the typical CUDA template to create a new Visual Studio project using CUDA, then you have to take care of correctly setting the compute capability for which to compile, changing the default values if needed. This can be done by setting, for example,
compute_12,sm_12
in the CUDA C/C++ Configuration Properties. In your case, the default compute capability was 2.0, while your card is of a previous architecture. This was the source of your mis-computations.
P.S. As of September 2014, CUDA 6.5 is the only version of CUDA supporting Visual Studio 2013, see Is Cuda 6 supported with Visual Studio 2013?.

Why a variable length array compiles in this c++ program?

It is said that arrays are allocated at compile time, then the size must be const and available at compile time.
But the following example also works, Why?
#include <iostream>
#include <vector>
using namespace::std;
int main()
{
vector<int> ivec;
int k;
while(cin>>k)
ivec.push_back(k);
int iarr[ivec.size()];
for (size_t k=0;k<ivec.size();k++)
{
iarr[k]=ivec[k];
cout<<iarr[k]<<endl;
}
return 0;
}
Compile your code with -pedantic.
Most compilers support variable length arrays through compiler extensions.
The code works due to the compiler extensions, However as you noted the code is non standard conforming and hence non portable.

Why do statements after return change the return value?

C++ returns invalid value in the following code:
#include <iostream>
#include <vector>
using namespace std;
int f(){
vector< int * > v[2];
return 1;
v[1].push_back(NULL);
}
int main(){
cout << f();
}
The output is:
205960
When I commnet line after return, it works fine:
#include <iostream>
#include <vector>
using namespace std;
int f(){
vector< int * > v[2];
return 1;
//v[1].push_back(NULL);
}
int main(){
cout << f();
}
The output is:
1
I am using code::blocks with mingw32-g++.exe compiler. The mingw version is: gcc version 4.4.1 (TDM-2 mingw32).
Your compiler has a bug. Fortunately, it is also obsolete. You should upgrade — G++ is up to version 4.6.2, which also implements much of C++11, which is very useful.
If you choose to stick with an older compiler, that is also a decision to accept its flaws.
Edit: If you are really stuck with 4.4 (for example due to a PHB), that series is still maintained. You can upgrade to GCC 4.4.6, released just this past April.

ICC segfaulting with variable length arrays

So, when compiled with the basic icc bob.cpp -o bob and run, the following code segfaults:
#include <string>
int foo () {
return 6;
}
int main() {
std::string t[foo()];
}
The following two similar programs, however, seem to run fine.
#include <string>
int foo () {
return 6;
}
int main() {
int f = foo();
std::string t[f];
}
and
#include <string>
int foo () {
return 6;
}
int main() {
std::string t[6];
}
I'm a bit confused about what's going on. Apparently, variable length arrays are non-standard, and this was a surprise to me since I've always used g++ which supports it. However, if it's not supported by ICC, why would it compile? Also, why would example 2 "work"?
What is correct code here, and, if the first snippet is incorrect, why does it compile, and then why does it segfault?
I'm using icc (ICC) 12.0.2 20110112 on 2011 x86_64 Intel(R) Core(TM) i5.
Thanks
Well, while it is true that C++ has no variable-length arrays (C99 does though), apparently ICC does support them as an extension, since your code actually compiles (and since your second snippet actually runs without crashing).
If the first version crashes, then it must be a bug in ICC's implementation of that non-standard extension.