Dynamic memory allocation - C++ - c++

I'm in the process of teaching myself C++ and am currently learning about dynamically allocating memory. Here's the code that I'm currently working with:
#include <iostream>
using namespace std;
int *memAdd(int* dyn_Point, int *lenPoint){
int *new_Dyn_Point = new int[*lenPoint * 2];
*lenPoint = *lenPoint * 2;
for(int i = 0; i < *lenPoint; i++){
new_Dyn_Point[i] = dyn_Point[i];
}
delete lenPoint;
delete[] dyn_Point;
return new_Dyn_Point;
}
int main(){
int len = 2;
int *lenPoint = &len;
int current = 0;
int val;
int *dyn_Point = new int[len];
cout << "Input a value for point 1: ";
cin >> val;
dyn_Point[current] = val;
while(val > 0){
current++;
cout << "Input a value for point " << current+1 <<" (0 to exit): ";
cin >> val;
if(current+1 == len){
*dyn_Point = *memAdd(dyn_Point, lenPoint);
cout << len;
}
dyn_Point[current] = val;
}
for(int i = 0; i < len; i++){
cout << &dyn_Point[i] << "\n";
cout << dyn_Point[i] << "\n\n";
}
delete[] dyn_Point;
}
My Question: When adding more memory does it have to increment by a certain value?
Whenever I start with a value in my "len" variable that's not 2 my program will crash either as soon as I try and allocate more memory or after more memory has been allocated and even more has to be added a second time.
Is this how it's supposed to be or am I missing something entirely here?

