I have this assignment with managing computer labs. Specifically, there are 4 labs, each has a different number of computers. Hence, I want to create a 2D array with pointers, but after trying different stuff, I count on you for this error (please!!!). Below is a part of my programme, up to where the annoying bug comes up.
I got a run-time error after 1 run (terminate called after throwing an instance of std::bad_array_new_length what(): std::bad_array_new_length) when I leave the line with comment //PROBLEM HERE as such.
Add a & in front of lab room, the compiler gave me the error: lvalue required as left operand of assignment.
Newbie in C++, first time with pointers, I'd appreciate any help.
#include <iostream>
using namespace std;
//Global variables
const int SIZE = 4;
typedef int* Stations;
Stations *labroom;
//Function declaration:
void numberOfComputers();//Receive number of computers in each lab
int menu();//Display menu options for users
void menu_processor(int option);//process user's option
int main()
{
numberOfComputers();
menu();
menu_processor(menu());
return 0;
}
void numberOfComputers ()
{ char ans;
for (int i=0;i<SIZE;i++)
{
cout<<"Enter the number of computer stations in lab "<<i+1<<": ";
do
{
cin.get(ans);
} while (ans!='\n');
labroom [i] = new int [ans-'0'];//PROBLEM HERE
cout<<"\n";
}
}
That's not c++ code, it's just (ugly) C.
In C++ we have array for static arrays and vector for dynamic arrays.
First of all, choose the name of your variables or function in a smart way: prefer getNumberOfComputersFromUser instead of numberOfComputers. What numberOfComputers means? A function name must describe what it is doing.
Here a simplified snippet:
#include <vector>
#include <array>
#include <iostream>
using namespace std;
using Station = int;
using LabRooms = array<vector<Station>, 4>;
LabRooms getNumberOfComputersFromUser()
{
LabRooms labRooms;
int roomIndex = 0;
for(auto& computersInLab : labRooms)
{
cout << "Enter the number of computer stations in lab " << ++roomIndex << ": ";
auto computerCount = 0;
cin >> computerCount;
computersInLab.resize(computerCount);
}
return labRooms;
}
Explain
array requires two template arguments: the type and the size. Elements are statically allocated, no need to new because we already know how many rooms we have. The list of computers in each room is not know so we use vector that can dynamically increase or decrease.
using LabRooms = array<vector<Station>, 4>; it's the same of typedef array<vector<Station>, 4> LabRooms but it's clearer I think
for( auto& computersInLab : labRooms) iterate over labRooms and get a reference to its elements (in this case a reference to a vector of Station. This is the same of:
for(int i = 0; i < labRooms.size(); ++i)
{
auto& computersInLab = labRooms[i];
...
}
computersInLab.resize(computerCount); resize the list of computers with the value specified from the user.
Now, labRooms is an array of 4 elements, each element is a list of Station.
Related
I have class that contains some functions with an array. One of them called pick, this function does not has an array but i want to display array element from other function, once i write code to display array element i can not get the element, although the code work and if i add normal text it will display but can not display array elements, i will attach the code below:
Note i did not attache full code only important parts
#include <iostream>
#include<stdlib.h>
#include <string>
using namespace std;
// global variable
string plantType;
int temperatures ;
string water;
string sunExposure;
bool pet;
string plant1,plant2,plant3;
class inside{
public:
void low(){
string plant1 [3]={"Snake plant","Spider plant","Aloe Vera plant"};
for(int i =0; i<3; i++){
cout << "* "<< plant1[i]<<endl;
}
}
void medium(){
string plant2 [5]={"Pothos plant","Dracaena plant","ZZ plant","Rubber plant","Philodendron Green plant"};
for(int i =0; i<5; i++){
cout << "* "<< plant2[i]<<endl;
}
}
void high(){
string plant3 [2]={"Bird’s Nest Fern plant","Peace Lily plant"};
for(int i =0; i<2; i++){
cout << "* "<< plant3[i]<<endl;
}
}
void pick(){
cout <<"Best choice for you is: ";
if (temperatures >= 13 && temperatures <=29 ){
if(water=="low"){
if(sunExposure=="fully"){
cout<<"test"<<endl;
if(pet==true){
cout<<plant1[1]<<endl; //this line cannot be executed
}
}
}}}
int main(){
cout <<"Where do you want to grow the plant (inside), or (outside)"<<endl;
cin>>grow;
if (grow == "inside"){
//inside home
cout<<endl<<"inside"<<endl;
cout<<"Enter the Temperature in (Celsius scale 13-29)"<<endl;
cin>>temperatures;
cout<<"Enter water level (low - medium - high)"<<endl;
cin>>water;
cout<<"Enter Sun Exposure (fully - partly - shady)"<<endl;
cin>>sunExposure;
cout<<"Do you have pet (true or false)? "<<endl;
cin>>pet;
inside inside;
inside.pick();
}
}
The string variables plant1[3], plant2[3] & plant3[3] are only visible to low(), medium(), high() but they're not for each other. For example, you're something trying to access a locally declared variable of foo() function in bar() function, which is impossible.
Secondly, you're trying to access plant1[3] data member in pick() but compiler doesn't know where you've actually defined those three plants' name outside of that function, just it knows plant1 variable with no arrays which is declared in the private section of the class. Hence, there's a good chance your code will get fail.
Rather you could just declare the same thing in this way:
class inside {
std::string plants1[3] = {"...", ...}; // these variables are
std::string plants2[3] = {"...", ...}; // only visible inside the
std::string plants3[3] = {"...", ...}; // class member functions
.
.
}
And after that, you'll get the line executed successfully.
I created a header file polygon.h and a polygon.cpp
polygon.h:
#pragma once
#include <iostream>
using namespace std;
class Polygon {
protected:
int numSides;
int* sides;
public:
Polygon(int nSides);
Polygon(const Polygon&);
~Polygon();
int getNumOfSides();
int perimeter(); // perimeter = hekef
bool operator==(Polygon);
};
polygon.cpp:
#include <iostream>
#include "Polygon.h"
using namespace std;
Polygon::Polygon(int nSides) {
this->numSides = nSides;
this->sides = new int[nSides];
if (nSides != 3 && nSides != 4)
{
cout << "Enter sides for polygon: " << endl;
for (int i = 0; i < nSides; i++)
cin >> this->sides[i];
}
};
Polygon::Polygon(const Polygon& other) {
this->numSides = other.numSides;
this->sides = new int[other.numSides];
for (int i = 0; i < this->numSides; i++)
this->sides[i] = other.sides[i];
};
Polygon::~Polygon() {
delete [] this->sides;
};
int Polygon::getNumOfSides() {
return this->numSides;
};
int Polygon::perimeter() {
int sum = 0;
for (int i = 0; i < this->numSides; i++)
sum += this->sides[i];
return sum;
};
bool Polygon::operator==(Polygon other) {
return (this->getNumOfSides() == other.getNumOfSides() && this->perimeter() == other.perimeter());
};
I'v created a Main file as well but it doesn't matter.
The problem is when I'm passing in for example into numSides the value 5 and the program should make from sides a new dynamic int array with 5 slots as for this example, but instead, while debugging it I find out that it creates only 1 slot as if it is just a regular integer and when I set in the 5 values into sides (as for this example with 5), sides ends up containing only the first value I have entered.
I will be happy if someone will help and even solve this problem :)
You didn’t say what debugger you are using, but for Visual Studio you can manually edit the debugger entry to be:
<pointer>, <size>
So in this case you’d say:
sides, 5
See this page for more info:
https://support.microsoft.com/en-us/help/198953/how-to-expand-an-array-pointer-in-the-visual-c-debugger-watch-window
As WhozCraig and 1201ProgramAlarm stated about std::vector in the comments above:
The debugger only knows that sides is a pointer, and doesn't know how many elements it points to. If you use std::vector you'd be able to see all of the points.
Stating the obvious, std::vector<int> sides; rather than manual memory management would make this trivial, and as a bonus, eliminates the need for a numSides member as well. That said, you're usage from the "calling" code is relevant. It should be included in your question. You're also missing a copy-assignment operator, therefore introducing a recipe for a rule of three violation. Things to work on.
The fact I didn't see all of the slots is because I'm used to c# and less working with pointers, and the int pointer was pointing on the start of the array.
Can someone explain why this code does not work? It keeps crashing when it asks for input in addCar().
I think something is wrong with copying an array, but I can't figure out what exactly. I also tried to use copy() but it didn't work either.
#include <iostream>
#include <string>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
struct Car{
string Brand;
string model;
long mileage;
};
void addCar(int *ptr, struct Car *arra){
*ptr=*ptr+1;
Car *newArr = new Car[*ptr];
memcpy(newArr, arra, (*ptr)*sizeof(Car));
cout<<"Brand ";
getline(cin,newArr[*ptr].Brand);
cout<<"Model ";
getline(cin, newArr[*ptr].model);
cout<<"mileage ";
cin>>newArr[*ptr].mileage;
arra=newArr;
};
int main(int argc, char** argv) {
int size=1;
int *ptr_size;
ptr_size=&size;
Car *tab=new Car[*ptr_size];
tab[0].Brand = "Audi";
tab[0].model = "A8";
tab[0].mileage = 14366;
addCar(*ptr_size, tab);
return 0;
}
The fail is probably here:
getline(cin,newArr[*ptr].Brand);
A bit above, you did this: *ptr=*ptr+1; and made newArr an array of *ptr elements. Arrays are origin zero. That means the first item in the array is newArr[0]. The last will be at newArr[*ptr-1], so writing into newArr[*ptr] is writing over someone else's memory. Generally a bad thing to do.
But this is also not cool:
*ptr=*ptr+1;
Car *newArr = new Car[size+1];
memcpy(newArr, arra, (*ptr)*sizeof(Car));
You increment the size of the array. That's OK.
You create a new array with the new size. That's OK.
You copy new size number of elements from the old array to the new array and over shoot the end of the old array. Not OK.
The best answer is given by Jerry Coffin and Paul McKenzie in the comments: use a std::vector. If this is not allowed... Ick.
But alrighty then.
First, memcpy literally copies a block of memory. It does not know or care what that block of memory is or what it contains. Never use memcpy unless you are copying something really, really simple like basic data type or a structure made up of nothing but basic data types. String is not basic. The data represented by a string might not be inside the string. In that case, you copy a pointer to the string and that pointer will not be valid after the death of the string. That's not a problem in your case because you don't kill the string. That leads to problem 2. Let's fix that before you get there. The easiest way (other than vector) is going to be:
for (int index = 0; index < *ptr-1; index++)
{
newArr[index] = arra[index];
}
An optimization note. You don't want to resize and copy the array every time you add to it. Consider having two integers, one size of array and the other index into array and double the size of the array every time the index is about to catch up with the size.
When you allocate any memory for data with new somebody has to clean up and put that memory back with delete. In C++ that somebody is you. so, before you arra=newArr; you need to delete[] arra;
Passing in the array index as a pointer overcomplicates. Use a reference or just pass by value and return the new index. Also, don't name a variable ptr. Use something descriptive.
void addCar(int &arrasize, struct Car *arra){
or
int addCar(int arrasize, struct Car *arra){
Next problem: int addCar(int arrasize, struct Car *arra){ passes in a pointer to arra. But you passed the pointer by value, made a copy of the pointer, so when you change the pointer inside the function, it's only the copy that got changed and the new array is not going to come back out again. So,
int addCar(int arrasize, struct Car * & arra){
Passes in a reference to the pointer and allows you to modify the pointer inside the function.
Putting all that together:
int addCar(int size, struct Car * & arra)
{
Car *newArr = new Car[size + 1];
for (int index = 0; index < size; index++)
{
newArr[index] = arra[index];
}
cout << "Brand ";
getline(cin, newArr[size].Brand);
cout << "Model ";
getline(cin, newArr[size].model);
cout << "mileage ";
cin >> newArr[size].mileage;
delete[] arra;
arra = newArr;
return size+1;
}
int main()
{
int size=1;
Car *tab=new Car[size];
tab[0].Brand = "Audi";
tab[0].model = "A8";
tab[0].mileage = 14366;
size = addCar(size, tab);
// do more stuff;
// bit of test code here
for (int index = 0; index < size; index++)
{
cout << "Car " << index << " brand =" <<tab[index].Brand << " Model=" << tab[index].model << " mileage=" <<tab[index].mileage << endl;
}
delete[] tab;
return 0;
}
When you are copying the old array to the new one you are accessing invalid memory, remember that, in that point, arra has size *ptr-1 not *ptr, so the line should be
memcpy(newArr, arra, (*ptr-1)*sizeof(Car));
also in the other lines you should insert the new value in the *ptr-1 position because the indexes in newArr go from 0 to size-1 ie *ptr-1:
cout<<"Brand ";
getline(cin,newArr[*ptr-1].Brand);
cout<<"Model ";
getline(cin, newArr[*ptr-1].model);
cout<<"mileage ";
cin>>newArr[*ptr-1].mileage;
I am a newbie for OOP concepts and while trying to solve Project Euler Problem 7, to find 10001th prime number, I tried to do it using a class but encountered 2 major errors.
instantiating the class prime_n
initializing its argument
I have posted the code here for reference:
#include<iostream>
#include<cstdio>
using namespace std;
class prime_n
{
int j,k;
int n;
int *store;
public:
prime_n(int num)
{
n=num;
store[n];
}
static int isPrime(int j)
{
for(int i=2;i*i<=j;i++)
{
if(j%i==0) return 0;
}
return 1;
}
void find_n()
{
for(int i=0;i<n;i++)
{
store[i]=0;
}
store[0]=2;
j=3;
k=1;
while(store[n-1]==0)
{
if(isPrime(j)) store[k++]=j;
j+=2;
}
}
int get_num()
{
int value=store[n-1];
return value;
}
};
int main()
{
int num, req_num;
printf("Enter the position at which prime number is to be found ");
scanf("%d",&num);
printf("\nnumber = %d",num);
prime_n p = new prime_n(num);
req_num = p.get_num();
printf("The required prime number is %d\n",req_num);
return 0;
}
It would be a great help if someone could help me figure out where I am actually going wrong. Thanks a lot in advance!
Use
prime_n p(num);
or (not recommended in this particular case)
prime_n * p = new prime_n(num);
// some other code
req_num = p->get_num(); // note the -> operator replacing . in case of pointers
delete p;
The first case declares p on stack and it is automatically deallocated when the program leaves the scope (main function in this case)
The second one allocates space on heap and p is the pointer to it. You have to deallocate the memory manually.
As for your second question, the C++ way would be
#include <iostream>
...
int num;
std::cout << "Enter the position at which prime number is to be found "
std::cin >> num;
std::cout << std::endl << "Number = " << num << std::endl;
You provide a constructor:
prime_n(int num)
{
n=num;
store[n];
}
I think you are under the impression that store[n] creates an array with n elements, but that is not so; it attempts to access the (n+1)th element of an an array. Since store does not point anywhere (we are in the constructor, after all), the program crashes.
You probably want to write store = new int[num] instead.
And then I cannot see any call to find_n() originating from get_num() which is called in main(), so that your program would for now just return a random value.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I am getting this error message:
Debug Assertion Failed!
Expression:_BLOCK_TYPE_US_VALID(pHead->nBlockUse)
while trying to do the following
#include <vector>
#include <algorithm>
using namespace std;
class NN
{
public:
NN(const int numLayers,const int *lSz,const int AFT,const int OAF,const double initWtMag,const int UEW,const double *extInitWt);
double sse;
bool operator < (const NN &net) const {return sse < net.sse;}
};
class Pop
{
int popSize;
double a;
public:
Pop(const int numLayers,const int *lSz,const int AFT,const int OAF,const double initWtMag,const int numNets,const double alpha);
~Pop();
vector<NN> nets;
void GA(...);
};
Pop::Pop(const int numLayers,const int *lSz,const int AFT,const int OAF,
const double initWtMag,const int numNets,const double alpha)
{
popSize=numNets;
a=alpha;
nets.reserve(popSize);
for(int i=0;i<popSize;i++)
{
NN *net = new NN (numLayers,lSz,AFT,OAF,initWtMag,0,0);
nets.push_back(*net);
}
}
void Pop::GA()
{
...
sort(nets.begin(),nets.end());
...
}
The error appears to be related to the sort function. I check all instances of nets vector and they seem to be OK, having different sse's. The funny thing is that I created a simpler case of the above code (see below) and it worked without any errors. I am wrecking my brain. Please help.
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class Student
{
public:
string name;
double grade;
Student(string,double);
bool operator < (const Student &st) const {return grade < st.grade;}
};
Student::Student(string stName,double stGrade)
{
name = stName;
grade = stGrade;
}
int main()
{
vector<Student> group;
Student *st;
st = new Student("Bill",3.5);
group.push_back(*st);
st = new Student("John",3.9);
group.push_back(*st);
st = new Student("Dave",3.1);
group.push_back(*st);
sort(group.begin(),group.end());
for each(Student st in group)
cout << st.name << " " << st.grade << endl;
cin.get();
return(0);
}
The _BLOCK_TYPE_IS_VALID assertion gets fired, when you overwrite the header of an block allocated by new. This happens when you slice objects, use dead objects, etc.
You should have a look at your complete code, and try to work from the data you have in your debugger. This short code snippet contains several 'curious' usage of C++, but no obvious point at which this produces the described error (at least for me).
from my experience- This type of error could be caused by Heap corruption. so.. you must first check for memory leaks. If you are using Visual studio use _CrtCheckMemory().
Thanks everybody. First, I clear the memory allocated for nets vector inside the Pop destructor by
Pop::~Pop()
{
//nets.clear();
nets.~vector<NN>();
}
The error message does not say much and I would appreciate if somebody shows me how to make MSVC 2008 to show a more detailed info. Here is what it says (I can't cut and paste it for some reason, so I am retyping it):
Debug assertion failed!
Programm: ... GANN.exe
File: ... dbgedl.cpp
line: 52
Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
For information how ...
When I press debug, the compiler shows me line 52 of file dbgdel.cpp:
_ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse));
inside
void operator delete(void *pUserData)
Here is a more of my code showing what happens before I try to sort
double Pop::GA(...)
{
for (int gen=0;gen<ngen;gen++)
{
int istart=0;
if(gen>0) istart=eliteSize;
for(int i=istart;i<popSize;i++)
nets[i].getSSE(in,tgt,ntr,discount);
for(int i=istart;i<popSize;i++)
{
cout << i << " " << nets[i].sse << endl;
}
sort(nets.begin(),nets.end());
Everything works properly up to the sort() point. The lSz pointer is used inside NN to hold the number of nodes in each layer of the neural network, for example lSz[3]={12,5,1} (12 inputs, one hidden layer with 5 neurons and one output). It is used to create a 3D array of the weights for each connection of the network. Each network NN (there are 100 of them) inside the Population has its own weight array. But they share the same lSz[] and other structural parameters, which unfortunately get copied from other NN instance to the other. I wanted to use static to declare these shared class members, but that would prevent parallelization.
I just discovered that if I do Pop construction like this
Pop::Pop(const int numLayers,const int *lSz,const int AFT,const int OAF,
const double initWtMag,const int numNets,const double alpha)
{
popSize=numNets;
a=alpha;
cout << "defined a\n";
nets.reserve(popSize);
NN *net = new NN (numLayers,lSz,AFT,OAF,initWtMag,0,0);
for(int i=0;i<popSize;i++)
{
//NN *net = new NN (numLayers,lSz,AFT,OAF,initWtMag,0,0);
nets.push_back(*net);
}
}
Then everything works, including sort(). But, that does not work for me because now the nets vector contains the same instance of NN popSize times. The idea was to intialize each of these instances individually. Each instance of NN is supposed to have its own array of weights, randomly initialized inside the NN constructor:
NN::NN(const int numLayers,const int *lSz,const int AFT,const int OAF,const double initWtMag,
const int UEW,const double *extInitWt)
{
// set number of layers and their sizes
nl=numLayers;
ls=new int[nl];
for(int i=0;i<nl;i++) ls[i]=lSz[i];
// set other parameters
aft=AFT;
oaf=OAF;
binMid=0.0;
if(aft==0) binMid=0.5;
// allocate memory for output of each neuron
out = new double*[nl];
for(int i=0;i<nl;i++) out[i]=new double[ls[i]];
// allocate memory for weights (genes)
// w[lr #][neuron # in this lr][input # = neuron # in prev lr]
w = new double**[nl];
for(int i=1;i<nl;i++) w[i]=new double*[ls[i]];
for(int i=1;i<nl;i++) // for each layer except input
for(int j=0;j<ls[i];j++) // for each neuron in current layer
w[i][j]=new double[ls[i-1]+1]; // w[][][ls[]] is bias
// seed and assign random weights (genes)
SYSTEMTIME tStart,tCurr;
GetSystemTime(&tStart);
for(;;)
{
GetSystemTime(&tCurr);
if(tCurr.wMilliseconds!=tStart.wMilliseconds) break;
}
srand(tCurr.wMilliseconds);
int iw=0;
for(int i=1;i<nl;i++) // for each layer except input
for(int j=0;j<ls[i];j++) // for each neuron in current layer
for(int k=0;k<=ls[i-1];k++) // for each input of curr neuron incl bias
if(UEW==0) w[i][j][k]=initWtMag*2.0*(rand()/(double)RAND_MAX-0.5);
else w[i][j][k]=extInitWt[iw++];
}
Sometimes its because you have a string of length x and you have accidentally put a longer word into it... thats what happened in my case.