Anonymous
if you need to listen terminate signal - do it
Anonymous
When any memory Is dynamically allocated then that memory exists from the start of the program till the end of the program and gets released once program ends. Then why is it advised to delete that memory manually? And also if I create an object dynamically, then at the time when program ends why the destructor doesn't run for this dynamically created object?
Because not all programs are meant to run and stop. There are programs that run continuously and in such programs memory leaks can manifest themselves into "Out of Memory" issues. And destructors are run only for objects that go out of scope and objects in the global scope (when the program ends). For objects that are allocated dynamically, only the pointer to it goes out of scope and there is nothing to be done when a pointer goes out of scope except for freeing up the memory which the pointer occupies which is in most cases just a stack pointer reset. So destructors are not called. If your dynamically allocated object had locked an IPC mutex, then this muted will remain locked.
Anonymous
So if I know that I need an object till the end of program, I don't need to write delete statment?
Well technically yes. If you need an object till the end it wouldnt make sense to delete it earlier now would it?
Anonymous
So if I know that I need an object till the end of program, I don't need to write delete statment?
But if that object is acquiring some resources that need to be freed at the end of the program then you must call delete on it to run its destructor.
Anonymous
Those resources won't be deleted automatically when program ends??
Not all resources. I gave you an example of an IPC mutex. There are other things like file locks and stuff which are not cleared up automatically by the OS. So you have to manually close them.
Anshul
What's an IPC mutex
Anonymous
What's an IPC mutex
A mutex used across processes.
Anshul
I still can't understand it
Anonymous
I still can't understand it
Do you know what a mutex is?
Anshul
No
Anonymous
No
Well then read up about it.
Anshul
Okay
Anonymous
Hi I have an assingment to do a running simulation by generating random numbers for an action like walk run etc with a timer. the timer will also determine how long the person will walk run etc depending on the elapsed time
Anonymous
I trieed int elapsed_time = (hour*60*60) +(min*60)+sec; but i dont get the results i want so is it possible to get the elapse time so i can use it for
Anonymous
I trieed int elapsed_time = (hour*60*60) +(min*60)+sec; but i dont get the results i want so is it possible to get the elapse time so i can use it for
Your question is not clear. If you show us a small code snippet of what it is that you are trying to do, maybe someone can help.
kartik
Hi all new to programming any help for prerequisite tools for c programming & how to configure it on windows10
Anonymous
Hi all new to programming any help for prerequisite tools for c programming & how to configure it on windows10
If you are new to C, start with Visual Studio Community Edition if you have the bandwidth for gigabytes of download. If not, download mingw package installer from msys2 website (this apparently offers the latest version of the compiler tools) and download an IDE like CodeBlocks or CLion (if you are a student) and configure it with the path to your mingw installation. Use one of these books : 1. Modern C by Jen Gustedt 2. Modern C by KN King 3. The C programming Language by Kernighan and Ritchie and fire away.
Crow
I recommend starting only with a text editor and compiling with the terminal
cafejoy
ok thanks
mito
int main() { int arr[5] = {1,2,3,4,5}; for (int i=0; i!=5; i++) { std:: cout<<arr[i]; } return 0; } Is there any difference if I replace i!=5 with i<5 in the for loop ? Not asking in terms of output.
Anonymous
I saw a solution, where it had vector<string> v(n); but he was doing this: mymap[v[j][i]]++; Now, I wanna know how come a 1D vector access [j][i]
kartik
If anyone help For vscode installation it's helpful because organization is use it. For C specific
Anonymous
I saw a solution, where it had vector<string> v(n); but he was doing this: mymap[v[j][i]]++; Now, I wanna know how come a 1D vector access [j][i]
The first index is on the vector which returns a string and the second index is on the string returned. So v[j] gets the string at index j and v[j][i] gets the i'th character from the string at index j in the vector.
Anonymous
i'm a bit confused :/
It is the equivalent of this code. string s = vec[j]; char c = s[i]; This is equivalent to char c = vec[j][i];
Anonymous
It is the equivalent of this code. string s = vec[j]; char c = s[i]; This is equivalent to char c = vec[j][i];
so originally, the vector contained strings right suppose it had: { "abcd" , "efgh", "ijkl" }; now, let's say, j = 1 then, s = efgh and let's say, i= 0 then, c = e Am I correct?
Anonymous
Yes
So we can use that for any "container" - the v[j][i] style?
Anonymous
So we can use that for any "container" - the v[j][i] style?
If the container elements support indexing then yes. For ex: vector<vector<int>> vec = ....; You can do vec[i][j] to acces the j'th element in the i'th vector in vec.
Anonymous
also why does it work like this - when it ain't a 2D one?
Anonymous
yes ik that but this was an 1D vector
The first index applies to the vector. The second index applies to the element in the vector. There is nothing like a 2D vector. A vector is a sequence of elements. That is it. Now whether you can apply a second index depends on the type of the elements in the vector. If the element is a string, a deque or a vector or a std::array or any class that overloads operator[], then you can use a 2nd index. This can be extended further.
Robert
when we look at very low level ?
It depends on how compiler interpret these two operators into assembly instructions which they are associated to.In addition,the category of instruction set could be another factor make the difference.
Robert
Sorry ,I am unable to give you specific answer .If you are interested in it ,something about compiler is recommended.
Anonymous
Basically no difference here. Personally, I prefer i!=5. It implies i=5 when the loop ends. While another implies i>=5.
C++ developers generally prefer != over < because it is what is used with iterators as well. Not all iterators support < but all iterators support !=.
Diego
int main() { int arr[5] = {1,2,3,4,5}; for (int i=0; i!=5; i++) { std:: cout<<arr[i]; } return 0; } Is there any difference if I replace i!=5 with i<5 in the for loop ? Not asking in terms of output.
I often use < rather than != because I get slightly baselessly worried that is it were to somehow miss that stage it'd run forever, never to be stopped again with !=
MRT
Can someone give me the algorithm for crop image with scale and recangle?
MRT
rectangle*
MRT
i have two property, scale and rectangle area and i want to create script to crop image,i need example :D
kartik
https://code.visualstudio.com/docs/languages/cpp This?
& how to setup vscode for user input , terminal output ,logs
cafejoy
& how to setup vscode for user input , terminal output ,logs
have you tried youtube? there are tons of videos there.
Anonymous
If someone needs help in c ask me dm
klimi
If someone needs help in c ask me dm
no, we don't do that here
Sommy
Links to getting live C++ projects with documentation ??
Shvmtz
Is there any C standard that provides the ability to size an array with a variable. Like this: #include <stdio.h> const int LENGTH = 5; int main() { int array[LENGTH] = { 10, 20, 30, 40, 50 }; }
Anonymous
Is there any C standard that provides the ability to size an array with a variable. Like this: #include <stdio.h> const int LENGTH = 5; int main() { int array[LENGTH] = { 10, 20, 30, 40, 50 }; }
Can you elaborate? Like, you can initialise the size with any variable (that has a value inputted or smth), and that's not a "global" variable as you declared
Anonymous
Edited Code
...yes and what do you wanna know?
Anonymous
Also you wrote int twice
Shvmtz
...yes and what do you wanna know?
Any c standard that allows this
Anonymous
Any c standard that allows this
Allows what? Global variables?
Shvmtz
Allows what? Global variables?
Allows variable sized array like in code.
Anonymous
Btw that itself is a C code snippet Use #define
...
Any c standard that allows this
should work with c++ and constexpr, in c i've seen people use the preprocessor for this a lot
...
youre probably looking for malloc/free though
Anonymous
#define LENGTH 5
Anonymous
Or you can declare a variable outside main
...
Yea. That way the code works fine in C
do you want to determine the size at run-time?
Anonymous
For global access
Shvmtz
But in case of C, do we have any C standard that allows the same using const int LENGTH instead of #define LENGTH
Shvmtz
do you want to determine the size at run-time?
I saw a video related to fixed and variable length arrays in youtube. I'm surprised why didn't his code produced error.
Shvmtz
You mean dynamic allocation?
Wait. I'm sharing his code.