Program stops working when class member function executes - c++

I know there is something wrong with the class member functions because I comment everything in them out and the program will run fine but when I uncomment anything it stops working. The constructor runs fine as well.
Here is my CharArray.h file:
#ifndef CHARARRAY_H
#define CHARARRAY_H
class CharArray
{
private:
char * pArray;
int iSize;
public:
CharArray(int size)
{
char *pArray = nullptr;
iSize = size;
pArray = new char[iSize];
pArray = '\0';
}
void setItem (int loc, char ch);
char getItem (int loc);
~CharArray()
{
delete [] pArray;
}
};
#endif // CHARARRAY_H
Here is my member functions:
#include <iostream>
#include <cstring>
#include <iomanip>
#include <cstdio>
#include "CharArray.h"
using namespace std;
void CharArray::setItem (int loc, char ch)
{
pArray[loc] = ch;
cout << pArray[loc] << endl;
return;
}
char CharArray::getItem (int loc)
{
char c;
c = pArray[loc];
return c;
}
And here is my main file:
#include <iostream>
#include <iomanip>
#include "CharArray.h"
using namespace std;
int main()
{
CharArray myChar (5);
int size;
char cstr[10] = "Drew";
myChar.setItem(1, 'A');
char c = myChar.getItem(5);
cout << c << endl;
return 0;
}

Your first problem is in the constructor:
CharArray(int size)
{
char *pArray = nullptr; // <-- unrelated to the pArray in the object!
iSize = size;
pArray = new char[iSize];
pArray = '\0'; // <-- we just lost the handle to new array
}
That last line should instead be:
*pArray = '\0';
Also, it would be better to use a more modern constructor style such as this:
CharArray(int size)
: pArray(new char[size]),
iSize(size)
{
*pArray = '\0';
}

Related

StringBuilder Class in C++ not working properly

I'm working on an assignment to create a class called StringBuilder that is used for fast string concatenation. I'm supposed to store strings in a dynamic array and have methods such as Append(string) which adds a new string to the dynamic array. The method I'm currently struggling with is GetString() that creates a single string on the heap that is the length of all the strings in the dynamic array that have been added thus far.
the code I have so far is:
okay my main problem is my GetString() function prints out hello over and over again until I force quit the program in Xcode. I don't understand what inside that method is making that happen.
My header file:
#pragma once
#include <string>
using namespace std;
class StringBuilder
{
public:
StringBuilder();
//~StringBuilder();
void GetString();
void AppendAll(string*, int);
void Length();
void Clear();
void Append(string userString);
void DoubleArray(string*& allWords, int newCapacity);
private:
string* p_array;
int capacity = 5;
};
my .cpp file :
#include "StringBuilder.hpp"
#include <iostream>
#include <string>
using namespace std;
----------
void StringBuilder::Append(string userString)
{
int nextWordPosition = 0;
for(int i=0; i < capacity ; i++)
{
p_array[i] = userString;
cout << p_array[i] << endl;
nextWordPosition +=1;
if(capacity == nextWordPosition)
{
capacity *=2;
DoubleArray(p_array, capacity * 2);
}
}
nextWordPosition++;
}
void StringBuilder::DoubleArray(string*& allWords, int newCapacity)
{
string* p_temp = new string[newCapacity];
for(int i =0; i < newCapacity / 2; i++)
{
p_temp[i] = allWords[i];
}
delete[] allWords;
allWords = p_temp;
}
void StringBuilder:: GetString()
{
for(int i=0; i < capacity ; i++)
{
cout << p_array[i]<< endl;
}
}
my main.cpp file :
#include <iostream>
#include <string>
#include "StringBuilder.hpp"
using namespace std;
int main()
{
string testString = "hello";
string test = "world!";
StringBuilder Builder1;
Builder1.Append(testString);
Builder1.Append(test);
Builder1.GetString();
return 0;
}

Store more data in iov in C++

