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.
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?
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.
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
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.
// 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>
usingnamespace 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 = newint;
int *yPtr = newint;
int *zPtr = newint;
// 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;
}