Trying to calculate ASCII code sum of my "sir" (string) class - c++

#include <iostream>
#include <string.h>
using namespace std;
class sir{
int lung; //sir lenght
char* sirul; //pointer to the first character form sir
public:
sir(const char*);//constructor,for test
~sir();// destructor
char operator[](int index);
int cod();
};
int sir::cod()
{
sir u=*this;
char* s=sirul;
int i,sum,l=lung;
if (lung=0)
cout<<"ASCII code sum is 0";
else
{
for(i=0; i<l; i++)
{
sum=sum+int(s[i]);
}
}
return sum;
}
sir::sir(const char* siroriginal)
{
int lungime=strlen(siroriginal);//string length
lung = lungime+1;
sirul = new char[lung];
strcpy(sirul,siroriginal);
}
sir::~sir(){
delete sirul;
}
char sir::operator[](int index){
if(index<lung && index >=0)return sirul[index];//verificare minima, verific daca este in limite
return '\0';
}
int main(){
cout<<"--------------test for sir-----------------\n";
sir s("un sir meserias");
char c=s[1];
cout<<c<<"\n";
cout<<s.cod();
}
When I'm executing this program an error says that "double free or corruption", I don't understand what causes this error. It appears after I'm trying to calculate ASCII code sum of my sir(string) with cod method, that should return an integer value.
How can I solve the problem?

There are few problems within your code :
1. First :
You have a mismatched new[]/delete call here. Replace delete sirul by delete[] sirul.
2. Second :
Inside sir::cod, you are actually copying your object by doing
sir u=*this;
Since you are not using u at all, you should remove this line. It is the source of your double free. At the end of the cod function, u will be destroyed and your internal pointer sirul will be deleted. But since you haven't defined any copy operator, the compiler will generate it for you and you'll end up sharing a pointer across two sir instances.
When the second instance is destroyed, sirul is deleted again. Resulting in the error.
3. Third :
Next, in cod, you forgot to initialize sum to 0. That's why you have bad results.
4. Fourth :
In cod, you are doing if (sum = 0). You are missing the == here.

Related

Unlimited Object Creation in C++

While learning the dynamic object creation in C++ i have encountered a doubt . Here is my code.
And my question is , when the limiting condition in the loop is same as that of the no of objects created it works fine. But what happens when the loop works for more than the size given , it seems printing the values entered , but we have created only 4 objects and changed the condition of loop to more than 4
#include <iostream>
using namespace std;
class item{
int number;
public:
item(){
cout<<"Constructor"<<endl;
}
~item(){
cout<<"Destructor"<<endl;
}
void get_num(int num){
number = num
};
void show_num(){
cout<<"Number is "<<number<<endl;
}
};
const int size=4;
int main() {
item *itemObj = new item[size];
item *d = itemObj; //copy the address of itemObj inorder to access its member functions later
int tempNum;
for (int i = 0; i < size; ++i) {
cout<<"Enter the Number"<<endl;
cin>>tempNum;
itemObj->get_num(tempNum);
itemObj++;
}
//to print the numbers entered
for (int i = 0; i < size; ++i) {
d->show_data();
d++;
cout<<d<<endl;
}
delete itemObj;
return 0;
}
Your code isn't working fine at all. Because you change the value of the pointer that you requested from the new operator. When you call the delete for the itemObj at the last line, it doesn't have its original value.
So, instead of modifying the itemObj, you should modify the copy of it which is the pointer d here. Therefore, the problem isn't about the iteration amount of the loop. It's actually the violation on the heap memory.
Also, if you're creating a dynamic array, you should call delete [] instead of delete.

C++ - printing objects in statistically allocated array causes segmentation fault

