New Posts  All Forums:Forum Nav:

[C++] Questions - Page 8

post #71 of 91
I'm not sure if we're on the same page. If you allocated AccountID statically(no new keyboard) inside the function you are calling to return your object all you have to do is change it to dynamic and then remember to deallocate it with the delete keyword when you are done with it. That, or copy the value to a persistant space of memory for use with your bank object. I don't think you should have to alter more than a few lines of code.
BlackBox
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 3.6ghz @ 1.35V Gigabyte X38-DS4 ATI 5870 4GB Corsair Dominator 1033Mhz 
Hard DriveOSMonitorKeyboard
Hitachi P7K500 320GB 64-bit Gentoo Linux / 64-bit Vista Ultimate Sceptre X24-WG 24" WUXGA Deck 82-key 
PowerCaseMouse
Corsair 750TX BlackBox (scratch built) Logitech G5 
  hide details  
Reply
BlackBox
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 3.6ghz @ 1.35V Gigabyte X38-DS4 ATI 5870 4GB Corsair Dominator 1033Mhz 
Hard DriveOSMonitorKeyboard
Hitachi P7K500 320GB 64-bit Gentoo Linux / 64-bit Vista Ultimate Sceptre X24-WG 24" WUXGA Deck 82-key 
PowerCaseMouse
Corsair 750TX BlackBox (scratch built) Logitech G5 
  hide details  
Reply
post #72 of 91
Thread Starter 
Ok, in the function, I now have a Bank object declared like this:

Bank *SearchResult = new Bank;

Still crashing at the same point though.
Megadoomer
(14 items)
 
Family Computer
(13 items)
 
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T @ 4.0Ghz ASUS M4A89GTD PRO Sparkle GTS 450 2x4GB G-Skill Sniper 
Hard DriveCoolingOSMonitor
Samsung F1 1TB CM Hyper 212+ Windows 7 Professional x64 Samsung T220 
KeyboardPowerCaseMouse
Logitech MX3000 Laser CM 1000M HAF 922 Logitech VX Revolution 
CPUMotherboardGraphicsRAM
Q6600 Asus PN5-D 750i Evga GTS 250 2x2GB Crucial Ballistix 
Hard DriveOptical DriveOSMonitor
750GB Hitachi Samsung Super Writemaster Windows 7 Professional x64 19" Dell 
PowerCase
Corsair 450VX Antec 900 
  hide details  
Reply
Megadoomer
(14 items)
 
Family Computer
(13 items)
 
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T @ 4.0Ghz ASUS M4A89GTD PRO Sparkle GTS 450 2x4GB G-Skill Sniper 
Hard DriveCoolingOSMonitor
Samsung F1 1TB CM Hyper 212+ Windows 7 Professional x64 Samsung T220 
KeyboardPowerCaseMouse
Logitech MX3000 Laser CM 1000M HAF 922 Logitech VX Revolution 
CPUMotherboardGraphicsRAM
Q6600 Asus PN5-D 750i Evga GTS 250 2x2GB Crucial Ballistix 
Hard DriveOptical DriveOSMonitor
750GB Hitachi Samsung Super Writemaster Windows 7 Professional x64 19" Dell 
PowerCase
Corsair 450VX Antec 900 
  hide details  
Reply
post #73 of 91
I was talking about the allocation of the AccountID/the Account object, not the Bank object. You probably just fixed half the problem.

Essentially what I'm getting at is that a function should not return pointers to/addresses of variables statically declared and allocated inside of the same function. This is bad because once the function call ends, the objects created in the function get deleted and the pointers/addresses that were returned become invalidated.

If this still isn't making sense, post the code for ReturnBankRandom() and I'll point out the issue.
BlackBox
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 3.6ghz @ 1.35V Gigabyte X38-DS4 ATI 5870 4GB Corsair Dominator 1033Mhz 
Hard DriveOSMonitorKeyboard
Hitachi P7K500 320GB 64-bit Gentoo Linux / 64-bit Vista Ultimate Sceptre X24-WG 24" WUXGA Deck 82-key 
PowerCaseMouse
Corsair 750TX BlackBox (scratch built) Logitech G5 
  hide details  
