program help

Feb 11, 2008 at 7:22pm
Hello there,

I posted this in the beginner's section and received no response, so I figure the general board might know.

Anyways, I'm writing a program to familiarize myself with pointers (the bane of C++, in my opinion, but, again, I don't know much about their power). I am trying to create a function that will deallocate a pointer to an array. Here's the code I used.

1
2
3
4
void destroyDynamicArray(double *&numbers) {
       delete [] numbers;
       numbers = NULL;
}


Simple, I know. But I thought it would work. And it doesn't. I use a ccmalloc program, and I'm getting constant memory leaks. Anyone know how I can fix this?

Thanks
Feb 11, 2008 at 8:26pm
why *&numbers and not simply *numbers???????
Feb 12, 2008 at 12:32am
that I'm not really sure of... however, it seems to make sense to whoever wrote the book I'm reading.
Feb 13, 2008 at 2:04pm
it seems to be correct.
could you show how your code allocate array dinamically?

for DimitriuS:
"double *" is to says that the following variable is a pointer to a memory location containing a double.
Normally functions when activated crate a copy of parameters passed them so they can work with the paramerers without side effect on the original variables passed as arguments.
The "&" symbol says that function must use the poiter passed and have not to make a copy of it.
So using & before a parameter identifier you declare that you want that the function modify variable passed.
Finally "*&" have a sense when you want to modify the passed object.

I hope my poor english is clear enough to make you understand what I would like to tell.
Last edited on Feb 13, 2008 at 2:06pm
Feb 13, 2008 at 5:14pm
here's the create function

1
2
3
4
5
6
7
8
double *createDynamicArray(int arraySize) {
       const int arrSize = arraySize;
       double *arr;
       double newArr[arrSize];

       arr = newArr;
       return arr;
}


And I know there's a much easier way of going about that, but I'm rather on the new side of C++ programming, and didn't see it right away.

Here's what I think woulda been a little easier

1
2
3
4
double *createDynamicArray(int arraySize) {
       double * arr = new double[arraySize];
       return arr;
}
Last edited on Feb 13, 2008 at 5:22pm
Feb 13, 2008 at 6:02pm
Ok, now I'm in it!

This is normal,
C++ array manager is smart.
It allocates an array of 8 Kb.
If you add enough elements before it's full it automatically increase his size.
This way you have a compromise between high performance and low memory usage.
So if you don't add elements to the array it has a little size.

Try using your ccmalloc program during the execution of the following program
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
#include <iostream.h>
#include <conio.h>	// getch() function need this inclusion


double * createDynamicArray(int arraySize) {
       return new double[arraySize];
}


void destroyDynamicArray(double *& numbers) {
       delete [] numbers;
       numbers = NULL;
}


/////////////////////////////////////////////
// DEBUG UTIL


void breakpoint(char * msg) {
	cout << "\n[breakpoint:]\n " << msg << "\n press any key to continue...";
	getch();
	cout << endl << endl;
}


// DEBUG UTIL
/////////////////////////////////////////////


void main() {
	double * a;
	unsigned long size = 1 * 1024 * 1024;

	breakpoint("I will dinamically allocate array");
	a = createDynamicArray(size);

	breakpoint("I will add object to the array");
	for(unsigned int i = 0; i < size; i++)
		a[i] = 1.0;

	breakpoint("array is full, I will dinamically deallocate it");
	destroyDynamicArray(a);

	breakpoint("program terminated");
}


Hope this helps!
;)
Feb 15, 2008 at 10:55am
The use of pointers will greatly increase the memory efficiency of your program. Pointers allow for the use of link lists which dynamically add to arrays as necessary.
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
// THE TECHNICAL ITEMS ILLUSTRATED IN THIS EXAMPLE:
//	1.	The use of pointer variables.
//	2.	The use of the dereferencing operator (*).
//	3.	The use of the new and delete operators.

// PROGRAM DESCRIPTION: The purpose of this program is to accept two integer
// values through the keyboard.  Then, the sum and product of the two values
// is computed and displayed.

// The purpose of this simple program is to illustrate these operations
// using pointers.


#include <iostream>


using namespace std;


int main() {

	// Define three integer pointer variables (xPtr, yPtr, and zPtr).
	// In addition, allocate memory for three integers (new int).
	// The address for each new integer is to be passed to one
	// of the integer pointer variables.  When these statements
	// have executed, each pointer variable points to an newly
	// allocated integer.
	int *xPtr = new int;
	int *yPtr = new int;
	int *zPtr = new int;


	// Use of the dereferencing operator (*) in input statements.
	// Enter a value from the keyboard into the first of the
	// allocated integers.
	cout << "Enter value one: ";
	cin >> *xPtr;

	// Target input data for the second of the allocated integers.
	cout << "Enter value two: ";
	cin >> *yPtr;


	// Use of the dereferencing operator (*) in arithmetic
	// operations and as the target of assignment statements.
	// This is identical to the similar statement in Example06a.
	// In this example, however, it is the allocated integer
	// locations (created by new int) that are used.
	*zPtr = *xPtr + *yPtr;

	// Use of the dereferencing operator (*) in an output statement.
	// Display the value found in the allocated location pointed to
	// by zPtr.
	cout << "The sum is: " << *zPtr << endl;

	// Take the product.  Target this new value for the allocated 
	// location pointed to by zPtr.
	*zPtr = *xPtr * *yPtr;

	// Display the new value found in the allocated location pointed
	// to by zPtr.
	cout << "The product is: " << *zPtr << endl;


	// Delete the three allocated integer locations.
	delete xPtr;
	delete yPtr;
	delete zPtr;

	// Exit the program.
	return 0;
}
Last edited on Feb 15, 2008 at 11:06am
Topic archived. No new replies allowed.