Your while loop need a break
while() {
//do your steps
break;
}
In function memAdd following changes required :
// *lenPoint = *lenPoint * 2;
// above line need to be commented else it will trouble the loop condition by causing over flow:
for(int i = 0; i < (*lenPoint-1); i++){
// for loop is corrected to resolve the over flow
Below delete is unnecessary since you didn't allocate memory
using this variable
// delete lenPoint;
For your question: When adding more memory does it have to increment by a certain value?
There is no hard and hard fast rule on this regard. std::vector<> double its size( memory allocation) whenever it is needed more memory. It is slightly different than your approach. You are doubling memory before reaching the allocated upper limit.
**Edit**
Compiled full code as OPs request
#include <iostream>
using namespace std;
int *memAdd(int* dyn_Point, int *lenPoint){
int *new_Dyn_Point = new int[*lenPoint * 2];
// *lenPoint = *lenPoint * 2;
for(int i = 0; i < (*lenPoint-1); i++){
new_Dyn_Point[i] = dyn_Point[i];
}
//delete lenPoint;
delete[] dyn_Point;
return new_Dyn_Point;
}
int main(){
int len = 2;
int *lenPoint = &len;
int current = 0;
int val;
int *dyn_Point = new int[len];
cout << "Input a value for point 1: ";
cin >> val;
dyn_Point[current] = val;
while(val > 0){
current++;
cout << "Input a value for point " << current+1 <<" (0 to exit): ";
cin >> val;
if(current+1 == len){
*dyn_Point = *memAdd(dyn_Point, lenPoint);
cout << len<<"\n";
}
dyn_Point[current] = val;
break;
}
for(int i = 0; i < len; i++){
cout << dyn_Point[i] << "\n";
cout << &dyn_Point[i] << "\n\n";
}
delete[] dyn_Point;
}

"My Question: When adding more memory does it have to increment by a certain value?"
Of course you have to manage your memory allocations using a design like this.
In particular you should obey the Rule of Three (Five), and to copy all of the existing elements, when reallocating memory to increase to the necessary amount.
The much better choice than doing it all yourself (which is likely to be error prone), is to use a
std::vector<int> dyn_point;
and/or alike in your class.
Memory management is taken care of in the container implementations, and you don't need to bother about it.

Related

Locating a Segmentation Fault

I have the fallowing code. I read the guide for what a segmentation fault is, but I'm not 100% sure where its actually happening within my code. It works until I start working with the dynamic array (histogram), more specifically at the //set all initial values to be zero. Within that mess after I'm not sure. Thanks!
The instructor asked to "Use a dynamic array to store the histogram.", Which I think is my issue here.
-Solved-
thanks for the help, the error was in how I initialized the array pointer
rather than
const int hSize = 10;
IntArrayPtr histogram;
histogram = new int[hSize];
I used
const int hSize = 10;
int hValues[hSize] = { 0 };
IntArrayPtr histogram;
histogram = hValues;
Which worked as the instructor wanted.
#include <iostream>
#include <vector>
using namespace std;
typedef int* IntArrayPtr;
int main() {
vector<int>grades;
int newGrade;
cout << "Input grades between 0 and 100. Input -1 to calculate histogram: " << endl;
cin >> newGrade;
grades.push_back(newGrade);
while (newGrade > 0) {
cin >> newGrade;
while (newGrade > 100) {
cout << "less than 100 plz: ";
cin >> newGrade;
}
grades.push_back(newGrade);
}
grades.pop_back();
int size = grades.size();
cout << "Calculating histogram with " << size << " grades." << endl;
//Create dynamic array for the histogram of 10 sections.
const int hSize = 10;
IntArrayPtr histogram;
histogram = new int[hSize];
}
//Make the historgram
int stackValue = 0;
for (int j = 0; j < hSize; j++) {
//Loop through the grade vector slots
for (int i = 0; i < size; i++) {
int testValue = grades[i];
//If the grade at the index is between the stack values of the histogram add one to the value of the slot
if (testValue > stackValue && testValue < stackValue + 10) {
histogram[j]++;
}
}
//After looping through the vector jump up to the next histogram slot and corresponding stack value.
stackValue += 10;
}
//Histogram output. Only output the stacks with values
for (int i = 0; i < 10; i++) {
if (histogram[i] != 0) {
cout << "Number of " << (i + 1) * 10 << "'s: " << histogram[i];
}
}
return 0;
}
Working Code:
#include <iostream>
#include <vector>
using namespace std;
typedef int* IntArrayPtr;
int main() {
vector<int>grades;
int newGrade;
cout << "Input grades between 0 and 100. Input -1 to calculate histogram: " << endl;
cin >> newGrade;
grades.push_back(newGrade);
while (newGrade > 0) {
cin >> newGrade;
while (newGrade > 100) {
cout << "less than 100 plz: ";
cin >> newGrade;
}
grades.push_back(newGrade);
}
grades.pop_back();
int size = grades.size();
cout << "Calculating histogram with " << size << " grades." << endl;
//Create dynamic array for the histogram of 10 sections.
const int hSize = 10;
int hValues[hSize] = { 0 };
IntArrayPtr histogram;
histogram = hValues;
//Make the historgram
int stackValue = 0;
for (int j = 0; j < hSize; j++) {
//Loop through the grade vector slots
for (int i = 0; i < size; i++) {
int testValue = grades[i];
//If the grade at the index is between the stack values of the histogram add one to the value of the slot
if (testValue > stackValue && testValue < stackValue + 10) {
histogram[j]++;
}
}
//After looping through the vector jump up to the next histogram slot and corresponding stack value.
stackValue += 10;
}
//Histogram output. Only output the stacks with values
for (int i = 0; i < 10; i++) {
if (histogram[i] != 0) {
cout << "Number of " << (i + 1) * 10 << "'s: " << histogram[i] << endl;
}
}
return 0;
}
histogram is a pointer, not an array.
While
int histogram[hSize] = {0};
would create a zero-initialised array, your
histogram = { 0 };
does not set any elements to zero (it couldn't, because histogram points to one int, not many).
The braces are ignored – a pretty confusing behaviour inherited from C – and it is equivalent to
histogram = 0;
that is,
histogram = nullptr;
You want
int* histogram = new int[hSize]();
The parentheses value-initialises the array, and in turn its elements.
Value-initialising integers sets them to zero.
(By the way: the habit of typedeffing away asterisks causes more problems than it solves. Don't do it.)
Seg faults are problems with accessing regions of memory you don't have access to, so you need to look at your use of pointers. It often means you have a pointer with a bad value that you just dereferenced.
In this case, the problem is this line:
histogram = { 0 };
This is not setting the histogram values to zero as you think: it's resetting the historgram pointer to zero. Then you later dereference that pointer causing your SegFault (note that this line doesn't even compile with clang, so your compiler isn't helping you any on this one).
Changing that line to:
memset(histogram, 0, hSize);
Will sort the problem in this case.
More generally, to diagnose a segfault there are two tricks I use regularly (though avoidance is better than cure):
Run the program under a debugger: the debugger will likely stop the program at the point of the fault and you can see exactly where it failed
Run the program under Valgrind or similar - that will also tell you where the error surfaced but in more complex failures can also tell you where it was caused (often not the same place).

Heap Corruption Error while doing bubble sort using dynamic arrays

Hi i am trying to create a bubble sort using dynamic arrays, the code seems to work but throws an run time error : HEAP Corruption Detected(since i am deleting dynamic arrays in the ...i donot understand why i am getting such an error). Also, the last two elements in the given array get sorted but i get the address displayed for the last element . As i am trying to learn dynamic arrays on my own.Kindly help me understand the error. Thanks in advance !!!
Array = {125,12,2,36,19}
#include "stdafx.h"
#include <iostream>
using namespace std;
void bubblesort(int* a, int length); // for bubble sort//
int _tmain(int argc, _TCHAR* argv[])
{
int size;
cout << " enter the size of array: " << endl;
cin >> size;
int* a = new int[size];
cout << "enter the elements in an array: " << endl;
for (int i = 0; i < size; i++)
cin >> *(a+i);
bubblesort(a, size);
delete[] a;
a = NULL;
return 0;
}
void bubblesort(int* a, int length)
{
int temp = 0;
for (int i = 0; i < length; i++)
{
if (a[i] > a[i+1])
{
temp = a[i+1];
a[i+1] = a[i];
a[i]= temp;
}
}
for (int i = 0; i < length; i++)
{
cout << " The elements are : " << endl;
cout << a[i] << endl;
}
}
As (it was) mentioned in the comments, you're reading outside the array.
a[i + 1] = a[i]; //When i == length - 1, this is UB
In the last iteration of the for loop, you'll overwrite whatever is after the end of the array. An array a[length] is only valid from 0 through length - 1.
Also, your bubble sort only runs once, while it is supposed to constantly run until all items are sorted.
On a subjective note, *(a+i) is identical to, but less readable than, a[i].

Passing arrays and pointers with access violation

I am working on an assignment which must pass pointers for all function
parameters. No global variables are allowed except global constants.
I'm to create an array of "bids" in main and fill it with readBids() function. This works, but I am then supposed to pass it to a function to bubble sort it. My program breaks once my sortBids function is called. I'm learning pointers now and I can't see what I am doing wrong. The Call Stack gives Project4.exe!main()Line32, which points to sortBids(bidArray, numBids);
Any help and an explanation would be very appreciated.
#include <iostream>
#include <string>
using namespace std;
string* readProductName();
int* readNumBids();
double* readBids(string,int);
void sortBids(double*, int*);
void averageBid();
void maxBid();
void totalBid();
void printReport();
int main(){
string* productName;
int* numBids;
productName = readProductName();
numBids = readNumBids();
double* bidArray = readBids(*productName, *numBids);
sortBids(bidArray, numBids);
cout << *productName << " " << *numBids << endl;
for (int i = 0; i < *numBids; i++){
cout << bidArray[i] << endl;
}
system("PAUSE");
delete productName;
delete numBids;
delete bidArray;
return 0;
}
string* readProductName(){
string* productName = new string;
cout << "\n Please enter a product name\n";
cin >> *productName;
return productName;
}
int* readNumBids(){
int* numBids = new int;
cout << "\n Please enter the number of bids\n";
cin >> *numBids;
return numBids;
}
double* readBids(string productName, int numBids){
int* size = new int;
size = &numBids;
string* productNamePtr = new string;
productNamePtr = &productName;
double *bidArray;
bidArray = new double[*size];
cout << "\nHow many bids for the " << *productNamePtr << endl;
for (int i = 0; i < *size; i++){
cout << "Please enter bid #" << i + 1 << endl;
cin >> bidArray[i];
if (bidArray[i] <= 0){
cout << "\nPlease enter an amount larger than 0\n";
i--;
}
}
return bidArray;
}
void sortBids(double* array, int *size){
bool* swap = bool{ false };
double* temp = new double;
do
{
*swap = false;
for (int count = 0; count < *size - 1; count++)
{
if (array[count] > array[count + 1])
{
*temp = array[count];
array[count] = array[count + 1];
array[count + 1] = *temp;
*swap = true;
}
}
} while (*swap);
}
Problem:
You intialise swap to 0. As swap is a pointer to bool, you have a null pointer.
You later dereference this pointer without ever having it point to a valid bool object:
*swap = true;
Tha is UB and this is why you get an access violation !
Solution
Either you define this variable as plain object bool swap = false; and use swap everywhere. Or you initialize it correctly bool *swap = new bool{false}; and you use *swap everywhere.
Miscellaneous advice:
Attention: bidArray is allocated with new[], so you have to delete[] it or risk undefined behaviour !
In pointer definitions, take the habit of puting the star next to the variable and not to the type. Why ? Because optically it is confusing:
bool* a,b; // defines a pointer to bool a, but a PLAIN BOOL b !
bool *a,b; // invites otpically to right interpretation by human reader

Incorrect Variable output with Vector Class C++

My output for the call to the temporary array size wont correctly output. It resizes as according, but I can't get the MAX to display the new value of the new array. My error is within the Resize function within the class.
#include <iostream>
#include <vector>
#include <string>
#include <math.h>
#include <ctime>
using namespace std;
class VectorClass {
private:
int * Vector;//This will be our resizeable array
int Size; //Keep track of vector current size
int MAX=10;
int growth = 5;
int num;
int Resize(int growth, int MAX);
public:
VectorClass(int growth, int Size);
~VectorClass();
int AddItem(int num);
void RemoveItem();
void Print(void);
};
VectorClass::VectorClass(int growth, int Size)
{
Size = 10;
growth = 5;
Vector = new int[Size];
}
VectorClass::~VectorClass()
{
cout << "Destructor was called." << endl;
}
//Will insert num into the vector at the current open position
int VectorClass::AddItem(int num)
{
Vector[Size] = num;
Size++; //Indicate that there isnt as much free space
if (Size == MAX)
{
Resize(Size, MAX);
}
Print();
return num;
}
//Get rid of the most recently added item
void VectorClass::RemoveItem()
{
Size--; //Tricks the vector into one fewer elements in it it currently does
Print();
}
int VectorClass::Resize(int growth, int MAX)
{
cout << "Array is full! Resizing the Array!" << endl;
//Step 1: make a copy
int * temp = new int[MAX]; //Make a new array, same size as exiting array
//loop that copies the original into the copy
for (int i = 0; i<MAX; i++)
{
temp[i] = Vector[i];
}
//Step 2: Delete the original
delete[] Vector; //Deletes all elements in the array Vector from the Heap
//Step 3: Make a bigger vector
Vector = new int[MAX + growth];
//Step 4: Reverse the copy and record the size change
for (int i = 0; i<MAX; i++)
{
Vector[i] = temp[i];
}
MAX = MAX + growth;
//Step 5: Delete the copy
delete[] temp;
cout << "Resize was called.\n" << endl;
return MAX;
}
void VectorClass::Print()
{
cout << "*******************************************************" << endl;
for (int i = 0; i< Size; i++)
{
cout << Vector[i] << endl;
}
cout << "Size = " << Size << "\tMAX = " << MAX << "\t Growth = " << growth << endl << endl;
cout << "*******************************************************" << endl;
}
int main(void)
{
VectorClass V(5,10);
for (int i = 0; i <= 4; i++)
{
int x = rand();
V.AddItem(x);
}
//Print the Vector #1
V.Print();
//Delete 2 Items
V.RemoveItem();
V.RemoveItem();
//Add 9 random Numbers
for (int i = 0; i <= 8; i++)
{
int x = rand();
V.AddItem(x);
}
//Print the Vector
V.Print();
system("pause");
return 0;
}
Several things are wrong with you code. The first one, probably not the one you care about most, is that you never free the memory. You should do it in your destructor, or even better use a std::unique_ptr to handle your memory.
Now, i believe you are yourself confused about your own variables. I see that you possess a variable member named num that you never use. Even worse, you have a parameter in AddItem with the same name. Are you sure it does what you want? The same is true for growth. I would advise you to name your member variable differently, so that you know what they are quickly. I prefixe them with "m_" for example, but you can do as you wish.
You do not need to declare your function parameters inside your class. Only in the function prototype.
Then, in your AddItem function, you use your variable Size to determine where to add the new element, but you initialize your array with it too, which means that not only you do not add your elements at the beginning of your array, you try to write them in memory you do not own!
I could continue for a long time. I am sorry but it only appears to me that you do not know C++ at all. You should go learn the basics again, and maybe start with an easier project to begin your C++ learning.
Good luck :-)

C++ code breaks into line and for the love of me I can't fix it

Basically my code is supposed to take input from the user about the runners of a marathon, and then display the 3 best times and have the ability to search and display any runner. Right now the code is still pretty bare-bones, but it should at least take the input it, organize the info in ascending order (winners function) and I was in the middle of writing the display function when I decided to compile the code to test, and then it all goes down hill.
Basically when I have to input the name of the first runner, the code breaks into this line:
static void __CLRCALL_OR_CDECL assign(_Elem& _Left, const _Elem& _Right) _NOEXCEPT
{ // assign an element
_Left = _Right;
}
First of all I have no idea what that means or why left = right, and second a friend ran my code on his machine and told me he encountered no errors.
Here is my code:
#include <iomanip>
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
#pragma once
#include <string>
using namespace std;
class race
{
private:
public:
int timeH, timeM, timeS, timeT;
int number;
string name;
void input();
};
int size;
race *runner = new race[size];
void input();
void winners();
void display();
int main(){
input();
winners();
display();
system("pause");
}
void input(){
cout << "Indique la cantidad de corredores: ";
cin >> size;
for (int i = 0; i < size; i++){
cout << "Nombre del corredor ";
cin >> runner[i].name;
cout << "Numero del corredor # "<<i;
cin >> runner[i].number;
cout << "Tiempo del corredor # " << i << endl << "Indique cuantas horas tardo :";
cin >> runner[i].timeH;
runner[i].timeH = runner[i].timeH * 3600;
cout << "Cuantos minutos: ";
cin >> runner[i].timeM;
runner[i].timeM = runner[i].timeM * 60;
cout << "Cuantos segundos: ";
cin >> runner[i].timeS;
runner[i].timeT = runner[i].timeH + runner[i].timeM + runner[i].timeS;
}
}
void winners(){
race temp;
int flag = 1;
for (int j = 1; (j <= size) && flag; j++){
flag = 0;
for (int i = 0; i < size-1; i++){
if (runner[i + 1].timeT < runner[i].timeT)
{
temp = runner[i];
runner[i] = runner[i + 1];
runner[i + 1] = temp;
flag = 1;
}
}
}
}
Thanks in advance, any help is appreciated.
int size;
race *runner = new race[size];
That's a problem right there. You're allocating memory, but you haven't set size yet (that allocation runs before main() is invoked). That means you are doing new race[0] (because size is initialized to zero, since it's a global int). Ergo, the rest of your program is invoking undefined behavior, because runner is an empty array, which you're trying to (illegally) access.
You're also leaking memory, as you never delete [] the memory you allocate with new []. I suggest heading Joachim's advice and using std::vector.
The problem is this:
int size;
race *runner = new race[size];
Uninitialized global variables are zero initialized, so size will be initialized to zero, so you're allocating a zero-sized array which means any access to the array will be out of bounds and lead to undefined behavior.
There are two ways of solving this:
Wait with the allocation until you know the size. (Not the solution I recommend.)
Use std::vector which is a dynamically sized "array" type (the solution I do recommend).