Reply
BlackBox
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 3.6ghz @ 1.35V Gigabyte X38-DS4 ATI 5870 4GB Corsair Dominator 1033Mhz 
Hard DriveOSMonitorKeyboard
Hitachi P7K500 320GB 64-bit Gentoo Linux / 64-bit Vista Ultimate Sceptre X24-WG 24" WUXGA Deck 82-key 
PowerCaseMouse
Corsair 750TX BlackBox (scratch built) Logitech G5 
  hide details  
Reply
post #74 of 91
Thread Starter 
I get what you mean about not returning the pointers, but there are only two things I declare, and neither are an Account object.

But, here's the code:

Code:

Bank* BankTree::ReturnBankRandom(Account* AccountInfo)
{
  Node *current;
  Bank *SearchResult = new Bank;
  if (root == NULL)
     cout << "ERROR: The tree is empty." << endl;
 
  else
  {
    current = root;              
    while (current != NULL)
      {
        SearchResult = Preorder2(current, AccountInfo);
        return SearchResult; 
      }//End of WHILE loop
  }//End of else
}//End of method
 
Bank* BankTree::Preorder2(Node* current, Account* AccountInfo)
{     
  if (current != NULL)
  {
    for (int i = 0; i < current->info->CustomerInfo->NoofAccounts; i++)
    {
      if (current->info->CustomerInfo != NULL && current->info->CustomerInfo->Accounts[i].AccountID != 0)
      {
        if (current->info->CustomerInfo->Accounts[i].AccountID == AccountInfo->AccountID)
        {
          /*cout << endl
               << "CustomerID: " << current->info->CustomerInfo->CustomerID << endl
               << "Customer name: " << current->info->CustomerInfo->FirstName
               << " " << current->info->CustomerInfo->LastName << endl
               << "Customer address: " << current->info->CustomerInfo->Address << endl
               << "Account Balance: ";
               Comma(current->info->CustomerInfo->Accounts[i].Balance, 1);
               cout << endl;*/
          return current->info;
        }
      }
    }   
    Preorder2(current->Llink, AccountInfo);
    Preorder2(current->Rlink, AccountInfo);
  }
  return NULL;

Megadoomer
(14 items)
 
Family Computer
(13 items)
 
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T @ 4.0Ghz ASUS M4A89GTD PRO Sparkle GTS 450 2x4GB G-Skill Sniper 
Hard DriveCoolingOSMonitor
Samsung F1 1TB CM Hyper 212+ Windows 7 Professional x64 Samsung T220 
KeyboardPowerCaseMouse
Logitech MX3000 Laser CM 1000M HAF 922 Logitech VX Revolution 
CPUMotherboardGraphicsRAM
Q6600 Asus PN5-D 750i Evga GTS 250 2x2GB Crucial Ballistix 
Hard DriveOptical DriveOSMonitor
750GB Hitachi Samsung Super Writemaster Windows 7 Professional x64 19" Dell 
PowerCase
Corsair 450VX Antec 900 
  hide details  
Reply
Megadoomer
(14 items)
 
Family Computer
(13 items)
 
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T @ 4.0Ghz ASUS M4A89GTD PRO Sparkle GTS 450 2x4GB G-Skill Sniper 
Hard DriveCoolingOSMonitor
Samsung F1 1TB CM Hyper 212+ Windows 7 Professional x64 Samsung T220 
KeyboardPowerCaseMouse
Logitech MX3000 Laser CM 1000M HAF 922 Logitech VX Revolution 
CPUMotherboardGraphicsRAM
Q6600 Asus PN5-D 750i Evga GTS 250 2x2GB Crucial Ballistix 
Hard DriveOptical DriveOSMonitor
750GB Hitachi Samsung Super Writemaster Windows 7 Professional x64 19" Dell 
PowerCase
Corsair 450VX Antec 900 
  hide details  
Reply
post #75 of 91
OK, two things. First, you've got a memory leak in ReturnBankRandom(). You allocate memory for a Bank object on the same line you declare SearchResult, but then you change SearchResult to point to what you've found. When you change SearchResult to point to something else, you lose access to the memory that was dynamically allocated and you have no way to free/delete it. This is a memory leak. To fix it, just initialize SearchResult to 0/Null rather than allocate memory. There's no reason to allocate memory for SearchResult because you aren't going to use it for anything.

The second thing I see is a problem with your recursive Preorder2. The main bulk of the code looks ok after a quick skim, but your recursive calls at the end:

Preorder2(current->Llink, AccountInfo);
Preorder2(current->Rlink, AccountInfo);

are problematic because you discard their return values. If you do a trace of this function, it will only return non-null if the value you are searching for is at the root of the tree. If it is not at the root of the tree, the address gets discarded and null is returned at the end of the function. This is most likely what is causing the seg faults.

I have another piece of advise for you- when working with functions that return pointers I would do one of two things. If you are writing the function, you can write it in such a way such that it can never possibly return null. The other option is to do a check on the return value of every function that you call which returns an address before you try to deference it. Choosing the former method means that you can call your own functions without checking their return values after every call. In most cases I would suggest the latter option of performing a check after each call. It is much easier and safer than the first and it forces good habits that will protect you when calling code that someone else has written. If you really know what you're doing you can opt for the first option, but experience tells me that taking that route tends to over-complicate things and typically causes more trouble than its worth.

Bottom line- always check your return values. If you did this where you are calling ReturnBankRandom(), you would know that it is returning null in some cases and your program would not crash.
BlackBox
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 3.6ghz @ 1.35V Gigabyte X38-DS4 ATI 5870 4GB Corsair Dominator 1033Mhz 
Hard DriveOSMonitorKeyboard
Hitachi P7K500 320GB 64-bit Gentoo Linux / 64-bit Vista Ultimate Sceptre X24-WG 24" WUXGA Deck 82-key 
PowerCaseMouse
Corsair 750TX BlackBox (scratch built) Logitech G5 
  hide details  
Reply
BlackBox
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 3.6ghz @ 1.35V Gigabyte X38-DS4 ATI 5870 4GB Corsair Dominator 1033Mhz 
Hard DriveOSMonitorKeyboard
Hitachi P7K500 320GB 64-bit Gentoo Linux / 64-bit Vista Ultimate Sceptre X24-WG 24" WUXGA Deck 82-key 
PowerCaseMouse
Corsair 750TX BlackBox (scratch built) Logitech G5 
  hide details  
Reply
post #76 of 91
Thread Starter 
Quote:
Originally Posted by jpz View Post
OK, two things. First, you've got a memory leak in ReturnBankRandom(). You allocate memory for a Bank object on the same line you declare SearchResult, but then you change SearchResult to point to what you've found. When you change SearchResult to point to something else, you lose access to the memory that was dynamically allocated and you have no way to free/delete it. This is a memory leak. To fix it, just initialize SearchResult to 0/Null rather than allocate memory. There's no reason to allocate memory for SearchResult because you aren't going to use it for anything.
That's what I had in the first place, then you said stuff about dynamically allocating it, so I changed it.

Quote:
Originally Posted by jpz View Post
The second thing I see is a problem with your recursive Preorder2. The main bulk of the code looks ok after a quick skim, but your recursive calls at the end:

Preorder2(current->Llink, AccountInfo);
Preorder2(current->Rlink, AccountInfo);

are problematic because you discard their return values. If you do a trace of this function, it will only return non-null if the value you are searching for is at the root of the tree. If it is not at the root of the tree, the address gets discarded and null is returned at the end of the function. This is most likely what is causing the seg faults.
Ok, I'm a bit confused here. What return values am I discarding?
Won't it just not return anything if the correct AccountID is not found (it'll never go into the IF statement), and then continue on? But then once it is found, won't it return to the first function?
Megadoomer
(14 items)
 
