C++ help

Pages: 12
May 11, 2011 at 4:25pm
Hey guys, this is an assignment, i don't want it all done for me i just want some help to get on the right track. the one thing im struggling with is how to properly use the v_heap.h in mem_heap.h and mem_heap.cpp. If anyone could give me an example of some sort or just set me on the right track i would greatly appreciate it! I'll post the information below (i am new and have missed a few lectures and labs due to personal reasons but once again any help would be greatly appreciated!):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
Virtual Heap Class


You are to implement a C++ class that functions as a memory heap.


The virtual class v_heap.h has been provided, and must be implemented by your child class. The child class will be named MemHeap and it will be written in the files mem_heap.h and mem_heap.cpp. You must also write a program in main.cpp that thoroughly tests your heap implementation. Use print outs and assert statements accordingly.


In case you are wondering what the point of this is …..........................


This technique has been used by programmers in the past to avoid the overhead of making a call to the operating system to allocate and de-allocate memory. By allocating all the memory upfront, there is only 1 system call to allocate memory, and 1 system call to de-allocate memory, the rest is just pointer manipulation.


Implementing this technique also gives you a small insight into how memory management is handled “under the hood”.


The v_heap.h virtual class has been provided, its content follows.


#ifndef VHEAP_H

#define VHEAP_H


#include <cstdlib>


#define HEAP_SIZE 2048


class VHeap {

protected:

char * vheap;



public:

VHeap()

{

vheap = new char[HEAP_SIZE];

}



virtual void* vnew(size_t size) {}



virtual void* vcnew(size_t size) {}



virtual void vdelete(void* mem) {}



virtual size_t vsizeof(void* mem) {}



virtual void printHeapState() {}



virtual ~VHeap()

{

delete [] vheap;

}

};


#endif // VHEAP_H


Parts of v_heap.h explained


char * vheap;

Pointer to the memory allocated for the heap. You should not use this pointer directly, but rather assign another pointer to the address vheap holds, that way you wont inadvertently lose track of the start address of the heap.

Heap()

{

vheap = new char[HEAP_SIZE];

}

A constructor where the total amount of memory required for the heap is allocated.




virtual void* vnew(size_t size) {}

A user (main.cpp) can use this function to request memory (to be allocated within the heap).

The vnew function will allocate memory to the length of the parameter size, and it will return the address of the start of the allocated memory. The return type is void so as to handle any datatype. As you need to supply the size in bytes, you may find the function sizeof useful.


To use vnew to allocate a int, in main.cpp you would write

int *myNewInt = (int*)heapObj.vnew(sizeof(int));


To use vnew to allocate an array of long's that holds 5 items, in main.cpp you would write

long * myLongArr = (long*)heapObj.vnew(sizeof(long) * 5);




virtual void* vcnew(size_t size) {}

Functions the same as vnew but it also sets the allocated data to all 0's, where as vnew will leave whatever 'Junk' is in the allocated space.




virtual void vdelete(void* mem) {}

A release method where the user can return previously allocated memory back to the heap. When memory is released it should ensure that adjacent, available memory segments are consolidated into a single available segment.






virtual size_t vsizeof(void* mem) {}

Returns the amount of memory, in bytes, allocated at mem.






virtual void printHeapState() {}

A method which describes the in-use / available segments of the heap.

The following is an example of the output from printHeapState() where 'I' indicates areas being used for heap information, 'D' is allocated memory used to hold data and 'F' is for free/unused bytes.

IIIIIIIIIIIIIIIIIIIIIIIIDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDIIIIIIIIIIIIIIII

IIIIIIIIDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD

DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDIIIIIIIIIIIIIIIIIIIIIIIIFFFFFFFF

FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF


virtual ~VHeap()

{

delete [] vheap;

}



A destructor to release the memory associated with the heap back to the system.


You will also be marked on correct style, ie consistent indenting, appropriate variable naming etc.


Every Function Must have commenting in the style of


/**

* This function is only an example

* and does not actually do anything

*

* It returns nothing

* It takes no parameters

*/

