Thread: Memory (Static vs Dynamic)

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    47

    Memory (Static vs Dynamic)

    So, I have a program where I have to work with multiple arrays and other variables which leads to ALOT of variable declerations and innitializations, re-innitializations etc.. (all seperate) but they all have so much in common, basically the same over and over again with different names and different sizes (i.e. arrays).

    So i wanted to clean things up, create a single structure with a set of variables grouped together (integers, arrays, whatever), dynamically allocate memory for it on runtime depending on what I need, create multiple instances of it if need be. But i'm curious if i'm generating tons of data if I should just be going the long route and doing the painstaking "setup" in my program as opposed to the generic less coded dynamic way. I don't notice much speed difference (believe me I am generating lots of data), but I believe in the future I could be generating 2, 3, 4 times as much and maybe than ill see a speed difference?

    So I guess what i'm asking is if I absolutely don't need to have dynamic memory allocation, should I be doing it? i guess i found it more appealing because of the time and effort it saves me....It runs smoothly as well, proper allocation and de-allocation, no memory leaks etc..And it's also not alot, an array and a few integers grouped together under a structure.

    But, a program running for 5 minutes where this snippet of code is run possibly hundreds of thousands of times can add up I guess.....i mean ive done it both ways and didnt notice it too much. Speed is really only an issue if its bogged down like crazy, so allocation and deallocation happens alot
    Last edited by tempster09; 12-11-2009 at 05:06 PM.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    It's best to write the cleanest code possible, and only optimize if you run into performance problems. If your code is clean, and you don't have any performance issues, then don't change anything
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    47
    Ya, that's what I figured. My program has so much code making it modular with functions and using structs and memory allocation to decrease code just simplifies things. Running the profiler too you don't really see noticeable performance differences. I mean people are always complaining and saying don't do it or that it can be costly to scan for memory but am I ever realistically exceeding the amount of memory allotted to my program when it's ran....probably not so searching for memory isn't done.

    I've been thinking of even going overboard and using a linked list to manage my memory and structures so I can easily clean up and not have to worry about memory....but that seems a bit much and just means more pointers and memory allocations...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structs, dynamic memory, and phone book entries
    By wkfcs in forum C Programming
    Replies: 5
    Last Post: 10-09-2009, 03:57 PM
  2. pointers and dynamic memory
    By tkansara in forum C Programming
    Replies: 1
    Last Post: 06-16-2009, 09:22 AM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  4. Inheritance and Dynamic Memory Program Problem
    By goron350 in forum C++ Programming
    Replies: 1
    Last Post: 07-02-2005, 02:38 PM
  5. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM