Why Use Dynamic Memory Allocation?

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 9009 Views - Last Post: 10 April 2010 - 12:00 AM Rate Topic: -----

#16 Israel  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 7
  • View blog
  • Posts: 817
  • Joined: 22-November 04

Re: Why Use Dynamic Memory Allocation?

Posted 09 April 2010 - 01:43 AM

No doubt I'm beating a dead horse here.

I'm just going to take it that it performs better under certain circumstances.
Was This Post Helpful? 0
  • +
  • -

#17 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Why Use Dynamic Memory Allocation?

Posted 09 April 2010 - 01:50 AM

Try to run this in C++:
#include <iostream>

int main() {
    int elements;
    std::cin >> elements;
    int arr[elements];
    return 0;
}
It doesn't compile. You need dynamic memory allocation:
#include <iostream>

int main() {
    int elements;
    std::cin >> elements;
    int* arr = new int[elements];
    delete[] arr;
    return 0;
}

Was This Post Helpful? 1
  • +
  • -

#18 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5642
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Why Use Dynamic Memory Allocation?

Posted 09 April 2010 - 03:38 AM

View PostIsrael, on 09 April 2010 - 02:43 AM, said:

No doubt I'm beating a dead horse here.


Yes.

View PostIsrael, on 09 April 2010 - 02:43 AM, said:

I'm just going to take it that it performs better under certain circumstances.


No. Heap and stack are meaningless to the programmer. So is page size, minimum block size, and all the other minutia for which the OS is responsible. The reason for dynamic memory allocation is that it is required for anything non trivial.

Your confusion and arguments are understandable; you haven't programmed much. If you had, the answer would be obvious. If you actually get into programming, and do it for any length of time, you'll look back on this thread you've made and feel embarrassed at your ignorance. Most of the people responding know this and are trying to be gentle.

The horse is dead; leave it be. The less you add to this the better your future self will feel.
Was This Post Helpful? 1
  • +
  • -

#19 jbeme  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 93
  • Joined: 30-October 09

Re: Why Use Dynamic Memory Allocation?

Posted 09 April 2010 - 05:17 AM

Ok, maybe a dead horse but I think he might get this.

Example(dynamic):
I put in a dynamic string: "Here we go!"
The computer takes this string and saves it to code blocks 0-22, 36-54, 82-96, 3,024-3,152(it must make a pointer every time it skips)
I now have another dynamic string I enter: "And now we stop."
Now the computer may put this new string on blocks 23-202 overwritting your old string and changing it to "Hereow we stop."

Example(static):
You create an array and you tell the program that it will take 126 blocks of memory.
The program then sets asside 126 blocks of memory that are all in order.
These blocks will never be allowed to be overwritten and they allow the HDD to read them in order without having to jump around.
Was This Post Helpful? 1
  • +
  • -

#20 Israel  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 7
  • View blog
  • Posts: 817
  • Joined: 22-November 04

Re: Why Use Dynamic Memory Allocation?

Posted 10 April 2010 - 12:00 AM

Quote

No doubt I'm beating a dead horse here.

I'm just going to take it that it performs better under certain circumstances.


That was my attempt at saying thank you all, but I quit. I'll just keep coding.

Quote

If you actually get into programming, and do it for any length of time, you'll look back on this thread you've made and feel embarrassed at your ignorance. Most of the people responding know this and are trying to be gentle.


I thought I was into programming until that was said. I thought, and still believe, that because I had a sincere interest to actually understand what the code did, that was what separated me from a script kiddie. If my quest for knowledge was an annoyance I don't care. When I stop learning or asking questions then please put me in my grave.

I do not wish to discuss this topic anymore. I will keep playing with malloc and free until it reveals itself to me or continue research elsewhere.

This post has been edited by Israel: 10 April 2010 - 09:49 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2