Anshul
When I hit enter on my ".exe" file, then only both statically and dynamically created variables get memory. So how's that different
Suka
isn't it about linking, not memory allocation?
afaik in static, compiler will reserve the memory. but perhaps i am wrong hehe.
Suka
When I hit enter on my ".exe" file, then only both statically and dynamically created variables get memory. So how's that different
idk, but here some article about it. https://www.geeksforgeeks.org/difference-between-static-and-dynamic-memory-allocation-in-c/
Pavel
isn't it about linking, not memory allocation?
That's one of the ambiguous naming things in C++, Word static depending on context can refer to linking or to storage
Pavel
When I hit enter on my ".exe" file, then only both statically and dynamically created variables get memory. So how's that different
Only statically allocated memory is allocated by default when you run the application. Dynamically allocated memory is allocated at runtime (e.g. by calling new or malloc), note that STL library classes or some other classes can do dynamic memory allocations under the hood, so it can be that you don't see explicit allocations in your code but they still happen because you use e.g. std::cout
Suka
What do you mean by "hit enter on exe"?
ig he need to learn more about exe layout like .data .text n etc cmiiw
Anonymous
When I hit enter on my ".exe" file, then only both statically and dynamically created variables get memory. So how's that different
Your executable has a size in bytes. What do you think that size represents? Do you think all of that is code?
Anonymous
When I hit enter on my ".exe" file, then only both statically and dynamically created variables get memory. So how's that different
Try compiling these 2 codes separately (without optimizations) and see what is the size of the executable in each case. Now you will figure out what static memory allocation is #include <iostream> int main(){ std::cout << "Hello World"; } #include <iostream> int arr[10000] = {1}; int main(){ std::cout << "Hello World"; }
Suka
Compiler will delete your array
afaik only if he enable optimize flag other than O0. or using clang hehe or using volatile flags #include <iostream> volatile int arr[10000] = {1}; int main(){ std::cout << "Hello World" }
Anonymous
Compiler will delete your array
Turn off optimizations. The point of that exercise was to understand that not all memory is allocated at runtime. Some memory is reserved at load time itself even before the first line of your code has started executing
Anshul
What do you mean by "hit enter on exe"?
I meant when I run the executable of my code
Anshul
Why is the size different for them? Entire array is allocated at compile time?
Anonymous
Why is the size different for them? Entire array is allocated at compile time?
Memory is not allocated at compile time. It is allocated at load time. Your executable has different sections in it one of which is the .data segment which stores all initialized data. This section will map to some specific address in your process's virtual memory space and will have physical pages allocated for it. All of this happens when your executable is loaded into memory. Only after this is done and the runtime setup is done will your program start executing the first statement in main function
Anshul
Wherever I looked for sma and DMA, they say static or compile time memory allocation doesn't mean the memory is allocated at compile time, it just means that memory requirement is known at compile time.
Anshul
Is this what you mean?
Anonymous
So on the hard disk it is stored that these are the variables in code. And when I run the program it loads onto ram and then these variables gets memory. But in case of dynamic allocation no variables are stored in hard disk they are directly alloted memory on ram?
Static memory allocation refers to memory that is allocated after you click your executable file and before your main function starts executing. These variables all have a fixed initialization value determined during compilation process itself. Runtime memory allocation refers to all allocations done after main starts executing.
Anonymous
Ok then why sma is faster than dma
Who said it is faster? Depending on how much memory you allocate, it can slow the loading time. Or in OSes where pages are mapped in only when they are accessed, it may slow your execution time as well.
Anshul
Who said it is faster? Depending on how much memory you allocate, it can slow the loading time. Or in OSes where pages are mapped in only when they are accessed, it may slow your execution time as well.
I read it at many places. Even on gfg. Everyone says use sma for your online judge like if the constraints are 10^6 then create an array of the biggest size i.e. a[10e6]
Anshul
And if it's not faster than is it just used because compiler takes care of allocation and deallocation of statically created variables
Anonymous
The only reason why static memory allocation may be slightly more faster is because they remain active through out your program's runtime. So the OS doesn't need additional structures like it uses for freeatore management to support allocations and deletions. With static memory allocation, the allocation is fixed and happens once.
Pavel
Static memory allocation refers to memory that is allocated after you click your executable file and before your main function starts executing. These variables all have a fixed initialization value determined during compilation process itself. Runtime memory allocation refers to all allocations done after main starts executing.
I'm not sure that start of main is a good point to think about, I would say "any of your code" instead. I think it still possible to execute some code before main starts using static objects initialization tricks, and then do some dynamic allocation in that code
Anonymous
I read it at many places. Even on gfg. Everyone says use sma for your online judge like if the constraints are 10^6 then create an array of the biggest size i.e. a[10e6]
In competitive programming, only the runtime is calculated i.e. the time at which your main starts executing to the time at which your main function ends. So if you do memory allocations during this time, they will add to your runtime. If you use static allocations they will not add to your runtime (which is not actually true) but will happen for most of the common cases where your allocation can fit within a page of memory
Anshul
What is "page of memory"
Anonymous
What is "page of memory"
Read about OSes and virtual memory
Anshul
Can you tell me where I can read more about sma and dma
Anshul
I have not studied anything about OS yet.
Anonymous
From where?
Google it
Anshul
Google it
I mean suggest me some structured place to study about os. There are many resources on google but which one to choose
Pavel
I can not understand the last 2-3 lines. And also which one should we use preferably sma or dma?
If you can use sma, I would prefer that. E.g. if you know that your array will always have a specific size (or you know for sure max possible size), then it would be better to statically allocate it. If you don't know or not sure, then it depends: sometimes the cost of dynamically allocating some stuff once or twice is not that big compared to increasing of complexity of handling all possible sizes or extra memory cost of unused memory.
Anshul
And also I read this somewhere. " Int a[100]; Now I can't do this a=(some other address) As a is coming from symbol table But if I do this Int *a = new int[ 100 ]; Now I can make "a" point to some other memory location, as it's not coming from symbol table" This is what I saw in a tutorial. And I couldn't understand it. Can you help me what did it mean?
Anonymous
And also I read this somewhere. " Int a[100]; Now I can't do this a=(some other address) As a is coming from symbol table But if I do this Int *a = new int[ 100 ]; Now I can make "a" point to some other memory location, as it's not coming from symbol table" This is what I saw in a tutorial. And I couldn't understand it. Can you help me what did it mean?
int a[100] Here a is an array and it's type is fixed i.e. it is of type int[100]. C/C++ don't allow you to assign to arrays. int* a = new int[100] Here a is just a pointer and you can allocate a different value to it just like you can do with pointers that don't point to dynamically allocated arrays.
Pavel
Again can you show me how you will do this?
Don't hurry me, I'm preparing an example :)
Anonymous
In 1st can I say a is const pointer
It is not a pointer. That is why I specifically mentioned that it's type is int[100].
Anonymous
Don't hurry me, I'm preparing an example :)
Ah alright. I thought you didn't see the message
Anshul
But you once said arrays decay to pointers
Anonymous
But you once said arrays decay to pointers
Yes. They decay to pointers doesn't mean the array name is a pointer. It is converted to a pointer which is different from the array name
Anshul
*For statically created array* Does the value inside " a" is decided at compile time?? (The starting address of array)
Pavel
Ah alright. I thought you didn't see the message
Something like this https://wandbox.org/permlink/dlx3OPWmBvgiZmxF Note that code that initializes c and d will be executed before main this hack is described in Alexandrescu's Modern C++ Design, but I would not suggest to use it for anything since it's very easy to hit static initialization order fiasco with code like this (e.g. if b, c and d were declared in different *.cpp files then this code would be UB)
Anonymous
Something like this https://wandbox.org/permlink/dlx3OPWmBvgiZmxF Note that code that initializes c and d will be executed before main this hack is described in Alexandrescu's Modern C++ Design, but I would not suggest to use it for anything since it's very easy to hit static initialization order fiasco with code like this (e.g. if b, c and d were declared in different *.cpp files then this code would be UB)
But this is not static initialization. This is dynamic initialization. You are confusing initialization of a static with static memory allocation and initialization. Two different concepts. What I had said was that static memory allocation and initialization that is determined at linking stage itself will all happen before your code starts executing.
Anonymous
But I see the point you are making. That runtime allocation can also happen before main executes and I agree. Perhaps I should have rephrased that and said _ _startup or _ _crt0 instead of main
systemQuery
Is learncpp.com is good website to learn c++
Tg
Hi am c/c++ and php programming Lerner.
Tg
guys l will discous to about c/c++
Tg
guys am not profisstional c/c++ programmer but I do soon
Mar!o
So what's your question?
Anonymous
Is learncpp.com is good website to learn c++
Yes it is a decent website and much better than all the other ones like tutorialspoint, geeksforgeeks and stuff. I would recommend a book like C++ Primer by Barbara Moo however.
Tg
My question. Can you tell me about auto Storage. Class in c
Mar!o
Google it it's about scope bounded local variables. The auto keyword in C is implicit in C++ it's got a different use
Tg
tankyou
Tg
tell me about strstr(); string function in c
Anonymous
tell me about strstr(); string function in c
Use Google. We are not here to spoonfeed you
Anonymous
Okay