So I'm creating a program that implements several classes representing a school, and its students and courses. I'm getting a segmentation fault when I try to prints out all the Taken objects in the studentCoursePairs[] array which represents Student objects taking a particular Course. I think my segmentation fault comes from the addTaken() function in School.cc where its job is to find the student object and course object with the given student number and course id, and then creates a new Taken object with the found student and course objects as well as a grade. I then try to add this new object to the back of the Taken collection which is studentCoursePairs.
When I comment out studentCoursePairs[i]->print() the segmentation fault goes away. I'm not exactly sure what I'm doing wrong and would appreciate some help.
I'm not sure if the other classes besides School.cc are needed but I included them anyways to help with understanding.
School.cc:
#include <iostream>
#include <iomanip>
using namespace std;
#include <string.h>
#include "School.h"
School::School(string s1) : name(s1){
numTaken = 0;
}
void School::addTaken(string number, int code, string grade){
Student* s = nullptr;
Course* c = nullptr;
for(int i = 0; i < numTaken; ++i){
if((studentsCollection->find(number, &s)) && (coursesCollection->find(code, &c))){
Taken* taken = new Taken(s, c, grade);
studentCoursePairs[i] = taken;
++numTaken;
}
}
}
void School::printTaken(){
cout << name << " === TAKEN: "<< endl;
for(int i = 0; i < sizeof(studentCoursePairs)/sizeof(studentCoursePairs[0]); ++i){
studentCoursePairs[i]->print(); //seg fault
}
}
Additional files:
StudentCollection.cc
bool StudentCollection::find(string num, Student** s){
for(int i = 0; i < size; ++i){
if(students[i]->getNumber() == num){ //find student number
*s = students[i];
}
}
}
CoursesCollection.cc
bool CoursesCollection::find(int id, Course** c){
for(int i = 0; i < numCourses; ++i){
if(courses[i]->getId() == id){ //find course id
*c = courses[i];
}
}
}
I also have a Student class and Course class which just declare and initializes information like the name, program, gpa of a student as well as the course code, instructor, name, year of a course.
Your School object has two major problems. Let us start with the one you posted in your question:
void School::printTaken(){
cout << name << " === TAKEN: "<< endl;
for(int i = 0; i < sizeof(studentCoursePairs)/sizeof(studentCoursePairs[0]); ++i){
studentCoursePairs[i]->print(); //seg fault
}
}
This for loop will always run exactly MAX_PAIRS times, as this variable was defined as
Taken* studentCoursePairs[MAX_PAIRS];
so sizeof(studentCoursePairs) === MAX_PAIRS * sizeof(studentCoursePairs[0]).
Instead, you want to loop only over the first few slots that actually contain valid pointers. You have a variable for that: numTaken. So change the condition to i < numTaken and your print loop will work.
The second major problem is in addTaken:
void School::addTaken(string number, int code, string grade){
Student* s = nullptr;
Course* c = nullptr;
for(int i = 0; i < numTaken; ++i){
if((studentsCollection->find(number, &s)) && (coursesCollection->find(code, &c))){
Taken* taken = new Taken(s, c, grade);
studentCoursePairs[i] = taken;
++numTaken;
}
}
}
Let us play computer and work out what happens if the passed in number and code are valid:
If numTaken is 0, the loop immediately stops (as 0 < 0 is false) and numTaken is not incremented. You can call addTaken as much as you want, it will never change numTaken
Assuming you fix that, let us assume numTaken = 5. On the first iteration, you check the condition and agree this is a valid number-code combination. Thus, you create a new Taken object and .. overwrite studentCoursePairs[0] with the new object. On the second iteration you do the same and overwrite studentCoursePairs[1] with an equivalent object.
That is probably not the intended behavior.
Instead, you probably want to place a new object in studentCoursePairs[numTaken] and bump numTaken:
void School::addTaken(string number, int code, string grade){
Student* s = nullptr;
Course* c = nullptr;
if((studentsCollection->find(number, &s)) && (coursesCollection->find(code, &c))){
Taken* taken = new Taken(s, c, grade);
studentCoursePairs[numTaken] = taken;
++numTaken;
}
}
Figuring out how to handle the case where the passed combination is NOT valid or when you exceed MAX_PAIRS combinations is left as an exercise to you.
EDIT: There is a third major problem in your CoursesCollection: you allocate space for one object new Course() while you treat it as an array, and you store the result in a local variable instead of a member. Your constructor should probably look like:
CoursesCollection::CoursesCollection(){
courses = new Course*[MAX_COURSES];
numCourses = 0;
}
or, using a member initializer list:
CoursesCollection::CoursesCollection()
: courses(new Course*[MAX_COURSES]), numCourses(0) {}

Can't pass class object array into function?

