473,383 Members | 1,862 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Custom C++ Vector Access Control Issues

Hi all,
I've had a bit of frustration with making a class and accessing the private variables from the functions. So here's the whole scoop (code snips are located below). I've created my own custom Physics class to emulate physics, and in it I am using a vector to keep track of my ball objects I will create. The problem is that even if I completely initialize 4 balls in the constructor (which works fine by the way) as soon as I try to change their properties or call push_back to make a new object, during run it gives me Access Violations. I know this is due to a bad pointer, but that is why I switched to a vector, because it was supposed to do all the dirty work for me. My code is below, please help!

The things to notice are how a call to the private variable in the "updateBalls()" method seems to work fine. But no matter what I do I can't even make the same call from "createBall()" This is completely baffling me.

Thanks ahead of time!


Physics.h File:
Expand|Select|Wrap|Line Numbers
  1. #include <vector>
  2. #include "3DBalls.h"
  3. #include "Ball.h"
  4.  
  5.  
  6. class Physics
  7. {
  8. private:
  9.     //Ball myBalls[MAXNUMOFBALLS];
  10.     //int numberOfBalls;
  11.     std::vector<Ball> myBalls;
  12.  
  13. public:
  14.     Physics();
  15.     void createBall(D3DVECTOR position, float mass, float radius);
  16.     int getNumOfBalls();
  17.     void collisionDetection();
  18.     void collisionPrediction();
  19.     void addGravity();
  20.     void updateBalls(float timeDelta);
  21.     bool insideBall(float posX, float posY, float posZ, float radius);
  22.     void physicsLoop(float timeDelta);
  23.     D3DVECTOR getBallPosition(int ballNum);
  24.     void release();
  25. };
  26.  

Physics.cpp File
Expand|Select|Wrap|Line Numbers
  1. #include "Physics.h"
  2.  
  3.  
  4. Physics::Physics()
  5. {
  6.     //Need to create struct to handle list of objects
  7.     int i;
  8.     D3DVECTOR temp = {0.0f, 100.0f, 0.0f};
  9.     for(i=0; i<MAXNUMOFBALLS; i++) 
  10.     {
  11.         // Fully initialized Balls from constructor
  12.                                 myBalls.push_back(*(new Ball(temp, 100.0f, 5.0f)));
  13.         temp.z +=30;        
  14.     }    
  15. }
  16.  
  17. void Physics::createBall(D3DVECTOR position, float mass, float radius)
  18. {
  19.                 // If this is commented the program runs fine
  20.                 myBalls.push_back(*(new Ball(position, mass, radius)));
  21.                 // Can't even do this!!!
  22.                 myBalls.at(0).getRadius();
  23. }
  24.  
  25.  
  26. void Physics::updateBalls(float timeDelta)
  27. {
  28.     int i;
  29.     for(i=0; i<myBalls.size(); i++)
  30.     {
  31.         // For some reason this works fine!  It doesn't make sense
  32.                                 myBalls.at(i).calculateNewPosition(timeDelta);
  33.     }
  34. }
Aug 15 '07 #1
7 2006
RedSon
5,000 Expert 4TB
Instead of doing two or three operations on a Ball object in one line of code why dont you try breaking it down even more. Then you can see what calls are causing problems. For example

Expand|Select|Wrap|Line Numbers
  1. Ball tempBall = myBall.at(i);
  2. tempBall.calculateNewPosition(timeDelta);
  3. //use debug to verify that the new position was calculated properly
  4. myBall.at(i) = tempBall;
Now you can watch every line of code as it executes and see what the result was.
Aug 15 '07 #2
Instead of doing two or three operations on a Ball object in one line of code why dont you try breaking it down even more. Then you can see what calls are causing problems. For example

Expand|Select|Wrap|Line Numbers
  1. Ball tempBall = myBall.at(i);
  2. tempBall.calculateNewPosition(timeDelta);
  3. //use debug to verify that the new position was calculated properly
  4. myBall.at(i) = tempBall;
Now you can watch every line of code as it executes and see what the result was.


Thanks for a quick reply. I guess I should have specified a bit further. I've actually debugged this code line by line and the Access Violation actually occurs inside of the vector class. So somehow the vector pointer is getting corrupt. (possibly a compiler bug?) I'm just looking for some sort of confirmation that I'm not doing something entirely stupid.

The same thing happened when I used my own array of Balls. For some reason when I call createBall() I get access violations no matter which variable I am trying to access. I can created an Int in the class, and for some reason I get Access violations from the createBall(); class, even if I am only trying to read it in memory.

And actually, as long as the Balls are created in the constructor (ie I use a loop to make 5 Balls), all the other code runs fine EXCEPT createBall(). The problem is I would like to make new balls on command.
Aug 15 '07 #3
ilikepython
844 Expert 512MB
Thanks for a quick reply. I guess I should have specified a bit further. I've actually debugged this code line by line and the Access Violation actually occurs inside of the vector class. So somehow the vector pointer is getting corrupt. (possibly a compiler bug?) I'm just looking for some sort of confirmation that I'm not doing something entirely stupid.

The same thing happened when I used my own array of Balls. For some reason when I call createBall() I get access violations no matter which variable I am trying to access. I can created an Int in the class, and for some reason I get Access violations from the createBall(); class, even if I am only trying to read it in memory.

And actually, as long as the Balls are created in the constructor (ie I use a loop to make 5 Balls), all the other code runs fine EXCEPT createBall(). The problem is I would like to make new balls on command.
Try storing pointers in the vector:
Expand|Select|Wrap|Line Numbers
  1. Ball *pball = new Ball(position, mass, radius);
  2. myBalls.push_back(pball);
  3.  
That might be difficult if your design depends on objects.
Declare the vector like this:
Expand|Select|Wrap|Line Numbers
  1.     std::vector<Ball *> myBalls;
  2.  
Also, don't forget to free your memory. If you use objects you have to do this:
Expand|Select|Wrap|Line Numbers
  1. Ball *pBall = &myBalls[i]
  2. delete pBall;
  3.  
EDIT: Reading over your post, maybe it's not a good idea because it works fine in the constructor.
Aug 15 '07 #4
JosAH
11,448 Expert 8TB
Can you show us the copy constructor for the Ball class?

kind regards,

Jos
Aug 15 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
Try storing pointers in the vector:

Code: ( cpp )
Ball *pball = new Ball(position, mass, radius);
myBalls.push_back(pball);
This just ducks the real issue: The Ball class has no proper copy constructor.

It has to be this since vector does move things around when some methods execute.

By having a class of pointers, at least the pointers will copy and the error should go away only to resurface elsewhere when a copy constructor is needed.

Lastly, a vector of pointer is is not a good idea since you can't tell when it's safe to delete the pointer. Certainly, the object can't be deleted if there are other pointers to it in the program. You uses handles in a case like this. That is, you need a vector of handles to Ball. There is an article in the C/C++ Articles forum that shows you how to do this.
Aug 15 '07 #6
Can you show us the copy constructor for the Ball class?

kind regards,

Jos


This could be my problem. Being natively used to java, this is my first real C++ program. I've never even heard of a copy constructor requirement. What exactly needs to be in a copy constructor? Is this simply to create a pointer to a new object create with the same attributes? If this is the problem that would be great!
Aug 15 '07 #7
Yeah, so I feel like a complete idiot. Horrah for being a noob! Consider this thread closed.

Like the idiot I am I ran a call to "createBall" before I made an instance to the class. I found this out by putting a break point in the constructor and the createBall function. And I was confused when it didn't stop at the break point in the constructor, and when it finally clicked I felt like crawling into a hole.

I thank everyone for their help and I found out I didn't need a copy cconstructor since I don't have pointers inside the ball class, it would simply create its own satisfactory copy constructor.

Thanks Again!
Aug 15 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Robert Neville | last post by:
I am having some trouble with some old code revolving around custom form navigation buttons. My main form has a sub-form with these custom navigation buttons. In other words, the code should be...
14
by: Rolf Welskes | last post by:
Hello, I have an ObjectDataSource which has as business-object a simple array of strings. No problem. I have an own (custom) control to which I give the DataSourceId and in the custom-control...
4
by: Charles Zhang | last post by:
I created a custom server control whose rendering logic relies on session variables and cookies. It works just fine at run time. The problem is at the design time, because session variables and...
11
by: Nick Gilbert | last post by:
Hi, How can I create a custom control which will wrap its content in a header and footer? eg: Is it possible to create a .NET user control which can surround other controls? eg:...
6
by: forest demon | last post by:
i have a custom control that gets loaded at runtime. i need to be able to access a property of a control thats part of the main form, through the clcik event of the custom control. i may be...
3
by: Tomasz J | last post by:
Hello Developers, I have a control derived from System.Web.UI.WebControls.WebControl. Control has this property: public string Value { set { _value = value; } get { return _value; }
7
by: Linda Liu[MSFT] | last post by:
Hi George, I have downloaded your sample solution and built it on my machine. I got a compilation error indicating that the type of "CustomResources" doesn't exist in the following xaml code: ...
5
by: gerry | last post by:
I am trying to create a custom container control that will only ever contain a specific type of control. At design time, when a control of a different type is added to the container I would like...
4
by: =?Utf-8?B?UmljaEI=?= | last post by:
I am trying to create a project using the ASP.NET AJAX accordion control. I would like to dynamically add panes to the control with a form template added when the pane is added. I have tried...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.