void myFunction()

{

}


Single line commenting should be used for class variables and for non trivial code within functions.


The program must also be accompanied by a report (report.pdf) on how it works, diagrams may be useful for this. Your report need only be a page long (3 pages max) and it must describe the technique you used to implement the virtual heap. It is not meant to be comprehensive documentation, but rather a proof that you understand the problem and its solution.

Note :


You MUST NOT use new , delete , malloc , calloc, or free in your program.



You are to use only the memory that has been allocated by v_heap.h ( char * vheap )

No STL containers are allowed. No static structs or objects allowed.


Tip :


You may find it useful to define a struct and use a struct pointer to index into the heap!
May 11, 2011 at 4:40pm

...You MUST NOT use new , delete , malloc , calloc, or free in your program.

I am also interested in this as how would one allocate memory on the heap without any of the calls mentioned? WTF?

edit:
Maybie they mean not outside vheap class?
I am gonna try this in my compiler and see if i can help you.
Last edited on May 11, 2011 at 4:44pm
May 11, 2011 at 4:47pm
Yea not outside of vheap.h it already uses new and delete in it to allocate and deallocate the memory

VHeap()

{

vheap = new char[HEAP_SIZE];

}

and in the deconstructor uses delete!

any help would be greatly appreciated!
May 11, 2011 at 4:53pm
@savavampir: You just don't allocate memory. Period. The challenge is clear: You have HEAP_SIZE bytes available for EVERYTHING, including both the private data and the buffers you hand out.

What is not clear is how the functions should inform whenever the heap is out of memory. Throw std::bad_alloc?

Anyway, @OP: This type of assignment is not for a beginner. Your question is beginner-like. Something doesn't fit here. You either missed a big fat lot of lectures or something else is going on.
May 11, 2011 at 5:01pm
I have only missed a few but there was one vital one that i missed that did something similar with virtual classes like vheap being used in memheap.h and memheap.cpp .

Been trying to get hold of my tutor for some advice just having some bad luck (holidays and various others!) i have been able to do the tutorials up to one thats similar to this.
May 11, 2011 at 5:07pm
Ok, so let me ask: Do you know how to create a derived class? If yes, then start there: Create a new class that inherits (or derives) from VHeap.

After that, do you know how to override virtual methods? If yes, then do that.

Do you know how to write constructors? If yes, write a constructor for your new class that conforms to the assignment's specification (one that allows the user of the heap to specify the heap size).

Do you know how to declare structs? Declare one that helps you write vdelete(), vsizeof() and printHeapState().

And so on. If you get stuck at any particular point, then post a specific question.
May 11, 2011 at 5:20pm
Thanks webJose that actually helped a bit, when i'm done with the derived class mind if i post it for a check?
May 11, 2011 at 5:23pm
You sure can. Just note that I won't be handing out answers; most likely just explanations. Dunno about other members here.
May 11, 2011 at 5:42pm
I'am just interested in this task as i am beginner too.
I am stuck at this function implementation:
virtual void vdelete(void* mem) {}

A release method where the user can return previously allocated memory back to the heap.
When memory is released it should ensure that adjacent, available memory segments are
consolidated into a single available segment.

What to do here? How to approach this?
1. Does this mean that i need to delete/(zero(fied) field) requested segment and shift other elements to be defragmented?
2. Or to preserve somewhere allocated segments, delete whole block (vheap), new it again, insert preserved?
3. Some other method i am not informed about, when you can (partially) deallocate parts of pointer to an array?
I am not native english speaker so i might wrongly express my self.

edit: How to know at what offset is pointer in question?
Last edited on May 11, 2011 at 5:48pm
May 11, 2011 at 5:50pm
Hey just about to head off to bed, will pick this back up in the morning, just struggling in understanding the derived class and exactly how it works with the virtual methods ect. Also VHeap is constructed in v_heap.h and allocated a size of 2048, in the derived class do i just make a pointer to the reference of vheap? Thanks for any future help!
May 11, 2011 at 5:51pm
You've inherited vheap so you can manipulate it in MemHeap class!

