!!!Help!!!Basic data structure Q.

Suppose that p is an int* variable. Write several lines of code that will make p point to an array of 100 integers, place the numbers 0 through 99 into the array components, and return the array to the heap. Do not use malloc.


Appreciate your help.
Can someone write an example code for me ? Appreciate it!
Don't know if this is what the question asked but I googled for it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;

int main()
{

    int *p = new int[100];

    for (int i=0; i<100; i++)
    {
        p[i] = i;                     // 0 to 99 into array
        cout << p[i] << endl;         // check it is in array
    }

    delete [] p;

    return 0;
}

Last edited on
do you know what does this sentence mean, " return the array to the heap"? how to express this sentence in code?
Ryan Feng wrote:
how to express this sentence in code?
delete[] p;
No I didn't know what a heap was but after a google,
http://www.cplusplus.com/reference/algorithm/make_heap/
It isn't a term I have come across yet but I have found it in my How to Program C++ book toward the end of the book on the standard template library.

Some more stuff on heaps,

http://www.learncpp.com/cpp-tutorial/79-the-stack-and-the-heap/

http://stackoverflow.com/questions/10860290/is-it-better-to-use-heap-or-stack-variables
Last edited on
That's a different kind of heap. Look up heap with regards to memory, not algorithms.
http://www.cprogramming.com/tutorial/virtual_memory_and_heaps.html
The memory area where new gets its blocks of memory for allocation (usually called free store or heap) is illustrated in the following picture:
So I take it that new assigns data to the memory heap (spare memory) and delete returns that memory for other uses.
Last edited on
You might encounter terms allocate and deallocate too.

Dynamically allocate (reserve) a block of memory from free store (which may, or may not, be same as the heap used by malloc) in order to have space to store data in. Deallocate (release) the block when it is no longer needed. The block is allocated for your program from memory area that the OS manages. Similarly, other programs may simultaneously have memory allocated to them. The OS should make sure that one process cannot access the memory of an another process, even though all programs do consume the same physical resource.


The heap and stack are names of specific logical data structures that suite to specific algorithms. OS provides "stack memory" and "heap memory" to processes. You are free to guess what kind of data structures and algorithms the OS does use in order to provide memory for programs.


@Ryan Feng:
Please note that this is not a homework site. We won't do your homework for you. The purpose of homework is that you learn by doing. However we are always willing to help solve problems you encountered, correct mistakes you made in your code and answer your questions.

We didn't see your attempts to solve this problem yourself and so we cannot correct mistakes you didn't made and answer questions you didn't ask. To get help you should do something yourself and get real problems with something. If your problem is "I don't understand a thing", then you should go back to basics and study again.

Some code was already posted, but did it help you or was it a disservice for your chance to learn?
Last edited on
Topic archived. No new replies allowed.