I am writing a C++ program (see below). My goal is to store data in iov struct. I have allocated buffer of fixed length in constructor. Every time that buffer gets filled, I want to transfer data in iov and allocated new buffer of fixed length. Finally when done with data processing, I want to return iov struct. My intension here is to store all these data into iov so that if it's required in future, I can send data easily. I have written sample code. But it seems it's not working. I got an "Bus error: 10". Can someone help me?
Sample code:
#include <iostream>
#include <string>
#include <sys/uio.h>
#include <cstdlib>
using namespace std;
#define MAX_LEN 1000
#define MIN_LEN 20
class MyClass
{
public:
MyClass();
~MyClass();
void fillData(std::string &data);
private:
struct iovec *iov;
unsigned int count;
unsigned int len;
char *buf;
unsigned int total_len;
unsigned int tmp_len;
};
MyClass::MyClass()
{
cout << "Inside constructor" << endl;
total_len = MIN_LEN;
buf = (char *)malloc(MAX_LEN);
if (buf == NULL) {
cout << "Error: can’t allocate buf" << endl;
exit(EXIT_FAILURE);
}
}
MyClass::~MyClass()
{
free(buf);
}
void MyClass::fillData(std::string &data)
{
unsigned int d_len, tmp_len, offset;
d_len = data.size();
const char* t = data.c_str();
total_len += d_len;
tmp_len += d_len;
if (total_len > MAX_LEN) {
/* Allocate memory and assign to iov */
tmp_len = d_len;
}
memcpy(buf + offset, t, d_len);
/* Adjust offset */
}
int main()
{
MyClass my_obj;
int i;
std::string str = "Hey, welcome to my first class!";
for (i = 0; i < 10; i++) {
my_obj.fillData(str);
}
return 0;
}
Without understanding the intent of your program in detail, it is very clear that you forgot to reserve memory for the iov-objects themselfes.
For example, in your constructor you write iov[0].iov_base = buf, yet iov has not been allocated before.
To overcome this, somewhere in your code, before the first access to iov, you should write something like iov = calloc(100,sizeof(struct iovev)) or a c++ equivalent using new[].
Consider the following program:
struct myStruct {
char *buf;
int len;
};
int main() {
struct myStruct *myStructPtr;
myStructPtr->buf = "Herbert"; // Illegal, since myStructPtr is not initialized; So even if "Herbert" is valid, there is no place to store the pointer to literal "Herbert".
myStructPtr[0].buf = "Herbert"; // Illegal, since myStructPtr is not initialized
// but:
struct myStruct *myStructObj = new (struct myStruct);
myStructObj->buf = "Herbert"; // OK, because myStructObj can store the pointer to literal "Herbert"
myStructObj->buf = "Something else"; // OK; myStructObj can hold a pointer, so just let it point to a different portion of memory. No need for an extra "new (struct myStruct)" here
}
I took your code, which didn't exactly use anything with the iovec, and I modified it a little.
I am not sure why developers prefer buffers of char* instead of std::string
or why use a pointer which should be allocated and then deleted instead of using a std::vector
I also added a function which uses the iovec. It is called void MyClass::print_data(). It prints all the data in the vector iovecs
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <vector>
#include <unistd.h>
#include <sys/uio.h>
using namespace std;
class MyClass
{
vector<struct iovec> iovs;
vector<string> bufs;
public:
MyClass();
~MyClass();
void fill_data(const string &data);
void print_data();
};
MyClass::MyClass()
{
cout << "Inside constructor" << endl;
}
MyClass::~MyClass()
{
}
void MyClass::fill_data(const string &data)
{
stringstream stream;
stream << setw(2) << setfill(' ') << (this->bufs.size() + 1) << ". "
<< data << endl;
this->bufs.push_back(stream.str());
iovec iov = {&(bufs.back()[0]), bufs.back().size()};
this->iovs.push_back(iov);
}
void MyClass::print_data() {
writev(STDOUT_FILENO, iovs.data(), iovs.size());
}
int main() {
MyClass my_obj;
string str = "Hey, welcome to my first class!";
for (int i = 0; i < 10; ++i) {
my_obj.fill_data(str);
}
my_obj.print_data();
return 0;
}
compile it like so: g++ test.cpp