edit: I am not going to bed any time soon, so if someone can explain my troubles please?
Thanks for your time.
Last edited on May 11, 2011 at 5:53pm
May 11, 2011 at 5:53pm
1. R/ You cannot shift memory that is in use. It will crash the program that uses the heap! That is the job of advanced heaps like .Net's garbage collector. Not good for C++.
2. R/ You cannot delete/new! It will delete other allocated objects plus the assignment forbids it.
3. R/No. The thing is purely mathematical.

Say this is your heap: AAAAFFFFAAAAFFFF. That is: 4 bytes (A)llocated, then 4 bytes (F)ree, then 4 A, then another 4 F. Imagine you are requested to vdelete() the second block that is allocated. This means that your heap should know that now there is a SINGLE block of 12 bytes available, as opposed to reporting 3 blocks of 4 bytes each. In other words, the heap must consolidate the free block that precedes the deleted block with the deleted block and the free block that succeeded it.
May 11, 2011 at 5:59pm
Thanks greatly, but i am still confused:
1. How (with what) to keep offsets in our heap where memory is "free"?
2. How to know at what offset is requested pointer for deletion?<-i forget that pointer holds address that it points to.
3. Do we just zero(fie) segment at requested pointer points to?

User can mix types inserted and also mix calls to vnew/vdelete witch complicates this task alot.
Last edited on May 11, 2011 at 6:12pm
May 11, 2011 at 6:15pm
1. R/ Cannot answer that. It is part of the assignment. You need to figure it out. Note the tip at the end of the assignment. You need to have very firm grasp of the pointer concept so you can merge the tip with the "no more memory allocation" restriction.
3. R/ No. Does zeroing it out help you in any way? Well, maybe. Write your own stuff and see how it goes.
May 11, 2011 at 6:32pm
Eat my shorts, you jinx. :-) AAAAAAAAAAAAAAAARGGGGGGHHHHHHHHHHHHHHHH
I don't have any clue how to handle this except to brute force this, add vector of pointers witch point to every new segment "allocated" to hold offsets and somehow to measure free space between allocated segments to be able to tell if i can insert new element in between.

I can bet on my life that this topic is going to die and i'll learn nothing :-(, unless you give me some mercy for little more hints.
Last edited on May 11, 2011 at 6:33pm
May 11, 2011 at 6:34pm
You cannot use a vector<>. It is expressly forbidden in the assignment.

Ok, one clue: You need a C-style array.
May 11, 2011 at 6:35pm
Also i don't understand this statement:

...available memory segments are
consolidated into a single available segment

Can't do this since memory is now fragmented, there is holes in "memory" and can't just hold this as one segment.
May 11, 2011 at 6:43pm

You need a C-style array.

Ok. thanks, give me time to see what are possibilities with it.
May 11, 2011 at 6:44pm
You are not being asked to consolidate ALL free segments. That would be a garbage collector.

You are asked to consolidate adjacent free memory segments.

Before vdelete(): AAAAFFFFAAAAFFFFFFFFAAAAFFFF
After vdelete() (not consolidated): AAAAFFFFFFFFFFFFFFFFAAAAFFFF
After vdelete() (consolidated): AAAAFFFFFFFFFFFFFFFFAAAAFFFF
May 11, 2011 at 7:11pm
Thanks.
Did you mean to have array of bool's to represent space avilable:
1
2
3
4
5
6
7
class MemHeap : pubic VHeap
{
private:
   ...
   bool freeSpace[HEAP_SIZE];
   ...
};


I have other problem now as i can't know how much space to free that void vdelete(void* mem) points, as this doesn't work for void:
 
sizeof(*mem);//error: expression must be a pointer to a complete object type 

so i don't know how many bool's to set as true, and user can mix differnet types in. It could be easier with templates. Ehhh...
Last edited on May 11, 2011 at 7:12pm
Pages: 12