Family Computer
(13 items)
 
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T @ 4.0Ghz ASUS M4A89GTD PRO Sparkle GTS 450 2x4GB G-Skill Sniper 
Hard DriveCoolingOSMonitor
Samsung F1 1TB CM Hyper 212+ Windows 7 Professional x64 Samsung T220 
KeyboardPowerCaseMouse
Logitech MX3000 Laser CM 1000M HAF 922 Logitech VX Revolution 
CPUMotherboardGraphicsRAM
Q6600 Asus PN5-D 750i Evga GTS 250 2x2GB Crucial Ballistix 
Hard DriveOptical DriveOSMonitor
750GB Hitachi Samsung Super Writemaster Windows 7 Professional x64 19" Dell 
PowerCase
Corsair 450VX Antec 900 
  hide details  
Reply
Megadoomer
(14 items)
 
Family Computer
(13 items)
 
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T @ 4.0Ghz ASUS M4A89GTD PRO Sparkle GTS 450 2x4GB G-Skill Sniper 
Hard DriveCoolingOSMonitor
Samsung F1 1TB CM Hyper 212+ Windows 7 Professional x64 Samsung T220 
KeyboardPowerCaseMouse
Logitech MX3000 Laser CM 1000M HAF 922 Logitech VX Revolution 
CPUMotherboardGraphicsRAM
Q6600 Asus PN5-D 750i Evga GTS 250 2x2GB Crucial Ballistix 
Hard DriveOptical DriveOSMonitor
750GB Hitachi Samsung Super Writemaster Windows 7 Professional x64 19" Dell 
PowerCase
Corsair 450VX Antec 900 
  hide details  
