数学の恋人
It instructs the compiler to place the struct at address multiple of MAX. In your case, it's multiple of 1024 * 64. I don't know why do you need that, it's even more than just a widely used page size, which is often only 4096. This seems to over-align for me. Not sure what's your need. As far as I can tell, there are several reasons to use this attribute: 1) You need aligned move for vector operations, like (x86-64 specific): - {v,}movdqa w.r.t. xmm register (need 16-byte aligned). - vmovdqa w.r.t. ymm register (need 32-byte aligned). - vmovdqa64 w.r.t. zmm register (need 64-byte aligned). [ There are more, but at least these cover the generic case. ] If the source or destination memory address is not multiple of the specified above, your program will get #GP exception, so for userspace will just be segmentation fault. 2) You need to create your own allocator and for some reason you need to guarantee that the user gets the minimum alignment for the specific case. Like implementing malloc(). For example: https://lore.kernel.org/io-uring/20211011064927.444704-1-ammar.faizi@students.amikom.ac.id/T/ 3) You want to align your struct to avoid false sharing in SMP system (performance wise). In the Linux kernel, we have a macro that yields something like that, like ____cacheline_aligned and ____cacheline_aligned_in_smp. Link: https://github.com/torvalds/linux/blob/5bfc75d92efd494db37f5c4c173d3639d4772966/include/linux/cache.h#L40-L50 4) Add yourself... there are a lot of reasons...
Thank you very much that was very informative.
Anton
Case B
Thanks. Although it is not clear what you meant by that.
Puspam
Anonymous
It instructs the compiler to place the struct at address multiple of MAX. In your case, it's multiple of 1024 * 64. I don't know why do you need that, it's even more than just a widely used page size, which is often only 4096. This seems to over-align for me. Not sure what's your need. As far as I can tell, there are several reasons to use this attribute: 1) You need aligned move for vector operations, like (x86-64 specific): - {v,}movdqa w.r.t. xmm register (need 16-byte aligned). - vmovdqa w.r.t. ymm register (need 32-byte aligned). - vmovdqa64 w.r.t. zmm register (need 64-byte aligned). [ There are more, but at least these cover the generic case. ] If the source or destination memory address is not multiple of the specified above, your program will get #GP exception, so for userspace will just be segmentation fault. 2) You need to create your own allocator and for some reason you need to guarantee that the user gets the minimum alignment for the specific case. Like implementing malloc(). For example: https://lore.kernel.org/io-uring/20211011064927.444704-1-ammar.faizi@students.amikom.ac.id/T/ 3) You want to align your struct to avoid false sharing in SMP system (performance wise). In the Linux kernel, we have a macro that yields something like that, like ____cacheline_aligned and ____cacheline_aligned_in_smp. Link: https://github.com/torvalds/linux/blob/5bfc75d92efd494db37f5c4c173d3639d4772966/include/linux/cache.h#L40-L50 4) Add yourself... there are a lot of reasons...
Just want to add that false sharing can happen even with your data being cache aligned. You need to do more to prevent false sharing apart from just aligning the data with L1 cache
Anonymous
Hi everyone! I declare following arrays in two different programms. A) static arr[x] B) static arr = malloc(x) Few question regarding memory management. 1) Arrays A and B both have static storage duration? 2) In which case it considers a leak if I don't free the array at programm exit?
I assume you meant a pointer in the second case and that it is a definition in global or namespace scope and you don't have thread_local access specifier as well As far as the equivalence of both the declarations are considered, they both don't lead to a memory leak. An array declared with static storage duration would last till the end of the program. Likewise by declaring a dynamically allocated array to be of static storage duration, you presumably want it to last for the entire lifetime of your program. So the pointer in your second case will be freed at the end of your program following which the memory will automatically be reclaimed by the OS. So there is no memory leak technically. A memory leak by definition is something where you lose access to your pointer to the memory before you can deallocate the memory. So this doesn't happen in this case.
Anonymous
How is this a C/C++ question?
Anonymous
Does anyone know how to simulate multithreading and concurrency using either c or c++?
C++ offers concurrency primitives that can do more than just simulating concurrency. They can actually enforce it. C has minimal concurrency support in the standard but there are tonnes of options outside the standard to support concurrency.
shriman_deepak
What does this substr do ?
Anonymous
Thanks for elaborating on that! Yes, my mistake, I meant a pointer in the second case. static arr = (char **)malloc(sizeof(char *) * (x + 1)) (do I refer to it as "pointer to static string array"?) is declared in a function, so it's not global. Just to be clear: do I still have to free everything that is stored in char array and array itself on last function call?
How will you know if some call is the last function call? Things with static storage duration are typically not allocated on the heap. If they are, the you don't need to free them explicitly unless you know for sure that it won't be needed anymore. Think about the case where you free the memory prematurely and then somone calls your function again in which case arr would either be an invalid pointer or a null pointer if you explicitly set it to 0 after freeing it. So then you have to handle this case as well.
Anonymous
If anyone knows java or c++ or python to the advanced level and can develop an app using that ,please dm me ,i have a great app startup idea,i just need a CTO or a tech cofounder to implement it...so if anyone interested plss dm me
Juli
Guys i need your help
Juli
#include <iostream> using namespace std; int main(void) { const int N = 10; int arrayOfIntegers[N]; std::cout << "You will be prompt to enter 10 integer numbers " << endl; int counter = 0; while ( counter < N ) { // while loop begins std::cout << "Please enter an integer number " << endl; cin >> arrayOfIntegers[counter]; counter++; }// while loop ends int min = arrayOfIntegers[0]; /// compute min of data (begin) for (int i=0; i < N; i++) { // for loop begins if( arrayOfIntegers[i] < min ) min = arrayOfIntegers[i]; } // for loop ends /// compute min of data (end) std::cout << "The minimum of the values you have entered is: " << min << endl; return 0; } // end of main
Juli
i need tocompile it, run it, and change it so that it becomes a program that finds the maximum value (instead of the minimum vale) in an array of integers.
Juli
How can i do this
\Device\NUL
Just reverse it
Juli
I need to remove min
Juli
And write max
\Device\NUL
Yea just reverse the flow
Juli
I dont understand
Khaerul
well
Juli
I need to do this int max = arrayOfIntegers[0]; for (int i=0; i < N; i++) { if( arrayOfIntegers[i] > max ) max = arrayOfIntegers[i]; }
\Device\NUL
< min = bla-bla
Juli
Can you speak seriously
Juli
I asked for help
\Device\NUL
Can you speak seriously
Yes, just reverse it
\Device\NUL
I've already give the clue
Juli
Ok
Juli
Thank you, you are very clear
Dmitriy
Sadgwick on coursera
Ali Emre AYGÜN
using namespace std; class Book{ int PageNum; public: Book(int num=0):PageNum(num){} friend void display(Book &); friend class Author; }; Hi What does the "Book(int num=0):PageNum(){} do ? I understand that we use constructor there but couldn't understand after we use ":" why do we use it ?
Ali Emre AYGÜN
Oooh I've been trying to understand this for 2+ hours. Thank you so much
Laopigo
How to return the value of variable (not variable itself) from macro?
Anonymous
What should i learn in 2021 python or c++??
Anonymous
Both
What should be favoured the most...it's a one choice question
SLC
#include<bits/stdc++.h> using namespace std; int main(){ int i, j, k, row; cout<<"Enter row: "; cin>>row; for(i=1; i<=row; i++){ k = i; for(j=1; j<=row; j++){ cout<<k--; } /*k = i; for(; j<=row; j++){ cout<<k--; }*/ cout<<endl; } }
SLC
please explain the code
SLC
ok i got it
SLC
thanks
Nameful
ok i got it
good job 👍
SLC
good job 👍
thank you
Pavel
What should i learn in 2021 python or c++??
Depends on what you want to work with in future (maybe neither of them). Also chances are, if you learn C++, you will learn a bit of python eventually anyway (while writing scripts for automating stuff).
Ali Emre AYGÜN
Does anyone know a good source to understand the concept of operators? For example this code here: ostream & operator<<(ostream &myout, Point p); I can't quite understand why and when do we use them
Egro
I think, those are related to header, like “Ostream “ then the normal pointer is the copying of an address
Egro
In the header, Ostream is define and then it’s called in another file.
Egro
If I’m wrong correct me
Ali Emre AYGÜN
You don't know in this example or in general?
In general, i see in my examples that they're mainly used in classes but i just don't understand why
Captain
In general, i see in my examples that they're mainly used in classes but i just don't understand why
Well in classes operators needs to be overloaded to use it with the class object. Like + operator, when you declare 3 objects o1, o2, o3and dont have any overloaded operator compiler won't understand what to do with o3 = o1+o2. So you just have to overload the operator. Like Myclass operator+(Myclass obj){ //Body of code }
Captain
The method just work with o1+o2 like o1.operator+(o2)
Ali Emre AYGÜN
Got it. Thank you so much for the explanation! I will try to do it myself now
Anonymous
Does anyone here know how to use MySQL?
Pavel
Does anyone here know how to use MySQL?
1) are you sure it's related to C/C++? 2) It's not a good way of asking (if you want to get more useful answers), details: https://bit.ly/3jNHlra
Pavel
Well, except if you asked what you wanted to ask, then the answer is "yes".
Eyvaz
how to use pointer of vector?
Eyvaz
for example the problem is sorting
Pavel
how to use pointer of vector?
A pointer to vector (std::vector<T>*) or a vector of pointers (std::vector<T*>)?
Eyvaz
first one maybe
Eyvaz
Eyvaz
i just want to write a function that sort the vector
Eyvaz
without returning
Pavel
first one maybe
Ok, then if you need to sort it you can just dereference it, it works with any pointers. std::sort(yourVec->begin(), yourVec->end()); Note that I use -> instead of ., which dereferences the pointer (analogue to writing (*myvec).begin() as an example). Not sure why you need it to be a pointer though, are you sure it's not the second case?
Eyvaz
void sort()
Pavel
thank you, but i have to use my own algorithm
Then also use it as a normal vector, just dereference e.g.(*yourVec)[0] to take item by index zero
Pavel
thank you, but i have to use my own algorithm
Or even better, you can once check it for not being nullptr and then create a reference variable to use instead without manual dereferencing. std::vector<T>& myVecRef = *myVec; ... myVecRef[0]...
Eyvaz
is this true swap algorithm with pointers? swap((*vec)[i], (*vec)[i+1])
Eyvaz
okay thank you so much, I'll try it
Pavel
okay thank you so much, I'll try it
But really, better to convert it to a reference as in the example above or even make your function to accept a reference instead of a pointer (if it can't be nullptr)