This question already has answers here:
sizeof operator returns different values for c & c++?
(2 answers)
Closed 7 years ago.
I just came across this simple code snippet and am wondering why output of this program when it's compiled by a C compiler is 4 and when it's compiled by a C++ one is 8.
#include <stdio.h>
int x;
int main(){
struct x {int a; int b;};
printf("%d", sizeof(x));
return 0;
}
C++ output is rational (8 = 4 + 4 = sizeof(x.a) + sizeof(x.b)), but output of C isn't. So, how does sizeof work in C?
C : https://ideone.com/zj5Qd2
C++ : https://ideone.com/ZZ4v6S
Seems C prefers global variables over local ones. Is it right?
In C, a struct definition like struct x { int a; int b; }; does not define a type x, it defines a type struct x. So if you remove the int x; global, you'll find the C version does not compile.
Related
This question already has answers here:
What happens when I print an uninitialized variable in C++? [duplicate]
(4 answers)
Closed 3 years ago.
I'm trying to learn C++, specifically how to declare and initialize variables. I wrote this code, and I don't know why the variable c is giving a value that I have not assigned it yet.
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!\n";
int a, b;
a = 1;
b = 2;
int d(4);
int result;
auto num = b;
decltype(b) c;
result = a + b - d;
cout << c;
}
The output is -2, but I didn't state c = -2 anywhere!
If you have not initialized the variable, it contains garbage value.
In C/C++, the values declared within a function represent some bytes of main memory on the cpu stack. Those bytes are usually dirty and need initialization. If you don't the values are undefined. That you're always getting '-2' is merely coincidence.
This question already has answers here:
size of reference variable in c++ [closed]
(1 answer)
Why isn't sizeof for a struct equal to the sum of sizeof of each member?
(13 answers)
What are the differences between a pointer variable and a reference variable?
(44 answers)
When to use references vs. pointers
(17 answers)
Closed 4 years ago.
Consider the following program:
#include <stdio.h>
struct A
{
int x;
};
int main()
{
printf("%d\n", sizeof(A));
return 0;
}
As expected, this program prints the number 4 because the integer in A uses 4 bytes. However, let's change the program into this:
#include <stdio.h>
struct A
{
int x;
int& r = x;
};
int main()
{
printf("%d\n", sizeof(A));
return 0;
}
Now the program prints the number 16.
I've always been told that references in C++ works like an alias, as in offering another name for the same variable. Thus, I expected the struct to still have a size of 4 bytes since the reference r is just another name for x, which will get replaced by the actual variable x when compiled.
Obviusly, this doesn't seem to be the case. Instead, it seems that the reference takes up just as much memory as a pointer would do. If we change int& r = x; into int* p = &x; the program will also print 16. From my point of view this doesn't make any sense because why have references if they take up memory just as pointers do, then we might just use pointers instead.
I did one more test program with the following code:
#include <stdio.h>
struct A
{
int x;
int y;
int z;
int& X = x;
int& Y = y;
int& Z = z;
};
int main()
{
printf("%d\n", sizeof(A));
return 0;
}
And the program above prints 40. That seems very weird in my eyes since the 3 integers should take up 4 bytes each, making it 12 bytes in total, and leaving 28 bytes left for the references. But 28 is not evenly divisible by 3.
I've compiled this with Visual Studio 2015 in x64 configuration.
So my question is: Have I done something wrong here or why does a C++ reference use up memory in a struct?
This question already has answers here:
what does this mean char (*(*a[4])())[5]?
(4 answers)
Closed 6 years ago.
so I've been studying pointers, trying to understand them.
I know that in the following line
int f(int ni, int n);
f is a function that accepts two int variables as its input and it returns an int as the result
If I write the following line
int (*f)(int ni, int n);
then f is a function pointer
However, what happens when I write something like?
int (*f[4])(int p);
Thanks for your help.
This is an array of 4 pointers to function, example:
int foo(int p) {
return 0;
}
int (*f[4])(int p);
f[0] = foo;
f[1] = foo;
f[2] = foo;
f[3] = foo;
refer to this link :
Array functions pointer
There's explainations about what is does and how to implement it
This question already has answers here:
Can we overload operators for built-in types like int or float?
(3 answers)
Closed 7 years ago.
I would like to re-define
+
operator.
So, I make a simple code like below code.
int operator+(const int &a, const int &b)
{
int temp = a-b;
return temp;
}
int main()
{
int a = 10;
int b = 5;
cout << a+b << endl;
}
I can re-define with Class type... But I don't want to use Class.
How can I resolve this issue?
You cannot define the operators for plain old data types like int.
You could build your own int class (my_int, say) and include the line #define int my_int. But this would be extremely pernicious and wouldn't work with compile-time evaluated literal expressions.
You cannot redefine operator for int (or any other built in type).
If you say, what do you want to do, we might find some solution.
This question already has answers here:
Why can templates only be implemented in the header file?
(17 answers)
Closed 8 years ago.
I'm trying to learn templates in c++ and i came across with a doubt that i can't find answers for. I'm sorry in advance if this is not a proper question.
If i have the following code:
template< class T >
T func( T a, T b )
{
return a + b;
}
And then:
int number = func( 2, 3 );
Will number simply be set to 5 or will a function
int func( int a, int b )
{
return a + b;
}
be generated?
I need to know if i can make a template that checks if a certain string is in a file.
Both (: The code:
int number = func( 2, 3 );
will instantiate the template function for int type, but compiler may (depending on compiler options) actually optimize it to just:
int number = 5;
A function equivalent to
int func( int a, int b )
{
return a + b;
}
will certainly be generated (unless, of course, it's optimized out). To see this, try
int (*func_int)(int, int) = func<int>; // pointer to instantiated function
int number = func_int(2, 3); // sets number to 5