I've got a program with 1 struct and 3 functions. Function 1
allocates memory to the structs. Function 2 deallocates the
memory. And function 3 tells you if the memory is allocated or not.
Code:
struct sTest
{
int i1;
int i2;
};
sTest *test1, *test2;
void AllocTest(sTest* TestToAlloc);
void DeallocTest(sTest* TestToDealloc);
void TestTest(sTest* TestToTest);
void AllocTest(sTest* TestToAlloc)
{
if(!TestToAlloc)
{
TestToAlloc=new sTest;
TestToAlloc->i1=10;
TestToAlloc->i2=11;
}
printf("%d allocated.",TestToAlloc);
}
void DeallocTest(sTest* TestToDealloc)
{
printf("%d allocated.",TestToAlloc);
if(TestToDealloc)
{
delete TestToDealloc;
TestToDealloc=NULL;
}
}
void TestTest(sTest* TestToTest)
{
if(TestToTest)
{
printf("%d allocated!",TestToTest);
}
else
{
printf("%d deallocated!",TestToTest);
}
}
In the app the you input a number 1-7 that is used in a switch to
perform one of the 3 functions on one of the 2 struct pointers.
Code:
bool bExit=false;
int choice;
for(;;)
{
printf("Enter your choice: " );
scanf("%d",&choice);
switch(choice)
{
case 1:
{
AllocTest(test1);
system("cls");
getch();
}break;
case 2:
{
AllocTest(test2);
//same system("cls") and getch() lines for 1-6, left out for
//brevity
}break;
case 3:
{
DeallocTest(test1);
}break;
case 4:
{
DeallocTest(test2);
}break;
case 5:
{
TestTest(test1);
}break;
case 6:
{
TestTest(test2);
}break;
case 7:
{
bExit=true;
DeallocTest(test1);
DeallocTest(test2);
}break;
}
if(bExit) break;
}
return(0);
Problems:
First off, I really need a good tutorial on heap management
When you choose either 1 or 2 it returns different #s every time.
As I understand what I'm returning here is a long that represents
a pointer to that structure in the heap memory. So if I choose 1
I get (for instance) 7867904. Then if I choose 1 again (without
choosing to deallocate it first) I get 7867840.
Also if I run AllocTest either test# and then test it with TestTest I
always get a return that it's deallocated (when I never chose to
deallocate it).
I'm pretty sure that it all boils down to the fact that I am really
lost when it comes to heap memory management. Does anyone
know of any good articles/tutorials on the subject? As I mentioned in an earlier post all that I could find in MSDN were
functions/structures that said they were unsupported in Win98.
Thanks in advance for any help.