Prince Of Persia
yeah that's better but in C++11 and more that's not recommended
char* is good but so many bugs could bring with your code (it's hard to be controlled as it isn't an object with constructor and deconstructor)
\Device\NUL
I agree for your opinion, but it's too overpower for simple task like that
\Device\NUL
If it wouldn't changed at run time i will prefer declare it as a macro or array of pointer char.
Prince Of Persia
> so many bugs What ?
because that's C way to doing things C++ is OOP to make things easier for programmer C is not OOP so it's harder to code on it but it is a bit faster in runtime
\Device\NUL
because that's C way to doing things C++ is OOP to make things easier for programmer C is not OOP so it's harder to code on it but it is a bit faster in runtime
No, I mean. What's the cons that using const char * than std::string for string that didn't changed at runtime ?
Misagh
char* is good but so many bugs could bring with your code (it's hard to be controlled as it isn't an object with constructor and deconstructor)
He's saying const char* not char* = new char[] Basically pointing to the read-only string literal pool in the memory.
Misagh
(using std::string_view is certainly better but requires c++17)
Prince Of Persia
No, I mean. What's the cons that using const char * than std::string for string that didn't changed at runtime ?
I'm not so expert but I know that std::string has these things that const char * doesn't 1-it has 3 iterator but const char * only has one pointer (so sizeof(std::string) == 3*sizeof(const char *)) 2-it allocates more than what is in use (it's bad as you said it won't be changed in runtime) 3-it has simpler interface (it's an class with constructor and deconstructor no need to new and delete[])
Prince Of Persia
www.learncpp.com Good one
I'm learning C++ for 4 years but I still feel I don't know nothing
Amrith
I'm learning C++ for 4 years but I still feel I don't know nothing
Try building real applications. Contribute to open source projects that will help
Prince Of Persia
Try building real applications. Contribute to open source projects that will help
I should do so the realest project I have worked on was a simple interpreter
\Device\NUL
Did compiler give you any messages ?
Prince Of Persia
you did increase in switch case
Prince Of Persia
you should put unconditioned break in your switch
\Device\NUL
don't blame me for 1st that it's only 16Byte more (in my pc) it is sometimes important to have something as small as you can
const char * allocation is on data segment. It's much faster because std::string allocation is on heap. heap is heavier and need to catch error if coudn't allocate memory from heap.
\Device\NUL
No, i ask you about compiler log
Prince Of Persia
you should put unconditioned break in your switch
and erase those a++ in switch and put them in the end of your loop or in for loop
Alviro Iskandar
const char * allocation is on data segment. It's much faster because std::string allocation is on heap. heap is heavier and need to catch error if coudn't allocate memory from heap.
Wrong. const char * is not an allocation, it does not even imply that the data lives in .data section. const char * simply says: You can't write to memory via this pointer. const here is just a compile time check that validates: a dereference only works for reading.
Alviro Iskandar
const char *p = malloc(100); lives on the heap
\Device\NUL
It it lived on .rodata ? 🤔
Alviro Iskandar
Alviro Iskandar
It can live everywhere.
\Device\NUL
It can live everywhere.
So why char *str = "defined string" in C++1 need to implicitly declare it with const word ?
Alviro Iskandar
So why char *str = "defined string" in C++1 need to implicitly declare it with const word ?
Because that string literal is read only, so it does not make any sense to write via that pointer
Alviro Iskandar
Prince Of Persia
optimize what?
to constexper it's only a string literal
Misagh
So where the string literal live ?
I do believe this is compiler implementation details. but usually it's in the text/code segment.
Alviro Iskandar
So where the string literal live ?
in that example it can live in .rodata, but that does not prove anything about your const char * statement
Alviro Iskandar
I do believe this is compiler implementation details. but usually it's in the text/code segment.
that string is not necessary to be executable, so placing it on .text section is an not ideal thing to do, but yes .text is read only by its nature
Alviro Iskandar
could be implementation defined
Leovan
Advice me any articles about how C-style cast work under the hood pls
\Device\NUL
VLA?
Variable length array
\Device\NUL
It exists on C but not C++
Anonymous
Variable length array
So is okay to use variables as length
\Device\NUL
So is okay to use variables as length
I will use macro but this is fine size _t sz = 100 type arr[sz] Compile with clang++ with -pedantic-errors no warn/errors
Prince Of Persia
Anonymous
What is that?
Length of array varies I guess
Anonymous
You can't use std::begin() and std::end() on it
Haven't tried it, but is best if std::array is used instead
Anonymous
C-style arrays should be avoided all together
Prince Of Persia
C-style arrays should be avoided all together
I see but sometimes all you need is C with OOP
Prince Of Persia
C-style arrays and C-style strings are faster and more efficient than std ones
-•𝐴𝑚𝑖𝑟.𝐺ℎ-
cpp is powerful 😕
-•𝐴𝑚𝑖𝑟.𝐺ℎ-
yeda
hi iman
Iman
hi iman
Hi daz
-•𝐴𝑚𝑖𝑟.𝐺ℎ-
Hi daz
saghez hava chetow?
مــــــــســــــعـــود
فارسی شد؟
Anonymous
Any c programers
Anonymous
?
Anonymous
ohhk dude
Artur
That’s*
~
Hello, I'm trying to do my homework in C and C++ but I couldn't, can you help me?
~
the homework is: Write a program where two different queue structures use the same array. The first of the queues you will write will try to use the array from beginning to end, while the second will use it from the end to the beginning. Do not forget that queues may conflict and create a mechanism to prevent this. One queue may use more space in the array than the other queue.
Anonymous
C-style arrays and C-style strings are faster and more efficient than std ones
Woah!! It may be but the standard library is faster and more efficient than you think Is the best written algos, and containers to name a few
\Device\NUL
What is that?
int a; //input a; int arr[a]; This is variable length array
mr.wyt
Hi
\Device\NUL
It's bad because it's dangerous (stack overflow) and slow
\Device\NUL
https://t.me/programminginc/423156
Anonymous
int a; //input a; int arr[a]; This is variable length array
Do people still do that? Is better to use dynamic memory the heap
پویا رحیم
Hi, question ! I used std::size_t in for loop to printing my array elements but it doesn't work(nothing prints), what's the reason ? I compiled that with gcc 11.1 and clang 5.0, but it still doesn't work without any error 🤯. int main() { srand(time(0)); std::array<std::size_t,10> arr; for(std::size_t i = 0; i<10; i++) arr[i] = rand()%9; for(std::size_t j = 9; j>-1; j--) std::cout << arr[j] << std::endl; }
Marcio
In truth, string[sizeofstring-i-1]