Reply
post #77 of 91
Quote:
Originally Posted by zodac View Post
But then once it is found, won't it return to the first function?
That's what you want it to do, but that's not what's happening. When it is found, the address gets returned to where the call was made that found the value. If the value you are searching for is not at the root of the tree, the value will get returned to the previous call to Preorder2(). When you have multiple levels of function calls as with recursion, the return value only gets returned one level up. You have to write your function such that it passes the value all the way up through all the levels. If you aren't following me, draw a tree where the value you are trying to find is not at the root and then do a hand trace of your recursion function.
BlackBox
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 3.6ghz @ 1.35V Gigabyte X38-DS4 ATI 5870 4GB Corsair Dominator 1033Mhz 
Hard DriveOSMonitorKeyboard
Hitachi P7K500 320GB 64-bit Gentoo Linux / 64-bit Vista Ultimate Sceptre X24-WG 24" WUXGA Deck 82-key 
PowerCaseMouse
Corsair 750TX BlackBox (scratch built) Logitech G5 
  hide details  
Reply
BlackBox
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 3.6ghz @ 1.35V Gigabyte X38-DS4 ATI 5870 4GB Corsair Dominator 1033Mhz 
Hard DriveOSMonitorKeyboard
Hitachi P7K500 320GB 64-bit Gentoo Linux / 64-bit Vista Ultimate Sceptre X24-WG 24" WUXGA Deck 82-key 
PowerCaseMouse
Corsair 750TX BlackBox (scratch built) Logitech G5 
  hide details  
Reply
post #78 of 91
Thread Starter 
Oh, I think I get you. So if I have to go through PreOrder2 4 times to get to the correct value, when I return it, it only goes up to the 3rd call of the function?
Megadoomer
(14 items)
 
Family Computer
(13 items)
 
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T @ 4.0Ghz ASUS M4A89GTD PRO Sparkle GTS 450 2x4GB G-Skill Sniper 
Hard DriveCoolingOSMonitor
Samsung F1 1TB CM Hyper 212+ Windows 7 Professional x64 Samsung T220 
KeyboardPowerCaseMouse
Logitech MX3000 Laser CM 1000M HAF 922 Logitech VX Revolution 
CPUMotherboardGraphicsRAM
Q6600 Asus PN5-D 750i Evga GTS 250 2x2GB Crucial Ballistix 
Hard DriveOptical DriveOSMonitor
750GB Hitachi Samsung Super Writemaster Windows 7 Professional x64 19" Dell 
PowerCase
Corsair 450VX Antec 900 
  hide details  