Pointing an array in c++?

I want to point an array in c++ , is it possible ?
My main code :
#include "ArrayPointerClass.h"
#include "stdafx.h"
#include <iostream>
#include <string>
int _tmain(int argc, _TCHAR* argv[])
{
float arr[2];
ArrayPointerClass::pointingArray(&arr);
return 0;
}
ArrayPointerClass.h
#pragma once
static class ArrayPointerClass
{
public:
ArrayPointerClass();
~ArrayPointerClass();
static void pointingArray(float* arr[2]);
};
ArrayPointerClass.cpp
#include "stdafx.h"
#include "ArrayPointerClass.h"
ArrayPointerClass::ArrayPointerClass()
{
}
ArrayPointerClass::~ArrayPointerClass()
{
}
void ArrayPointerClass::pointingArray(float* arr[2]){
float newArray[2] = { 2.2f, 2.2f };
*arr = newArray;
}
I've got this error :
Error 3 error C2653: 'ArrayPointerClass' : is not a class or namespace name c:\users\alex\documents\visual studio 2013\projects\pointerarray\pointerarray\pointerarray.cpp 13 1 PointerArray
Error 3 error C3861: 'pointingArray': identifier not found c:\users\alex\documents\visual studio 2013\projects\pointerarray\pointerarray\pointerarray.cpp 13 1 PointerArray
I know in C++ arrays ,arrays without length defined are not allowed . is it the reason ?
Thanks for your support
There is no way to create a static class in c++. static keyword can be applied to objects and functions.
And each array name is a pointer. Therefore subscripts cannot be given in the parameter. It is sufficient to provide the pointer type.
The modified code which works:
#include <iostream>
#include <string>
using namespace std;
class ArrayPointerClass
{
public:
ArrayPointerClass();
~ArrayPointerClass();
static void pointingArray(float* arr);
};
ArrayPointerClass::ArrayPointerClass()
{
}
ArrayPointerClass::~ArrayPointerClass()
{
}
void ArrayPointerClass::pointingArray(float* arr){
float newArray[2] = { 2.2f, 2.2f };
arr = newArray;
}
int main()
{
float arr[2];
ArrayPointerClass obj;
obj.pointingArray(arr);
return 0;
}
*arr = newArray; will not work you can't copy a C array like that .
You could have done memcpy() or std::copy() like;
memcpy( newArray, arr, 2);
std::copy( newArray, newArray+2, arr);
Thanks everybody for your answers (even there were partially working)
I found my self a solution
// PointerArray.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int* test() {
int size_needed = 2;
int* a = new int[size_needed];
a[0] = 0;
a[1] = 0;
return a;
}
int main()
{
//int arr[2] = { 1, 1 };
int* arr = test();
for (int i = 0; i < 2; i++){
cout << arr[i] << std::endl;
}
cin.get();
return 0;
}

how to reverse char array using class member pointer?

