Dynamic memory problem | SoloLearn: Learn to code for FREE!

+2

Dynamic memory problem

I just try to run two code one is simple that takes size of array during runtime And other takes size of array in run time using pointer, dynamic memory and new operator But both are working fine then why and when I have to use dynamic memory , new operator ? https://code.sololearn.com/cmXd4OeUejZj/?ref=app

9/8/2020 9:33:53 AM

Saad Mughal

12 Answers

New Answer

+3

In standard C++, variable length array is not allowed to be implemented in your first code. That is: int n = 10; int arr[n]; //Compile error. Then why the code is compiled you may ask. Because this is a g++ extension. It's only compileable when you compile with g++, and that's the compiler SoloLearn is using. So yes, you can do that in SoloLearn. But in other compiler it won't work. btw, in C variable length array is allowed. Whereas it is removed in C++.

+6

CarrieForle Not removed from C++. It was never a part of C++

+6

Saad Mughal There are no "memory leaks" because VLA are allocated on function stack. If for whatever reasons an exception is thrown then all stack elements are destroyed For your second question char arr[2000]; will not cause memory leak if an exception is thrown as the array is allocated on function stack where as char* buffer = new char[2000] can cause memory leak if the exception is thrown before delete [] buffer is executed.

+5

Saad Mughal You can always use it provided you are using it within the function only. Since the array is allocated on stack, it will be a problem if you return a pointer to this array to a location from where the current function was called. void processName() { char* name = getName(); // (1) } char* getName() { char name[100]; cin >> name; return name; // (2) } (2) will cause issue when the pointer is returned at (1) because (2) is returning a pointer to a local array which gets destroyed once the function returns. The pointer at (1) will be pointing to garbage value. To avoid that name array should be allocated on heap so that once the function returns at (1) the pointer is valid. At (1) after you are done using the pointer' you must delete it explicitly using delete[] name else there will be memory leak. Did someone told you not to use it?

+4

The second example is more compatible than the first, the first uses a VLA (https://en.m.wikipedia.org/wiki/Variable-length_array), as I understand it, VLA is not widely implemented and may not be such a good idea (it can exhaust the stack when allocation is big). Don't forget to `delete [] ptr;` for the second example, allocated memory must be released again once we no longer use it.

+4

Saad Mughal You are Welcome 🙂🙏👍

+3

CarrieForle Ipang ~ swim ~ char arr[2000]; This array can cause memory issue in large programs , memory leaks and don't remove data from heap AND char* buffer = new char[2000] delete [] buffer; This prevent program from memory leaks and delete data from the heaps Is what I write above is true?

+3

~ swim ~ so why we didn't simply use char arr[2000]; as it doesn't cause memory leaks? why we dynamically allocate data instead of automatically? I just started learning this today I am confused

+3

~ swim ~ 😄 Thanks I am a solo learner watch youtube videos to learn no one to guide except this platform and you guys

+2

Ipang OK bro thanks I am watching tutorials on dynamic memory but not study how to implement delete yet

+1

CarrieForle OK thanks bro i understand it now . I just watching tutorials and practice it in solo learn

+1

CarrieForle Ipang so in the first example if my code not runs successfully what we call this problem Did we called it "memory leaks"?