Anonymous
i think it was because of my kernel's copy/move constructors and copy/move assignment operators not set up correctly
Anonymous
is this the right way to copy data from a std::initializer list? std::initializer_list<double> *data = info->dataBuffer.get( iterator.next()->handle); for (size_t i = 0; i < data->size(); i++) { buffer[index++] = *(data->begin() + (sizeof(double) * i)); }
Dumb
Endless loop
Are you sure? I'd say 1.1
Anonymous
Oh wair
i dont saw the invoice
Anonymous
do all pointer arithmatic in C++ automatically apply the sizeof() in their operation?
Anonymous
eg ptr + (1 * sizeof(double))
Anonymous
eg is this the ONLY reason why sizeof is used with arithmatic operators? (besides finding out the size of an array) int* ptr = (int*)malloc(10 * sizeof(int));
Pavel
do all pointer arithmatic in C++ automatically apply the sizeof() in their operation?
Yes, pointer is shifted with regards to the size of the type
Anonymous
eg if you do * sizeof(T) ANYWHERE else other than malloc/realloc it will be incorrect?
Pavel
eg if you do * sizeof(T) ANYWHERE else other than malloc/realloc it will be incorrect?
Well, if you're working with raw memory (e.g. packing something into array of char/std::byte), or you have your pointer to memory as void* then it may still be needed. Not very common situation though in C++
Anonymous
ok :)
Anonymous
yay i got it to serialize 🙂
•‿•
Anonymous
@Ritardev AntiBilo You are both wrong. Notice that in the code, the OP is initiating the float variable with 1.1 which is a double. If it were 1.1f and the comparison were also against 1.1f then the code will print 1.1 because the compiler will optimize away the test. But if it is a double and the underlying implementation uses IEEE floating point standards then the comparison will fail right away and nothing will get printed. Read the link that I included in reply to the OPs post to understand why this happens.
Anonymous
So in C and C++ , the floating numbers are handled just like in java? (With F or D suffix I mean)
C,C++ and Java all now use IEEE floating point standards. But the old C and C++ standards didnt do so. But now all of them follow the same standards.
Dumb
Ah, I did not know Thanks
Anshul
When we pass array to functions, they're passed by reference right? So when in the function we right it Void fun(int a[ ]) { } Here in function what is a? Is it an array or a pointer or what else
Anshul
Then how are they passed? The tutorial from where I studied it said, they're passed by reference
Anonymous
Then how are they passed? The tutorial from where I studied it said, they're passed by reference
The tutorial is wrong. Arrays are passed by value as well except that in the case of arrays the value passed is a pointer to the first element in the array. This is because array names decay to a pointer to its first element in most cases. If I change the pointer inside the function, like make it point to some other location, it wont affect the array in the call site.
Anonymous
Are you sure? https://ideone.com/XVQkFR
You are changing the array element indirectly through the pointer. This has got nothing to do with pass by value or pass by reference.
Anonymous
OK, how would I pass the array by value then? The syntax
Arrays are passed by value (as described in my post above). The value that is passed is a pointer. For ex void fun(int a[]){ a[0]=5; //this changes the elements //in the array indirectly and //is not related to pass by //value or pass by reference a = new int{5}; delete a; //this does not affect the array //and this is what is meant by //pass by value semantics } int main(){ int array[] = {0,1,2,3,4}; fun(array); //after this call you can still //access array and array[0] //will be equal to 5 }
Pavel
Because an array can have some place in memory and some lifetime, the value that we get in the function doesn't have the lifetame of the function body
Anonymous
So you mean in any case a pointer to the array is passed? Or you mean that terms "array" or "pointer to array" used interchangeable?
Array names decay to pointers when they are passed as arguments to a function that is not expecting a reference to the array. This decayed pointer is then passed by value which is what I meant by pass by value semantics. Pointer to array is different from pointer to 1st element in the array. Read my replies again.
Anonymous
Then saying that "arrays passed by value" is misleading, what being passed is not an array anymore
Which is why I made it clear in my replies with "except that in the case of arrays the value passed is the pointer to the first element in the array". That is why i asked you to read my replies again.
Pavel
Which is why I made it clear in my replies with "except that in the case of arrays the value passed is the pointer to the first element in the array". That is why i asked you to read my replies again.
I've read your messages, I just saying that your answer is misleading It's like saying that if I call void func(int& i), i is passed by value, but the value passed is pointer to i
Pavel
You are passing by reference here.
ok, yes, by reference, the value passed is the pointer (memory address) anyway
Pavel
the meaning is different anyway
Anonymous
ok, yes, by reference, the value passed is the pointer (memory address) anyway
This is pass by reference and not pass by value. I will give an example for pass by value with pointers void fun(int* p){ *p=1; p = nullptr; } int main(){ int * a = new int{5}; fun(a); //*a will be 1 now and a //will remain a valid pointer. //it wont be nullptr } Here the pointer is passed by value. This is exactly what happens with arrays as well. The above example is "pass by value" semantics.
Anonymous
In this case, yes, but you are passing a pointer you can't say that you are passing the array by value here
There is no array here. And in my replies I mentioned clearly that arrays decay to pointers and it is the pointer that is passed by value. You should understand the full post before commenting on it.
Pavel
There is no array here. And in my replies I mentioned clearly that arrays decay to pointers and it is the pointer that is passed by value. You should understand the full post before commenting on it.
right, I've misread the braces I say that I've read your messages but I say that the original statement is misleading, even if you then clarified that you meant different thing I mean again, if array is decayed to pointer the pointer to the array is not the array anymore
Pavel
but OK, I think we just speak different languages here
Anonymous
this
What is the meaning of that line starting from the word except?
Anonymous
What is the meaning of that line starting from the word except?
I didnt clarify my statement. You misunderstood what I said and came up with an example that has got nothing to do with either pass by value or pass by reference. I was just clearing that misconception for you.
Pavel
Please do.
How I read it The tutorial is wrong. Arrays are passed by value as well except that it is not the array that is being passed but a pointer to the array. The rest is unchanged
Anonymous
How I read it The tutorial is wrong. Arrays are passed by value as well except that it is not the array that is being passed but a pointer to the array. The rest is unchanged
Not a pointer to the array but pointer to the first element in the array. Let me quote it again. "The tutorial is wrong" - This is because it said arrays are passed by reference which is not true. Just because you can change the underlying elements of the array doesnt mean the array is passed by reference. Incidentally you came up with the same mistake that the tutorial made. "Arrays are passed by value as well except that in the case of arrays the value passed is a pointer to the first element in the array" - When you have a function declared like any of these void fun(int* a) void fun(int a[]) void fun(int a[10]) void fun(int a [20]) they are all technically the same declarations. The parameter is of type int* in all cases and the array dimensions are ignored. Now when we talk about pass by value or pass by reference semantics for fun, it is pass by value as I demonstrated a couple of posts above. Since arrays decay to pointer (a rvalue) to their first element in calls to fun, this pointer is then passed by value. This is what I meant by pass by value semantics.
Pavel
Not a pointer to the array but pointer to the first element in the array. Let me quote it again. "The tutorial is wrong" - This is because it said arrays are passed by reference which is not true. Just because you can change the underlying elements of the array doesnt mean the array is passed by reference. Incidentally you came up with the same mistake that the tutorial made. "Arrays are passed by value as well except that in the case of arrays the value passed is a pointer to the first element in the array" - When you have a function declared like any of these void fun(int* a) void fun(int a[]) void fun(int a[10]) void fun(int a [20]) they are all technically the same declarations. The parameter is of type int* in all cases and the array dimensions are ignored. Now when we talk about pass by value or pass by reference semantics for fun, it is pass by value as I demonstrated a couple of posts above. Since arrays decay to pointer (a rvalue) to their first element in calls to fun, this pointer is then passed by value. This is what I meant by pass by value semantics.
Yes, I understand your point (in any case, thanks for the examples). I really think we're speaking different languages. What I'm trying to say, that using word "arrays" in the sentence "Arrays are passed by value as well except that in the case of arrays the value passed is a pointer to the first element in the array" is misleading and not entirely correct. It's not an array that being passed, but a pointer to the first element of the array. I guess in some cases "array" and "pointer to the first element of the array" can be used interchangeably 🤷‍♂️. UPD: (added more "the first element of")
Anonymous
Yes, I understand your point (in any case, thanks for the examples). I really think we're speaking different languages. What I'm trying to say, that using word "arrays" in the sentence "Arrays are passed by value as well except that in the case of arrays the value passed is a pointer to the first element in the array" is misleading and not entirely correct. It's not an array that being passed, but a pointer to the first element of the array. I guess in some cases "array" and "pointer to the first element of the array" can be used interchangeably 🤷‍♂️. UPD: (added more "the first element of")
You are wrong again. It is not a pointer to the array. It is a pointer to the 1st element in the array. And arrays decaying to pointers to their first element is a standard requirement for most of the cases. So in all these cases, an array can be treated as a pointer to the 1st element and you can then use the pass by value or pass by reference tests on this pointer. This is the reason why it is not technically wrong to say arrays are passed by values.
Anonymous
Anyone needs help i can help you
Anonymous
So you say that "pointer to the first element of the array" can be referred as "array" as well? Or only in the situation with a function call?
It can be in all the situations where the standard allows an array name to decay to a pointer to its first element. Function calls is one such case.
Feruza
OK, now it makes sense. Thanks
Hi there! Can I dm you
Feruza
I'm a beginner and I'm not sure how to explain my problem without the screenshot of my code
Anonymous
Guys it seems my c compiler has some problem i can make a c progect in codeblocks or visual studio code
Anonymous
What should I do
Anonymous
and what is wrong?
I just fixed it . Sorry
klimi
I just fixed it . Sorry
what was the problem?
Anonymous
what was the problem?
I couldn't compile the code also whole code had no color
Anonymous
and what was the fix? (i am curious)
Oh no i thought its fixed because the colors are back but it still gives this error : The compiler setup (GNU GCC Compiler) is invalid so Code::Blocks cannot find/run the compiler.
klimi
when i used to use windows, i have downloaded the code::blocks with mingw version, that one worked
Anonymous
Its mingw version
Anonymous
I have to use Ubuntu again run by virtual machine
klimi
maybe you could try installing mingw separatedly, but once again, i don't use windows and i don't know anything about it...
klimi
may I know which OS you use?
bunch of linux distros
Pradevel (Pratyush)
bunch of linux distros
Okay but which ones 👀
klimi
Can I ask what do you use?
i mainly use gcc with clangd
klimi
Okay but which ones 👀
arch, ubuntu, 20.04, gentoo, rhel 7/8, debian 10, fedora 34...