im tryin to reverse an array using pointer which is a class member:
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
class my_string
{
char* ptr;
int size;
public:
my_string(){};
my_string(char* str) : ptr(str),size(strlen(ptr)){};
char* getstr () {return ptr;};
void reverse();
int find (char);
void print();
};
void my_string::reverse()
{
int size2=size;
for (int i=0;i<(size/2);i++)
{
char tmp=ptr[i];
ptr[i]=ptr[size2-1];
ptr[size2-1]=ptr[i];
size2--;
}
}
int my_string::find(char c)
{
for (int i=0;i<size;i++)
{
if (ptr[i]==c)
return i;
}
return -1;
}
void my_string::print()
{
for (int i=0;i<size;i++)
cout<<ptr[i];
cout<<endl;
}
int main()
{
my_string s1("abcde");
s1.print();
s1.reverse();
s1.print();
}
im not gettin any errors but the reverse function is surely not working.
can someone please explain to me why?
*this is an homework assignment asking me not to use dynamic allocation or strings (for now).
You didn't mention not being able to use standard library algorithms, so
std::reverse(ptr, ptr+size);
You can use standard algorithm std::reverse declared in header <algorithm>.
For example
std::reverse( ptr, ptr + size );
But if you want to do it yourself then the function could look the following way
void my_string::reverse()
{
for ( int i = 0; i < size/2; i++ )
{
char tmp = ptr[i];
ptr[i] = ptr[size-1-i];
ptr[size-1-i] = tmp;
}
}
A test program
#include <iostream>
#include <cstring>
int main()
{
char s[] = "123456789";
char *ptr = s;
int size = std::strlen( ptr );
std::cout << s << std::endl;
for ( int i = 0; i < size/2; i++ )
{
char tmp = ptr[i];
ptr[i] = ptr[size-1-i];
ptr[size-1-i] = tmp;
}
std::cout << s << std::endl;
}
Output is
123456789
987654321

C++ VALGRIND Uninitialised value was created by a heap allocation

I have a problem. When I compile the program I don't have any errors, but when I use valgrind:
Uninitialized value was created by a heap allocation (line with new)
Conditional jump or move depends on uninitialized value(s)(line with delete)
I search through the forums however I didn't find much information which could help me.
I would be really grateful for a hint.
My program
#include <cstdlib>
#include <stdint.h>
#include <cstdio>
#include <iostream>
#include <string>
#include <istream>
#include <cstring>
#include <fstream>
#include <sstream>
#include <cctype>
using namespace std;
using std::cout;
using std::endl;
int dlugosc,miejsce;
ifstream file;
class channel
{
public:
int start;
double length;
int bytespix;
int resolution;
channel(double g) : start(g),
length(0),
bytespix(0),
resolution(0)
{
}
};
int fileopen() // opens the file and returns its size
{
file.open ("0_dlc.000", ios::in|ios::binary);
if( file.good() == true )
{
cout << "Uzyskano dostep do pliku!" << endl;
}
else
{
cout<< "File cannot open" <<endl;
}
file.seekg(0, file.end);
dlugosc = file.tellg();
return dlugosc;
}
int findword(const char* slowo,int startplace)
{
int m;
int c=0;
int cur=0;
unsigned int equal=0;
char element=0;
file.seekg (startplace, file.beg);
for(m=0;m<dlugosc;m++)
{
file.get(element);
if(element==slowo[cur])
{
equal++;
cur++;
}
else
{
equal=0;
cur=0;
if(element==slowo[cur])
{
equal++;
cur++;
}
}
if(equal==strlen(slowo))
{
return m+startplace;
}
}
return 0;
}
int findvalue(const char* wartosc,int startpoint)
{
int p;
int g;
char element=0;
char* buffer = new char[9];
miejsce = findword(wartosc,startpoint); // miejsce to global variable
file.seekg (miejsce+1, file.beg);
for(p=0;(int)element<58;p++)
{
file.get(element);
if((int)element>58 || (int)element<48)
break;
else
buffer[p] = element;
}
buffer[p]='\0';
g = atoi(buffer);
delete [] buffer;
return g;
}
int main()
{
int a,h=0,channels,start=0,length=0,resolution=0,bytespix=0,m=0;
const char* slowko="Data offset: ";
dlugosc=fileopen();
channel** kanaly=0;
kanaly = new channel*[9];
miejsce=0;
for(a=0;a<9;a++)
{
kanaly[a] = new channel(4);
start = findvalue("Data offset: ",miejsce+20);
kanaly[a]->start=start;
}
for(m=0;m<9;m++)
{
delete kanaly[m];
}
delete []kanaly;
file.close();
}
The problem is in the constructor of channel. Initialize all member variables, and the problem will go away :
class channel
{
public:
double start;
double length;
int bytespix;
int resolution;
channel(double g) : start(g),
length(0),
bytespix(0),
resolution(0)
{
}
};