Sharif
are you sure? it’s working correctly
http://cpp.sh/ code tested by this site and result is This numbers are: 19 10 8 17 9 15
Sharif
so? what’s the problem?
I'm working by CodeBlocks offline then showing 4354238 4354238 4354238 4354238 4354238 4354238 4354238 4354238 4354238 at result window.
Sharif
It happens to me sometimes... I just restart my IDE and then it works fine
If I use the for (int n:x){ cout << n << " "; } then the problem showing But: for (int i=0; i<6; i++){ cout << x[i] << " "; } showing fine. Experts help me to understand please.
Bumpy
HOW I CAN USE C++ IN WINDOWS
Bumpy
AND COMPILE WITH CLANG-9?
Dima
lol
Mihai
AND COMPILE WITH CLANG-9?
Turn caps-lock off
Igor🇺🇦
HOW I CAN USE C++ IN WINDOWS
WHY DO YOU SCREAM? https://devblogs.microsoft.com/cppblog/clang-llvm-support-in-visual-studio/
Anonymous
Hi
Ttfsefghh
What is the O(n) ?
Pavel
No
What compiler your CodeBlocks uses, can you look it up? I haven't used CodeBlocks so not sure how to find it there
Pavel
What is the O(n) ?
linear complexity https://en.wikipedia.org/wiki/Big_O_notation
Phil
hey, for the structure typedef struct { float m; unsigned int e; }Monom; So if I do Monom *polynome=(Monom *) malloc(s); ``` when I do sizeof(polynome) I get always 8.. But I want the size of my array... How can I do ? Thanks in advance
Ttfsefghh
Thanks bro
Ehsan
Thanks bro
you could have searched for 1 second in google and got the answer 🙂
Ehsan
this is not a search engine
Ttfsefghh
But for data struct
Sharif
What compiler your CodeBlocks uses, can you look it up? I haven't used CodeBlocks so not sure how to find it there
No, I'm not sure about that. That's mean I have to study more about CodeBlocks. Right?
Phil
array polynome
Pavel
No, I'm not sure about that. That's mean I have to study more about CodeBlocks. Right?
It just feels that you use some old compiler, I wanted to check that https://superuser.com/questions/206157/checking-version-of-gcc-compiler-in-codeblocks
Ehsan
which array?
polynome is a struct pointer not an array
Anonymous
polynome is a struct pointer not an array
pointer to array allocated in heap/dynamic memory I think.
Phil
polynome is a struct pointer not an array
so how can I declare that as an array?
Ehsan
it has 8 bytes because int is 4 bytes float is 4 bytes
Ehsan
4+4 = 8 bytes so c++ allocated 8 bytes in the heap
Phil
It's written xclusively in C but yes you right
Ehsan
so how can I declare that as an array?
you want an array of a struct?
Pavel
Ok thanks
Just if you find out what version it uses, check that it's not very old, and if it is old, just update your compiler.
Ehsan
easy: Monom *polynome = malloc(numberOfElements * sizeof(Monom))
Ehsan
this will allocate numberOfElements*8 bytes in the heap
Ehsan
you can access them like a normal array(c will take care of the offset)
Phil
ok so the problem is still there
Phil
so i called the function Monom * poly_write(){ ... } In main I use : Monom *a=poly_write(); sizeof(a); //still equal to 8
Phil
so the problem come from a ?
Ehsan
so the problem come from a ?
can you elaborate I don’t understand 🙂
Phil
easy: Monom *polynome = malloc(numberOfElements * sizeof(Monom))
I tried to show sizeof in the function where I created it and still have 8 not the size of the array polynome
Diego
I’m in vscode
Personally I think you'll fare SOOOOO much better in Visual Studio buuuut
Diego
Just download a CLI compiler like GCC or a GCC extension in VS Code
Diego
I need clang..
Then... Do the same but for Clang
Anonymous
I need clang..
https://releases.llvm.org/download.html
Phil
int sizeof_array = sizeof(Monom) * number_of_elements;
How do i get teh number_of_elements that is my question
Anonymous
How do i get teh number_of_elements that is my question
sizeof operator can't figure out how many elements your array have.
Ehsan
I tried to show sizeof in the function where I created it and still have 8 not the size of the array polynome
When you type sizeof(polynome) it’s kinda like sizeof(polynome[0]), that’s why it says 8
Ehsan
sizeof operator can't figure out how many elements your array have.
exactly, this is only possible with char arrays(because of the string terminator).
Ehsan
it’s also a bad practice if you allocate amount of memory that you don’t know in the heap 🙂
Phil
Ok thanks for the informations, i wasn't aware of it. So there is a tips to know th length of an array ?
Diego
Save it at the time of declaration
Assuming it's a dynamic array, people usually wrap it in an object Which holds both the array and an int stating the size
Phil
Just define the size: #DEFINE SizeOfArray = 10, for example
Hmm I can't do that it's dynamic
Diego
A vector I think would be your best bet in STD
Ehsan
Hmm I can't do that it's dynamic
if you want a dynamic array you can define a counter that gets incremented everytime you allocate a new element
Anonymous
Ok thanks for the informations, i wasn't aware of it. So there is a tips to know th length of an array ?
man, we told you :)) you should write something like this: int num_of_elems = 5; Monom* m = malloc(num_of_elemnts*sizeof(Monom)); int sizeof_m = sizeof(Monom)*num_of_elems; and if you wanna get the array size everywhere you should just pass num_of_elems.
Diego
Also if it's a string then it's far better to use a terminating sequence For example, null terminated strings hold pointers char objects and when they encounter a nullptr they know that's the end of the string
Diego
At least that's how I understand it 😅
Ehsan
but it’s also a bad practice, allocating memory is costly. You should have minimum memory size alocated, then if it’s full you copy the elements and free it. Then make a new array with something like double the size. This is how you should implement dynamic arrays
Diego
man, we told you :)) you should write something like this: int num_of_elems = 5; Monom* m = malloc(num_of_elemnts*sizeof(Monom)); int sizeof_m = sizeof(Monom)*num_of_elems; and if you wanna get the array size everywhere you should just pass num_of_elems.
Alsoo it's worth mentioning that calling sizeof on a dyanmic array will yield the size of the first element, since a dynamic array is really just a pointer to the first element, hence why it's so problematic
Phil
typedef struct { int length; Monom *monoms; } or i can detect if if(polynome[c].m==0 && polynome[c].e==0) //end of array
Diego
And it's copying that's costly