New Posts  All Forums:Forum Nav:

C++ pointers

post #1 of 12
Thread Starter 
I have a function that creates a pointer to a class, inserts some information into that class, and then stores a unique id and the pointer into a map. I haven't finished my code, but I have a suspicion that every time I call the function, it's going to cause the pointer from the previous class to point to the current class. Basically it will only create one pointer and then keep switching what it points to. Can anyone with more experience tell me if my function will for sure do this? I only ask now because it's a large program, and this is a bug I want to catch early.

Code:
int Code_Processor::New_Prize(string id, string description, int points, int quantity){

    Prize *p;
    map <string, Prize *>::iterator pit;

    p = new Prize;

    if(points <= 0){ return -1; }
    if(quantity <= 0){ return -1; }

    p->id = id;
    p->description = description;
    p->points = points;
    p->quantity = quantity;

    for(pit = Prizes.begin(); pit != Prizes.end(); pit++){
        if(pit->second->id == id){ return -1; }
        else if(pit++ == Prizes.end()){
            Prizes.insert(make_pair(id, p));
        }
    }

    return 0;
}
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
post #2 of 12
Judging by "Prize *p;" and "p = new Prize;" I think you will be fine. You create a new pointer every time you run the function and you point to a new object every time you start the function. I haven't worked in C++ in a bit but I don't see why this would have the pointers all point the same object.

The problem you are worried about would happen if you passed in the Prize object and set that Prize object to be a new Prize I would think. There you could possibly run into the new Prize overwriting the old one in the same location so the pointers all point to the same spot in the memory.
My giant
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6600 lapped Asus P5n32-E 680i GeForce 9800GT 512MB 2x1GB (unused ATM) & 2x2Gb Corsair XMS2 PC6400 
Hard DriveOSMonitorPower
WDJS SATA-II 160GB + WD80GB + WDAAKS Raid0 320GB Aperature FSII 19" LCD Apevia DarkSide 600W 
Case
NZXT Zero 
  hide details  
Reply
My giant
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6600 lapped Asus P5n32-E 680i GeForce 9800GT 512MB 2x1GB (unused ATM) & 2x2Gb Corsair XMS2 PC6400 
Hard DriveOSMonitorPower
WDJS SATA-II 160GB + WD80GB + WDAAKS Raid0 320GB Aperature FSII 19" LCD Apevia DarkSide 600W 
Case
NZXT Zero 
  hide details  
Reply
post #3 of 12
Thread Starter 
Hmmm... well what I'm thinking is that since the pointer is getting the same name every time, the program will only see one pointer. I need each Prize to have its own unique pointer. Maybe not though because each one is going to be put into a map with its own unique id, so maybe that unique id will allow the program to differentiate between the pointers, even though they have the same name. The map the prizes are going in is declared like this:
map <string, Prize *> Prizes
Edited by superhead91 - 4/6/11 at 10:13am
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
post #4 of 12
map <string, Prize *>::iterator pit;

This line intrigues me.

The rest I understand but I have no idea what you are doing here.
Unto The Void
(16 items)
 
  
CPUMotherboardGraphicsRAM
Intel i7 6800k Gigabyte G1 X99-Phoenix SLI Gigabyte GTX 1080 G1 32GB RipJaw 4 DDR4-3000 
Hard DriveHard DriveHard DriveHard Drive
Samsung 850 500GB Crucial M4 2 x Spinpoint F1 Samsung 850 1TB 
CoolingOSMonitorMonitor
Noctua NH-D15S Windows 10 LG Ultrawide 29" Asus 24" 
KeyboardPowerCaseMouse
Logitech 810 EVGA P2 650 Watt In Win 303 Logitech G502 
  hide details  
Reply
Unto The Void
(16 items)
 
  
CPUMotherboardGraphicsRAM
Intel i7 6800k Gigabyte G1 X99-Phoenix SLI Gigabyte GTX 1080 G1 32GB RipJaw 4 DDR4-3000 
Hard DriveHard DriveHard DriveHard Drive
Samsung 850 500GB Crucial M4 2 x Spinpoint F1 Samsung 850 1TB 
CoolingOSMonitorMonitor
Noctua NH-D15S Windows 10 LG Ultrawide 29" Asus 24" 
KeyboardPowerCaseMouse
Logitech 810 EVGA P2 650 Watt In Win 303 Logitech G502 
  hide details  
Reply
post #5 of 12
The name of the pointer is irrelevant outside of the function and it is only relevant inside the function for each call of the function. If it worked any other way my life would be a living hell and threading the same function would be impossible.

You could just copy and paste most of the code to make a real simple test and put the matter to rest. Create the map. Then call the function that adds to the map twice in a way where data in one object would be different from data in the other. Then just output the data in a format where you can compare it. Small scale tests like this help.

Edit:
Quote:
Originally Posted by Twinnuke View Post
map <string, Prize *>::iterator pit;

This line intrigues me.

The rest I understand but I have no idea what you are doing here.
He is creating an iterator named "pit" for a map object that maps a string to a Prize pointer. Its a way of iterating through enumerable objects. Here is some documentation.

Edit 2: On a side not you could check to see if the ID exists in Prizes or the other bail out situations before even creating the pointer for a new object and just call Prizes.insert at the end of the function. This should give a slight speed up because checking those situations is always required but creating the new Prize object and pointer is not if those situations are met. Basically this...

Code:
int Code_Processor::New_Prize(string id, string description, int points, int quantity){

    Prize *p;
    map <string, Prize *>::iterator pit;

if(points <= 0){ return -1; }
    if(quantity <= 0){ return -1; }

for(pit = Prizes.begin(); pit != Prizes.end(); pit++){
        if(pit->second->id == id){ return -1; }
    }

    p = new Prize;

    p->id = id;
    p->description = description;
    p->points = points;
    p->quantity = quantity;

Prizes.insert(make_pair(id, p));

    return 0;
}

Edited by Kirmie - 4/6/11 at 10:37am
My giant
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6600 lapped Asus P5n32-E 680i GeForce 9800GT 512MB 2x1GB (unused ATM) & 2x2Gb Corsair XMS2 PC6400 
Hard DriveOSMonitorPower
WDJS SATA-II 160GB + WD80GB + WDAAKS Raid0 320GB Aperature FSII 19" LCD Apevia DarkSide 600W 
Case
NZXT Zero 
  hide details  
Reply
My giant
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6600 lapped Asus P5n32-E 680i GeForce 9800GT 512MB 2x1GB (unused ATM) & 2x2Gb Corsair XMS2 PC6400 
Hard DriveOSMonitorPower
WDJS SATA-II 160GB + WD80GB + WDAAKS Raid0 320GB Aperature FSII 19" LCD Apevia DarkSide 600W 
Case
NZXT Zero 
  hide details  
Reply
post #6 of 12
Thread Starter 
Quote:
Originally Posted by Twinnuke View Post
map <string, Prize *>::iterator pit;

This line intrigues me.

The rest I understand but I have no idea what you are doing here.
Lol... yeah. Iterators are one of those ugly C++ functions. Do you know what a map is? This function is only part of my code. Outside the function a map is created, and an iterator is required to traverse the map.

And kirmie, thanks for the help. The reason I asked about this now is because I don't wanna have to try and find this after I write my whole program. I guess I need to figure out how to output a map to see the contents.

Edit: Ninja'd on the iterator explanation... lol
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
post #7 of 12
Quote:
Originally Posted by superhead91 View Post
Lol... yeah. Iterators are one of those ugly C++ functions. Do you know what a map is? This function is only part of my code. Outside the function a map is created, and an iterator is required to traverse the map.

And kirmie, thanks for the help. The reason I asked about this now is because I don't wanna have to try and find this after I write my whole program. I guess I need to figure out how to output a map to see the contents.

Edit: Ninja'd on the iterator explanation... lol
Super ninja! I did it a second time. Guess I should have looked to see if a post popped in while editing. The second thing I added isn't too important most of the time but just a good practice to take.
My giant
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6600 lapped Asus P5n32-E 680i GeForce 9800GT 512MB 2x1GB (unused ATM) & 2x2Gb Corsair XMS2 PC6400 
Hard DriveOSMonitorPower
WDJS SATA-II 160GB + WD80GB + WDAAKS Raid0 320GB Aperature FSII 19" LCD Apevia DarkSide 600W 
Case
NZXT Zero 
  hide details  
Reply
My giant
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6600 lapped Asus P5n32-E 680i GeForce 9800GT 512MB 2x1GB (unused ATM) & 2x2Gb Corsair XMS2 PC6400 
Hard DriveOSMonitorPower
WDJS SATA-II 160GB + WD80GB + WDAAKS Raid0 320GB Aperature FSII 19" LCD Apevia DarkSide 600W 
Case
NZXT Zero 
  hide details  
Reply
post #8 of 12
Thread Starter 
Quote:
Originally Posted by Kirmie View Post
Super ninja! I did it a second time. Guess I should have looked to see if a post popped in while editing. The second thing I added isn't too important most of the time but just a good practice to take.
Yeah. I understand. Creating a pointer I'm not going to end up using won't hurt me in a small program, but in anything large scale you've got memory leaks on your hands.
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
post #9 of 12
Quote:
Originally Posted by superhead91 View Post
I have a function that creates a pointer to a class, inserts some information into that class, and then stores a unique id and the pointer into a map. I haven't finished my code, but I have a suspicion that every time I call the function, it's going to cause the pointer from the previous class to point to the current class. Basically it will only create one pointer and then keep switching what it points to. Can anyone with more experience tell me if my function will for sure do this? I only ask now because it's a large program, and this is a bug I want to catch early.

Code:
int Code_Processor::New_Prize(string id, string description, int points, int quantity){

    Prize *p;
    map <string, Prize *>::iterator pit;

    p = new Prize;

    if(points <= 0){ return -1; }
    if(quantity <= 0){ return -1; }

    p->id = id;
    p->description = description;
    p->points = points;
    p->quantity = quantity;

    for(pit = Prizes.begin(); pit != Prizes.end(); pit++){
        if(pit->second->id == id){ return -1; }
        else if(pit++ == Prizes.end()){
            Prizes.insert(make_pair(id, p));
        }
    }

    return 0;
}
There is no need for the 'new' in this case. You can create 'Prize' without the new keyword, and because it remains in the map it will remain in scope, then your map is no longer a map of strings to pointers, but just a map of strings to prizes. Also, if you 'return -1' in your example above you end up leaking memory because of your use of 'new', but if you don't use 'new' you're fine. Also, as in the other thread, passing in your strings by reference will prevent the strings from being copied every time New_Prize() is called.

Code:
int Code_Processor::New_Prize(const std::string& id, const std::string& description, int points, int quantity)
{
    if(points <= 0){ return -1; }
    if(quantity <= 0){ return -1; }

    Prize p;
    p->id = id;
    p->description = description;
    p->points = points;
    p->quantity = quantity;

    // If a 'Prize' by 'id' doesn't exist, create it
    if ( Prizes.find(id) == Prizes.end() )
   {
         Prizes.insert(make_pair(id, p));
   }

    return 0;
}
Unrelated, I'm curious why the function returns an int instead of a bool.

You could also create a constructor in the class/struct 'Prize' to cleanup your assignments to it:
Code:
class Prize
{
     Prize(const std::string& id, const std::string& description, int points, int quantity) :
         m_id(id)
        , m_desc(description)
        , m_points(points)
        , m_quantity(quantity)
     {
     }
};

int Code_Processor::New_Prize(const std::string& id, const std::string& description, int points, int quantity)
{
    if(points <= 0){ return -1; }
    if(quantity <= 0){ return -1; }

    // This ends up looking cleaner and doesn't allow somebody
    // to make a 'Prize' without setting all required values.
    Prize p(id, description, points, quantity);

    // If a 'Prize' by 'id' doesn't exist, create it
    if ( Prizes.find(id) == Prizes.end() )
   {
         Prizes.insert(make_pair(id, p));
   }

    return 0;
}
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
post #10 of 12
All i can add is remember to match every new with a delete just to be safe
Atroc.V3.2
(16 items)
 
Home Server
(9 items)
 
 
CPUMotherboardGraphicsRAM
i5 2500k [Asrock] Z77E-ITX [EVGA] GTX 770 [Samsung] 8GB 30nm 
Hard DriveCoolingCoolingOS
[Crucial] M4 64Gb [Silverstone] NT06-PRO [Gelid] 120mm PWM Windows 7 Ultimate x64 
MonitorKeyboardPowerCase
[Samsung] 22" [Microsoft] Sidewinder X4 [Silverstone] SFX450w [Silverstone] SG05 
MouseAudioOtherOther
[Microsoft] Sidewinder [Shure] SRH-840 [Shiit] Magni [Schiit] Modi 
CPUMotherboardRAMHard Drive
[AMD] Athlon X3 450 [Asus] M4A785TD-V Evo [Corsair] 4GB XMS3 DDR3 [Western Digital] 200Gb 
Hard DriveOptical DriveOSPower
[Western Digital] 2Tb [Acer] DVD RW ArchLinux [CoolerMaster] Elite 460w 
Case
[NZXT] Gamma 
  hide details  
Reply
Atroc.V3.2
(16 items)
 
Home Server
(9 items)
 
 
CPUMotherboardGraphicsRAM
i5 2500k [Asrock] Z77E-ITX [EVGA] GTX 770 [Samsung] 8GB 30nm 
Hard DriveCoolingCoolingOS
[Crucial] M4 64Gb [Silverstone] NT06-PRO [Gelid] 120mm PWM Windows 7 Ultimate x64 
MonitorKeyboardPowerCase
[Samsung] 22" [Microsoft] Sidewinder X4 [Silverstone] SFX450w [Silverstone] SG05 
MouseAudioOtherOther
[Microsoft] Sidewinder [Shure] SRH-840 [Shiit] Magni [Schiit] Modi 
CPUMotherboardRAMHard Drive
[AMD] Athlon X3 450 [Asus] M4A785TD-V Evo [Corsair] 4GB XMS3 DDR3 [Western Digital] 200Gb 
Hard DriveOptical DriveOSPower
[Western Digital] 2Tb [Acer] DVD RW ArchLinux [CoolerMaster] Elite 460w 
Case
[NZXT] Gamma 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming