C++ creating dynamic array of objects? - c++

#include "resistor.h"
void main() {
srand(GetTickCount()); // Lewt's start by seeding the random number generator so that different runs of our program will always yield fifferent results
Resistor * pResistor; // Pointer to a resistor class
int numberOfResistors = 0; // Variable will hold the user choice of the number of resistors to be created
cout << "How many resistors do you want to create?" << endl; // prompt user
cin >> numberOfResistors; // Allow the user to enter the number of resistors to be created
pResistor = new Resistor[numberOfResistors]; // Create an array of objects of class resistor and assign its address to our pointer
// You will notice that there is a logic error here. All the resistors will have the same value. This
// is because they were all created at once. To address this issue we need to create each object
// of the class resistor separately. This means using a loop. This will be up to you to do
// It should be a very easy fix
for (int i = 0; i< numberOfResistors; i++) { // This loop will be used to display the resistor values
cout << "Resistor # " << i + 1 << " has a value of " << pResistor->getResistance() << " " << (char)234 << endl; // Display value of resistor pointed to by pResistor
}
cout << "So far, we have created " << pResistor->getNumerOfResistors() << " resistor(s)" << endl; // Display total number of resistors
delete[] pResistor; // Delete the array that was created and release the memory that was allocated.
} // end of main
I'm am woriking with this code for a class assignment. As you can probably see in the comments there is a logic error that my instructor put in there and I need to fix it. I tried putting the pResistor = new Resistor[numberOfResistors]; line inside a for loop that runs the number of times spcified by user input (instead of creating an array). The problem is still that each object is still created with the same value for ResistorObject.resistance
Any input would be much appreciated
EDIT: The following code is what i tried to explain above:
for (int i = 0; i < numberOfResistors; i++) {
pResistor = new Resistor[i]; // Create an array of objects of class resistor and assign its address to our pointer
}
This however will not compile as I get the error:
error C4703: potentially uninitialized local pointer variable 'pResistor' used

srand(GetTickCount());
Resistor * pResistor = NULL;
vector<Resistor*> resList;
int numberOfResistors = 50; // Use your cin cout here
for (size_t i = 0; i < numberOfResistors; i++)
{
pResistor = new Resistor;
// Assuming some sort of pResistor->SetResistance( /* val */ )
resList.push_back(pResistor);
}
for (int i = 0; i< numberOfResistors; i++) { // This loop will be used to display the resistor values
cout << "Resistor # " << i + 1 << " has a value of " << resList[i]->getResistance() << " " << (char)234 << endl; // Display value of resistor pointed to by pResistor
}
cout << "number of resistors : " << resList.size() << endl;
Each resistor needs its own reference, and all the references must be added to an ordered list. std::vector is used to make ordered lists

Related

Issue With Freeing Dynamically Allocated Memory In C++

Before my program can free up memory and end it crashes. Crashes seem to happen on transition from the function UserDataCollection and back to main. This is only my second program using pointers so I'm still quite the newbie considering the whole point of c++ is to use pointers.
Here is the aforementioned code:
#include <iostream>
//Prototypes
void UserDataCollection(int * &movieData_ptr, int &numSurveyed); // Movie Statistics
void DisplayOutput(int *movieData_ptr, int numSurveyed); //Mean, Median, Mode (Display To Console)
//Global Constants
int main()
{
//Variables
int numSurveyed = 0;
//Pointers
int * movieData_ptr = nullptr;
movieData_ptr = new int[numSurveyed];
//"Program Start"
std::cout << "Program start...\n\n";
UserDataCollection(movieData_ptr, numSurveyed);
DisplayOutput(movieData_ptr, numSurveyed);
//Release Memory
delete[] movieData_ptr;
std::cout << "Memory Cleared.";
return 0;
}
void UserDataCollection(int * &movieData_ptr, int &numSurveyed)
{
//Get Number of Students Surveyed
std::cout << "How many students were surveyed: ";
std::cin >> numSurveyed;
//Student Data Input Loop
for (int i = 0; i < numSurveyed; i++)
{
//Get Student Data
std::cout << "Enter How many movies student " << i + 1 << " has seen in ONE month: ";
std::cin >> *(movieData_ptr + i);
//Validation Check
while (*(movieData_ptr + i) >= 337)
{
std::cout << "\nImpossible value!" << std::endl
<< "Hours in a month: 730. Average movie length: 130 minutes."
<< "Total Possible movies: 337";
std::cout << "\n\nEnter How many movies student " << i + 1 << " has seen in ONE month: ";
std::cin >> *(movieData_ptr + i);
} //end while (Validation Check)
} // end for (Data Input)
}
void DisplayOutput(int *movieData_ptr, int numSurveyed)
{
//Display loop for pointer array
for (int i = 0; i < numSurveyed; i++)
{
std::cout << *(movieData_ptr + i) << " ";
}
//End Message
std::cout << "\n\nProgram end.";
}
You never allocated any memory.
int numSurveyed = 0;
//Pointers
int * movieData_ptr = nullptr;
movieData_ptr = new int[numSurveyed];
This is the equivalent of
int *movieData_ptr = new int[0];
You are allocating size of 0 ints. This is undefined behaviour. You can't do anything useful with that pointer without a segmentation fault. You need to either pre-allocate a certain amount, and make sure you don't overflow, or dynamically allocate every time you plan to add data.
Since this is C++, it's probably better not to use raw pointers, but use vector or something instead.
Sorry:
From 5.3.4/7
When the value of the expression in a direct-new-declarator is zero, the allocation function is called to allocate an array with no elements.
From 3.7.3.1/2
The effect of dereferencing a pointer returned as a request for zero size is undefined.

How to create objects inside a loop which name depends on an int

I have an structure named "Particle" and I want to create several objects whose names depends on an int.
As I am inside a for loop the name is going to change as follows: part0, part1, part2.
for (int i = 0; i<num_particles; i++)
{
//double sample_x, sample_y, sample_theta;
string name = "part" + std::to_string(i);
Particle name;
name.id = i;
name.x = dist_x(gen);
name.y = dist_y(gen);
name.theta = dist_theta(gen);
cout << "Sample" << " " << name.x << " " << name.y << " " << name.theta << endl;
}
As you can imagine this approach doesn't work, do you have any solution?
I have updated my question, now this is my new approach:
I have created a vector and an int "number of particles":
std::vector<Particle> particles;
And the function code:
void ParticleFilter::init(double x, double y, double theta, double std[]) {
// TODO: Set the number of particles. Initialize all particles to first position (based on estimates of
// x, y, theta and their uncertainties from GPS) and all weights to 1.
// Add random Gaussian noise to each particle.
// NOTE: Consult particle_filter.h for more information about this method (and others in this file).
default_random_engine gen;
normal_distribution<double> dist_x(x, std[0]);
normal_distribution<double> dist_y(y, std[1]);
normal_distribution<double> dist_theta(theta, std[2]);
//for (int i = 0; i<num_particles; i++)
//{
//double sample_x, sample_y, sample_theta;
//string name = "part";
//+ std::to_string(i);
//Particle particles;
particles[num_particles].id =num_particles;
particles[num_particles].x = dist_x(gen);
particles[num_particles].y = dist_y(gen);
particles[num_particles].theta = dist_theta(gen);
num_particles++;
cout << "Sample" << " " << particles[num_particles].x << " " << particles[num_particles].y << " " << particles[num_particles].theta << endl;
//}
}
But it doesn't work yet, it outputs "Segmentation fault".
you can use itoa() function of cstdlib simply in your code.
for (int i = 0; i<10; i++)
{
char a[max];
string pa="part_";
string name = pa + itoa(i,a,i+1) ;
cout << "Sample" << " " << name << endl;
}
}
Sample Output:
Sample part_0
Sample part_1
Sample part_2
Sample part_3
Sample part_4
Sample part_5
Sample part_6
Sample part_7
Sample part_8
Sample part_9
This construct exists in C++, it is called std::vector.
// we want to have a bunch of variables of type Particle
// all named particles[i] for i == 0,1,2....
std::vector<Particle> particles;
// create a new particle variable
particles.emplace_back(x, y, theta);
// print the variable number 42
std::cout << particles[42];
Why do you want to down the messy road of variable naming such as var0, var1, var2 and so on? I'd recommend creating an array or vector.
It's not clear from your code snippet that why you need to create variables with different names. Moreover, your code/usecase doesn't sit right with the concept of variable scoping.

Pointer to pointer gets EXC_BAD_ACCESS when calling function

I have two classes, one called Handler and one called Dice. In my Handler class i have a private variable called Dice **dices and a public function called rollDices. And in my Dice class i have a function called toss that will randomize a number 1-6. The problem is that when the function rollDices is calling the function toss I get EXT_BAD_ACCESS in toss function. Does anyone know why, and have a solution for it?
My Handler.cpp:
void Handler::rollDices(){
Dice **allDices = new Dice*[this->nrOfDices];
this->dices = allDices;
dices[nrOfDices]= new Dice(nrOfDices);
int count =1;
for (int i = 0; i < this->nrOfDices; i++)
{
allDices[i]->toss();
cout << "Dice "<< count << ": " << allDices[i]->getValue() << endl;
count ++;
}
}
My Dice.cpp:
void Dice::toss(){
this->value = rand()%this->nrOfSides+1; //Value is a private int in Dice class
}
If you need more code i can post it, just tell me!
Dice **allDices = new Dice*[nrOfDices];
Allocates the top level pointer so now we have all of the rows in memory. When you add the columns
dices[nrOfDices]= new Dice(nrOfDices);
This does not add a new Dice to all of the rows. It adds a new Dice to one past the end of valid range of dices. What you need to do is use a loop and go through all of the rows and add a Dice to each one like
for (int i = 0; i < nrOfDices; i++)
dices[i] = new Dice(nrOfDices);
You are only allocating a single Dice object at index nrOfDices (which is of bounds by the way), if you want to allocate all Dice objects you need:
void Handler::rollDices(){
Dice **allDices = new Dice*[nrOfDices];
this->dices = allDices;
int count =1;
for (int i = 0; i < this->nrOfDices; i++)
{
dices[i] = new Dice(nrOfDices);
allDices[i]->toss();
cout << "Dice "<< count << ": " << allDices[i]->getValue() << endl;
count ++;
}
}
How about using modern C++? Try something like this:
void Handler::rollDice()
{
std::vector<Dice> allDice( nrOfDice );
int count = 1;
for( const auto & d : allDice )
{
d.toss();
cout << "Die "<< count << ": " << d.getValue() << endl;
++count;
}
}

How to access array from a class in c++? (closed)

I'm writing a copy constructor for my class, Vehicle, but I'm having trouble accessing an array that the class has in this constructor. I set other values using dot notation (vehi.vin) and that works fine, as expected. So I tried to access the array using the same idea vehi.accessories[0] but that seems to return a null value rather than the string that's actually there. And I know when I first initialize the class that this array does have the proper values in it because I can print them out. So my question is, how can I access an array from a class? Am I using dot notation wrong? Can I not use dot notation for arrays? Do I just need a get method?
code -
vehicle.cpp
Vehicle::Vehicle(const Vehicle& vehi)
{
//get values and set them equal to the local object's attributes
vin = vehi.vin;
for(int i = 0; i < vehi.numAccessories; i++)
{
//cout << "get here\n";
accessories[i] = vehi.accessories[i];
cout << accessories[i] << " " << vehi.accessories[i] << endl;
}
}
how accessories gets its values:
for(int i = 0; i < numAccessories; i++) //loop through file until all accessories for the car have been put into the array
{
getline(fin, accessories[i]); //put accessory in the next spot in the accessory array
cout << "Accessory " << (i+1) << ": " << accessories[i] << endl;
}
main.cpp declaration of Vehicle object. The constructor reads the values in using the file object passed to it.
Vehicle temp(fin);
vehicle.h
private:
string accessories[50]; //array of accessories stored in string form
Edit: When I try calling the copy constructor directly it works fine but when calling the printVehicle() function it seems to get there but fail somehow. The code below does what I want and shows me that the copy constructor works fine, so I guess it's the way it's invoked from printVehicle()
main.cpp:
Vehicle test(fin);
test.startAcc();
cout << endl << test.nextAcc() << endl;
Vehicle test2(test);
test2.startAcc();
cout << endl << test2.nextAcc() << endl;

C++ Structs in arrays

Am i doing this right, I want a map with a Integer as key, and struct as value. What is the easiest way to, say I want the object at 1. How do I retrieve the value of isIncluded? The last two lines in the code, I tried doing it, but then I realized I donĀ“t really know what is the way to retrieving values of structs in a numbered Map array.
Do I need to call cells.get(1) and assign that to a new temporarely struct to get its values?
/** set ups cells map. with initial state of all cells and their info*/
void setExcludedCells (int dimension)
{
// Sets initial state for cells
cellInfo getCellInfo;
getCellInfo.isIncluded = false;
getCellInfo.north = 0;
getCellInfo.south = 0;
getCellInfo.west = 0;
getCellInfo.east = 0;
for (int i = 1; i <= (pow(dimension, 2)); i++)
{
cells.put(i, getCellInfo);
}
cout << "Cells map initialized. Set [" << + cells.size() << "] cells to excluded: " << endl;
cells.get(getCellInfo.isIncluded);
cells.get(1);
}
the Map, is declared as an private instance variable like this:
struct cellInfo {
bool isIncluded;
int north; // If value is 0, that direction is not applicable (border of grid).
int south;
int west;
int east;
};
Map<int, cellInfo> cells; // Keeps track over included /excluded cells
From the documentation for Map, it appears that .get() returns a ValueType.
You would use it thus:
// Display item #1
std::cout << cells.get(1).isIncluded << "\n";
std::cout << cells.get(1).north << "\n";
Or, since the lookup is relatively expensive, you could copy it to a local variable:
// Display item #1 via initialized local variable
cellInfo ci = cells.get(1);
std::cout << ci.isIncluded << " " << ci.north << "\n";
// Display item #2 via assigned-to local variable
ci = cells.get(2);
std::cout << ci.isIncluded << " " << ci.north << "\n";
My best advice is to use the standard library's std::map data structure instead:
// Expensive way with multiple lookups:
std::cout << cells[1].isIncluded << " " << cells[1].north << "\n";
// Cheap way with one lookup and no copies
const cellinfo& ci(maps[1]);
std::cout << ci.isIncluded << " " << ci.north << "\n";