I am trying to use a function to calculate a grade average and store the letter grade into "letter". However, whenever I try to call the function, I get an error saying "no matching for call to "findAvg". I do not completely understand references and pointers. Is that the issue here? Any help and information is appreciated, thank you.
#include <iostream>
#include <fstream>
using namespace std;
class Student{
public:
double grades[4];
float avgGrade;
string letter;
string name;
};
void findAvg(Student f[]);
int main(){
Student students[3];
int i = 0;
fstream fin;
fin.open("input1.txt");
while(!fin.eof()){
fin >> students[i].name;
for (int j =0; j < 4;++j){
fin >> students[i].grades[j];
}
i += 1;
}
fin. close();
findAvg(students[3]);
cout << students[1].letter;
}
void findAvg(Student f[]){
for(int i = 0;i<3;++i){
f[i].avgGrade = ((f[i].grades[0] + f[i].grades[1] + f[i].grades[2] + f[i].grades[3]) /4);
if (f[i].avgGrade>=90){
f[i].letter = "A";
} else if (89>f[i].avgGrade && f[i].avgGrade<=80){
f[i].letter = "B";
} else if (79>f[i].avgGrade && f[i].avgGrade<=70){
f[i].letter = "C";
} else if (69>f[i].avgGrade && f[i].avgGrade<=60){
f[i].letter = "D";
} else {
f[i].letter = "F";
}
}
}
findAvg(students[3]);
should be
findAvg(students);
The idea that you reference the whole array, by using array[SIZE] (where SIZE is the size of the array) is a common newbie error. I guess it comes from a confusion between the array declaration and an expression. But declarations and expressions are not the same thing and different rules apply. In an expression array[n] always references an element of the array. And furthermore if n is the same as the size of the array then you are referencing an element that does not exist.
The strange thing is that you handle the arrays perfectly correctly in every other part of your code, students[i].grades[j]; for instance. But for some reason when you are calling the findAvg function you think different rules apply.
Try findAvg (students) instead of findAvg (students[3]). students[3] would give you the 4th student, which does not exist.
Your call findAvg(students[3]); is incorrect. You've defined a function that takes an array, but students[3] is a single Student object, not an array. It is also an error because it is trying to access an element outside the boundaries of the array
Try calling it as
findAvg(students);
Here you need to paas the object array
So
use this
findAvg(students);
instead of using
findAvg(students[3]);

push object in the heap using pointer to the static container

class Node_Str{
public:
string name;
string value;
string type;
Node_Str(string name,string value,string type){
name=name;
value=value;
type=type;}};
static stack<Node_Str> s;
void find_token(string input){
int cursor=0;
string current="";
while(cursor<input.length()){
char value;
value=input[cursor];
cout<<value<<endl;
if(value=='('||value==')'||value=='+'||value=='-
'||value=='*'||value=='/'){
Node_Str* p=new Node_Str("pare",string(1,value),"Pare");
s.push(*p);
cursor++;
delete p;}
if(value==' '){
cursor++;
}
if(value=='1'||value=='2'||value=='3'||value=='4'){
Node_Str* p=new Node_Str("num",string(1,value),"Number");
s.push(*p);
cursor++;
delete p;}}}
int main(){
while(!s.empty()){
cout<<s.top().value<<" ";
s.pop(); }
return 0; }
The find_token function should separate the input string by white Space and constructing the Node_Str object with the value of that string. Then in the
main function, I would like to print it. The characters are limit. Just '1', '2','3','4','+,'-','*','/'.
Input is 4 + 4 , output should be 4+4. However, there is no output.
The comments already said about memory leak and forgetting to call find.
In addition to that, stack is a container in which to last to be pushed in would be the first to be popped out. In order to output 0 1 2 3, you would need to push in the stack in the sequence of 3 2 1 0.
Just giving a better version.
#include <iostream>
#include <stack>
using std::cout;
using std::stack;
static stack<int> s;
void find()
{
int* p;
for (int i = 3; i >= 0; i--) {
p = new int(i);
s.push(*p);
delete p; // p itself does not have to be returned so it can be safely deleted here
//This can also ne replaced by directly using s.push(i)
}
}
int main() {
find();
while (!s.empty())
{
cout << s.top() << " ";
s.pop();
}
return 0;
}
as suggested by leyanpan, data should be pushed into reverse order, to get the desired output. One more point is no need of dynamic allocation for int type. It is always better to store non array built in types in stack rather than heap.
Also allocating stack data structure as static variable will extend scope up to program termination. Better to use stack object as a local variable in main and pass it as are reference argument to function find.

Run time error for dynamic memory allocation in C++

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.