Anonymous
Can't see much diff actually
That is the problem. In a union, you can set only one of the fields and read back the same field. For ex, if you set "make" in your example, then you can only get "make" out of it. An attempt to read "model" after setting "make" is Undefined Behavior. So at any point of time, only one of the fields will be set. So it is enough to allocate memory large enough to hold the largest type in the union. So your question doesn't arise at all.
Shahar
Why is it redundant?
Why would one need a collection of data the can be accessed exclusively?
Anonymous
Why would one need a collection of data the can be accessed exclusively?
To save space when you know only of them is going to be used at any point of time. So the example that you chose "Car" is not a good fit for a union. That should be a struct.
Shahar
When you want to store different data, you can use this variable
Anonymous
It's like holding a variable this way: char[max_size_needed] various_vars;
This doesnt take alignment into consideration but the idea is correct.
Shahar
This doesnt take alignment into consideration but the idea is correct.
Yeah. BTW, in the event of the union I asked about, the size was of 30 bytes. But what processor alignment was done here? Processors of 32b/64b have to have alignments of 4/8 multiples respectively, no? Whereas 30 bytes is none of those multiplies
Anonymous
Yeah. BTW, in the event of the union I asked about, the size was of 30 bytes. But what processor alignment was done here? Processors of 32b/64b have to have alignments of 4/8 multiples respectively, no? Whereas 30 bytes is none of those multiplies
No. In your case you only had short and char arrays. The alignment requirement for short is 2 bytes and for char arrays it is byte aligned. So the union will be allocated on an address which is even. It will not be allocated on an odd address.
Luca
hello guys
Luca
is there some way to generate automatically a cmake file based on my way of launching app?
Luca
I'm just compiling by terminal with: "g++ -std=c++11 pkg-config --cflags gtk+-3.0 main.cpp GUI.cpp -o counter2 `pkg-config --libs gtk+-3.0`" I would like to make a cmake to avoid this
Luca
but i'm not able to set it in the correct way
klimi
but i'm not able to set it in the correct way
that shouldn't be that hard, just make simple cmake, set standard and add the gtk+-3.0 library
Luca
i did but it don't work
Anonymous
In c++ many people mistaken data hiding for data security, what does it mean?
Anonymous
A instructor told this
Shahar
But it's not inherent to C++, but to every object-oriented programming language
Anonymous
Ok
Shubh
Write a C program take the pin code from the user and convert in to ascending order using ternary operator(E.g. Input = 382174,Output = 123478)
klimi
I dont see a problem
Shubh
I dont see a problem
what is the answer
Vikas
what is the answer
Use string to store input, then sort the string and print the string.
klimi
what is the answer
I dont see a question
Paa
Hi thanks for adding me
Lavi
Thank you but I am totally new or noob you can say
Lavi
But interested in knowing thing's
Kevin
can someone help me to do this code by reference
Kevin
call by reference#include <iostream> using namespace std; int main(void){ int i, j,n,a[100]; i= 0; cout<<"Ingrese un numero: "; cin>>n; while (n > 0){ a[i] = n % 2; n /= 2; ++i; } for (j=0;j<i;++j) cout<<a[j]; cout<<"\n"; system ("pause"); return 0; }
Kevin
i want to create a function
Kevin
maybe void binary (int* ) or something like that
Talula
i want to create a function
Function of what? to call a function and pass variable by reference. you simply pass it the address of the variable... so void FunctionName(int *Arg) { *Arg = 10; } And pass it using reference... like so int a = 0; FunctionName(&a);
Kevin
.... i'm a little bit confused now
Talula
This is pass by value and not pass by reference
By default C/C++ passes by value.
Anonymous
.... i'm a little bit confused now
Search for "pass by reference in C++" in google
MᏫᎻᎯᎷᎷᎬᎠ
This question is little bit about Qt How to read xml files against a schema? I'm having an xml schema where default values are presented in some attributes, so I need to automatically fallback to those defaults while reading the xml files when those attributes are missing Does anyone know how to do that? I checked out the xml, xmlpattern module API, and haven't found anything relavent so far
Anonymous
By default C/C++ passes by value.
Passing a pointer is not pass by reference. C++ has special compound types called references.
Kevin
#include <iostream> #include <math.h> using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */ void binario(int *); void binario(int * n) { if (*n == 0) return; int val = 0; int exp = 0; int num = *n; while (num != 1) { val = val + (pow(10, exp) * (num % 2)); num = num / 2; exp++; } *n = val + pow(10, exp); } int main(int argc, char** argv) { int n; cin>>n; binario(&n); cout<<n<<endl; return 0; }
Anonymous
I'm sure he is asking about this...
No. This is pass by reference int fun(int& a){ a=5; } int main(){ int a =0; fun(a); }
Kevin
the last code is from a friend
Talula
No. This is pass by reference int fun(int& a){ a=5; } int main(){ int a =0; fun(a); }
Yeah, you're taking reference of the variable when function is called or passing the variable as a reference...
Talula
sooo, can i do something like this with my code?
You could do it either way, both are passing variable by reference.
Anonymous
Yeah, you're taking reference of the variable when function is called or passing the variable as a reference...
This is what C++ programmers refer to as "pass by reference". Your exple was wrong because that expresses "pass by value" semantics as well. It passes a pointer by value
Kevin
A little controversy around here
Anonymous
You could do it either way, both are passing variable by reference.
No. You are wrong. Passing a pointer is called "pass by value" semantics. The pass by value and pass by reference is used to refer to semantics of argument passing and not whether the pointed to variable is being changed underneath. Pass by reference in C++ always refers to reference parameters.
Talula
In C/C++ if variables are passed without and pointers it is pass by value.
Anonymous
In C/C++ if variables are passed without and pointers it is pass by value.
You have very little understanding of the standard usage of the terms as you mentioned in a previous post of yours. So please look it up before arguing further
Idowu Oluwarimi Joshua
I need help ro how to download Maple 2021..can somebody here assist?
Kevin
Thanks, reading
Anonymous
Yep... and I don't want to argue with someone who have a built in hate for me.
I don't have built in hate for you. If correcting you means I have hatred towards you, then by all means assume so. But don't pass of invalid statements are factual here.
Idowu Oluwarimi Joshua
Noted
Talula
I don't have built in hate for you. If correcting you means I have hatred towards you, then by all means assume so. But don't pass of invalid statements are factual here.
Then understand something, you're good but C by default passes variables by value... you don't have to add anything extra.
Talula
What I did is called "Pass by Pointer"...
Kevin
I just wanted to know what it was like to pass by reference
Talula
The question was on C++
Even in C++, it passes all the variables by value by default.
Talula
If you don't specify what you want to do it passes it by value.
Anonymous
Even in C++, it passes all the variables by value by default.
Yes. Even pointers are passed by value. So passing arguments by pointers is not pass by reference in C++
Talula
Yes. Even pointers are passed by value. So passing arguments by pointers is not pass by reference in C++
No... pointers are not passed by values, pointers are memory areas that is passed when you specify... that means the values stored in that memory area can be changed... by reference of where the value is stored in the memory. By value means when variable is passed as a number... like int 45 will be passed as 45, the function cannot change or modify that information.
Talula
Passing by reference allows the variable to be changed in the function.
Kartik
Pass by reference (C++ only) - IBM Documentation https://www.ibm.com/docs/en/zos/2.4.0?topic=calls-pass-by-reference-c-only