Reply
Megadoomer
(14 items)
 
Family Computer
(13 items)
 
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T @ 4.0Ghz ASUS M4A89GTD PRO Sparkle GTS 450 2x4GB G-Skill Sniper 
Hard DriveCoolingOSMonitor
Samsung F1 1TB CM Hyper 212+ Windows 7 Professional x64 Samsung T220 
KeyboardPowerCaseMouse
Logitech MX3000 Laser CM 1000M HAF 922 Logitech VX Revolution 
CPUMotherboardGraphicsRAM
Q6600 Asus PN5-D 750i Evga GTS 250 2x2GB Crucial Ballistix 
Hard DriveOptical DriveOSMonitor
750GB Hitachi Samsung Super Writemaster Windows 7 Professional x64 19" Dell 
PowerCase
Corsair 450VX Antec 900 
  hide details  
Reply
post #79 of 91
Yes, that's pretty much it.
BlackBox
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 3.6ghz @ 1.35V Gigabyte X38-DS4 ATI 5870 4GB Corsair Dominator 1033Mhz 
Hard DriveOSMonitorKeyboard
Hitachi P7K500 320GB 64-bit Gentoo Linux / 64-bit Vista Ultimate Sceptre X24-WG 24" WUXGA Deck 82-key 
PowerCaseMouse
Corsair 750TX BlackBox (scratch built) Logitech G5 
  hide details  
Reply
BlackBox
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 3.6ghz @ 1.35V Gigabyte X38-DS4 ATI 5870 4GB Corsair Dominator 1033Mhz 
Hard DriveOSMonitorKeyboard
Hitachi P7K500 320GB 64-bit Gentoo Linux / 64-bit Vista Ultimate Sceptre X24-WG 24" WUXGA Deck 82-key 
PowerCaseMouse
Corsair 750TX BlackBox (scratch built) Logitech G5 
  hide details  
Reply
post #80 of 91
Thread Starter 
Right, I'll go see whether I can fix it then.
Megadoomer
(14 items)
 
Family Computer
(13 items)
 
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T @ 4.0Ghz ASUS M4A89GTD PRO Sparkle GTS 450 2x4GB G-Skill Sniper 
Hard DriveCoolingOSMonitor
Samsung F1 1TB CM Hyper 212+ Windows 7 Professional x64 Samsung T220 
KeyboardPowerCaseMouse
Logitech MX3000 Laser CM 1000M HAF 922 Logitech VX Revolution 
CPUMotherboardGraphicsRAM
Q6600 Asus PN5-D 750i Evga GTS 250 2x2GB Crucial Ballistix 
Hard DriveOptical DriveOSMonitor
750GB Hitachi Samsung Super Writemaster Windows 7 Professional x64 19" Dell 
PowerCase
Corsair 450VX Antec 900 
  hide details  
Reply
Megadoomer
(14 items)
 
Family Computer
(13 items)
 
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T @ 4.0Ghz ASUS M4A89GTD PRO Sparkle GTS 450 2x4GB G-Skill Sniper 
Hard DriveCoolingOSMonitor
Samsung F1 1TB CM Hyper 212+ Windows 7 Professional x64 Samsung T220 
KeyboardPowerCaseMouse
Logitech MX3000 Laser CM 1000M HAF 922 Logitech VX Revolution 
CPUMotherboardGraphicsRAM
Q6600 Asus PN5-D 750i Evga GTS 250 2x2GB Crucial Ballistix 
Hard DriveOptical DriveOSMonitor
750GB Hitachi Samsung Super Writemaster Windows 7 Professional x64 19" Dell 
PowerCase
Corsair 450VX Antec 900 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming