Anonymous
But if I write like this
Anonymous
But if I write like this
#include <iostream> int main() { int sum = 0, val = 1; while (val <= 10) { Difference val++; Difference sum += val; } std::cout << "Sum of 1 to 10 inclusively is " << sum << std::endl; return 0; }
klimi
You switched 2 lines
klimi
In first code you sum 1 + 2 + 3 .... And in second you sum 2 + 3 + 4 ....
Anonymous
for(int value = 0; value <= 10; value++) { sum += value; } for(int value = 0; value <= 10; value++) sum += value; Which one is a good habit? Including curly brackets or not?
Anonymous
Language: c Source: #include <stdio.h> int main() { char arr[3] = "hell"; puts(arr); } Warnings: 1015575703/source.c: In function ‘main’: 1015575703/source.c:4:19: warning: initializer-string for array of chars is too long char arr[3] = "hell"; ^~~~~~ Result: hel Note: gcc assumed, other valid options are clang, visual_c, you can be more specific next time.
Pavel
why this warning? arr[3] means there's 4 index right? 0,1,2,3
no, only 3: 0,1,2 also "hell" probably needs zero char at the end ('\0') so it probably (not completely sure) needs 5 chars
Nameful
whats that '\0' for?
It says that the string ends here
Anonymous
It says that the string ends here
if , suppose char arr[] = "hell"; outputs hell I haven't given '\0' in it, how did it know it ends there ?
Nameful
yes
Send source
Anonymous
Send source
#include <stdio.h> int main() { char arr[] = "hell"; puts(arr); }
Anonymous
I was using an online compiler
Nameful
Language: c Source: #include <stdio.h> int main() { char arr[] = "hell"; puts(arr); } Result: hell Note: gcc assumed, other valid options are clang, visual_c, you can be more specific next time.
Pavel
if , suppose char arr[] = "hell"; outputs hell I haven't given '\0' in it, how did it know it ends there ?
as far as I know string literals (the thing you put into code inside two doublequotes) already have it https://ideone.com/BvfWuw
Pavel
But it is not printed when you print the string because it is a terminator (the idea is that the sting to work on is everything from the beginning and before the terminator)
Nameful
Language: c Source: #include <stdio.h> int main() { char arr[4] = {'h', 'e', 'l', 'l'}; puts(arr); } Result: hell Note: gcc assumed, other valid options are clang, visual_c, you can be more specific next time.
Nameful
Interesting …
Anonymous
As far as I know
where does it get the space to put that character in the string?
klimi
movl $1819043176, -12(%rbp) leaq -12(%rbp), %rax movq %rax, %rdi call puts@PLT
Pavel
where does it get the space to put that character in the string?
it allocates one more char when you ask it for char[], basically "hell" can use 5 chars to be stored (not sure if it always should) I've updated the code above to show how this '\0' affects the logic, note that sizeof(arr) is five
klimi
i don't see anywhere allocating 5 chars hm
klimi
i think p means pointer... so... hm
Anonymous
it allocates one more char when you ask it for char[], basically "hell" can use 5 chars to be stored (not sure if it always should) I've updated the code above to show how this '\0' affects the logic, note that sizeof(arr) is five
#include <stdio.h> int main() { char arr[4] = "helloWorld"; puts(arr); printf("%d",sizeof(arr)); } OUTPUT : hell 4 when I put arr[4] is it allocating 5 spaces?
Pavel
i don't see anywhere allocating 5 chars hm
I'm not an expert here, I know very little C, so I can misunderstand what char arr[] = "hell" means;
klimi
i think that compillers is smart enough than when it sees it is only 4 bytes it ends the stream and doesnt run into memory
Anonymous
you've specified size of [4], if will use the size that you have specified and copy first 4 characters there
so if I put arr[4] = "hello"; is the index, 0 - h 1 - e 2 - l 3 - l or 0 - h 1 - e 2 - l 3 - l 4 - \0 ?
Anonymous
First variant
but sizeoff(arr) is 4, that fills the "hell" in "hello" so you did they put a "/0" in an already filled arr ?
klimi
I think its undefined behaviour
klimi
And we are lucky cuz there was 0
Nameful
Maybe the bot zeroes the memory
Nameful
To prevent leaking information
klimi
write(1, "hell\n", 5) = 5
Pavel
but sizeoff(arr) is 4, that fills the "hell" in "hello" so you did they put a "/0" in an already filled arr ?
sizeof is size of array in chars, you specified your array size as 4 chars, so sizeof returns 4.
Pavel
but sizeoff(arr) is 4, that fills the "hell" in "hello" so you did they put a "/0" in an already filled arr ?
Here's how it looks, note that first time it prints two strings, it's because the strings are going one after another in memory (because they are in the same struct) and the first one lacking '\0' at the end https://ideone.com/InKWga
klimi
klimi
in memory there is nullbyte! aaaaaaaaaaaaaa
Pavel
Here's how it looks, note that first time it prints two strings, it's because the strings are going one after another in memory (because they are in the same struct) and the first one lacking '\0' at the end https://ideone.com/InKWga
And that's because it doesn't carry the information about the size of the char array to printf, basically what printf have is only pointer, it doesn't know what size string is, so it needs the '\0' at the end to figure out that the string is finished. If you know that some string is always will be of a specific length (e.g. a GUID or some specific format) you maybe don't need this zero char at the end and can store your strings without it, but if you don't know in advance the size of the string, then you need either pass the size together with string or have this zero at the end.
klimi
yes... it is undefined behaviour it reads next bytes
klimi
char arr[4] = {'h', 'e', 'l', 'l'}; char next[4] = {'A','A','A','A'}; puts(arr); klimi@14are05~/testing$ ./a.out hellAAAA
Pavel
char arr[4] = {'h', 'e', 'l', 'l'}; char next[4] = {'A','A','A','A'}; puts(arr); klimi@14are05~/testing$ ./a.out hellAAAA
But I guess it's also not guaranteed that they will be in memory like this one after another, the example by the link above is more reliable :)
klimi
they are saved onto stack, so you create 4bytes and 4bytes
klimi
they should be next to each other
Ammar
Or even elided if the optimization is on.
Pavel
they should be next to each other
I guess the compiler can decide that it wants them in another order as it does with other variables sometimes 🤷‍♂️
klimi
idk
klimi
but thats not the point
Ammar
ammarfaizi2@integral:/tmp$ cat test.c #include <stdio.h> int main() { char arr[4] = {'h', 'e', 'l', 'l'}; char next[4] = {'A','A','A','A'}; puts(arr); } ammarfaizi2@integral:/tmp$ gcc test.c -O0 -otest ammarfaizi2@integral:/tmp$ ./test hellAAAA ammarfaizi2@integral:/tmp$ gcc test.c -O3 -otest ammarfaizi2@integral:/tmp$ ./test hell ammarfaizi2@integral:/tmp$
Pavel
C++ question Do we have some way of rounding/flooring float with some amount of digits after zero (not necessary decimal digits)? Something better than two multiplications and round/floor (static_cast<float>(static_cast<int>(floatVal * 100))*0.01f), I'm thinking maybe we can directly zero some bits in mantissa depending on value of exponent, or maybe there's already some function for that?
Pavel
My task, I want to store floating points as a key in a map, but they can be slightly off and I want nearly equal values to be stored in the same node of the map like I want these two to give me the same value from the map: myMap[shorten(3.1415926)] and myMap[shorten(3.141592)] and I want to implement this shorten function for that
Pavel
There is std::hash methods for floats and doubles. Why do you want to shorten them?
Maybe shorten isn't the best name of the function. I can have some values that are nearly equal, close enough that for my algorithms they are basically the same. But if I store them as key in the map, then they are checked for exact equality. I don't know how std::hash implemented, but I expected it to return the same hash (if no collisions) only for exactly equal floats, if it returns the same hash for nearly equal floats, then it is probably what I need.
Pavel
What’s the point?
I descried it in the next messages
Ehsan
When you assign a float it’s 32 bit whether you round it or not
Ehsan
I descried it in the next messages
It’s just an extra overhead that you can rid of
Ehsan
what do you mean by overhead?
I mean things will not go faster because you rounded up
Ehsan
32 bit is 32 bit
Ehsan
And float operations are expensive
Pavel
I mean things will not go faster because you rounded up
I don't want them to go faster, I need to use float as keys in maps (already written it twice with example above)
Pavel
I don't want them to go faster, I need to use float as keys in maps (already written it twice with example above)
It would be nice though to "round" it more efficiently, I guess compiler can optimize multiplication and division if it knows what I multiply and divide with at compile time
Anonymous
Here's how it looks, note that first time it prints two strings, it's because the strings are going one after another in memory (because they are in the same struct) and the first one lacking '\0' at the end https://ideone.com/InKWga
wait, so idk if my understanding is correct or not, but from observing that example code, so, suppose, char myCharArr[4] = "hello world"; when it is printed it shows "hell". now myCharArr[4] doesn't have a \0 and by default assuming that the char someGarbage[] = "\0"; then, memcpy(data.shortArr, myCharArr, sizeof(myCharArr)); memcpy(data.someGarbage, someGarbage, sizeof(someGarbage)); Now, this will make data.shortArr "hell\0" (memory is next to each other since they are in same struct) and finally when printing it was able to print "hell" Is my understanding correct? Anything wrong here? All of this based on your https://ideone.com/InKWga
Pavel
wait, so idk if my understanding is correct or not, but from observing that example code, so, suppose, char myCharArr[4] = "hello world"; when it is printed it shows "hell". now myCharArr[4] doesn't have a \0 and by default assuming that the char someGarbage[] = "\0"; then, memcpy(data.shortArr, myCharArr, sizeof(myCharArr)); memcpy(data.someGarbage, someGarbage, sizeof(someGarbage)); Now, this will make data.shortArr "hell\0" (memory is next to each other since they are in same struct) and finally when printing it was able to print "hell" Is my understanding correct? Anything wrong here? All of this based on your https://ideone.com/InKWga
I specified the sizes of the arrays so char myCharArr[4] = "hello world"; assigns only the first 4 chars without '\0'. Then memcpy copies this data "as is" to data.shortArr. So this array has only this 4 chars. someGarbage is an arrays of 16 chars, when I assign "some garbage" there, it sets first 12 chars to represent the symbols of the string, then sets 13th char to zero ('\0'), the last three chars probably left as zero (not sure if in C they are initialized or not to zero), but we don't care at all here about these last three chars. When I memcpy the someGarbage array I copy all 16 chars "as is". What I wanted to show, that if you print your array of 4 chars withour '\0' at the end, then it's matter of luck whether it will be printed correctly or not (as in klimi's example there can be non-zero memory after your array on the stack and then it will interpret memory as part of your string until it find a zero byte). You don't want to rely on something that works by luck.