Thread: Static, Stack, Heap Array and time

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    10

    Static, Stack, Heap Array and time

    hi..i need help with this one:

    write three functions in C/C++; one that declares a large
    array statically, one that declares the same large array on
    the stack, and one that creates the same large array from
    the heap. Call each of the subprograms a large number of
    times (at least 100,000) and output the time required by
    each.

    so i know that heapArray uses the word new..

    Code:
    void StaticArray()
    {
         int x[1];
    }
    
    
    void HeapArray()
    {
         int *z;
         z = new int[1];
    }
    is my static and heap function are correct?.
    and please help me for the stack array function.
    and how am i going to calculate da strt time and end
    time for each if i put a for loop that will call each function.?

    thanks.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    In the first function you have an array on the stack, not a static array.
    In the second function you have a memory leak. You must delete[] whatever is allocated with new[].

    It is foolhardy to time something that has no net effect and thus could be optimised away by the compiler. Surely your teacher is expecting these functions to actually do something.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    10
    Quote Originally Posted by iMalc View Post
    In the first function you have an array on the stack, not a static array.
    In the second function you have a memory leak. You must delete[] whatever is allocated with new[].

    It is foolhardy to time something that has no net effect and thus could be optimised away by the compiler. Surely your teacher is expecting these functions to actually do something.
    oh yeah ur right.
    the first function is really stack array.
    and to make it static, i should put "static"
    on the declaration, right?

    ex.. static int x[50]; ???

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, that is correct.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Partition and scheduling HW/SW help!
    By thangdc01_02 in forum C++ Programming
    Replies: 3
    Last Post: 11-18-2010, 02:07 PM
  2. Grouping Question
    By baikal_m in forum C Programming
    Replies: 7
    Last Post: 10-26-2010, 04:41 PM
  3. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 03:55 AM
  4. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM