Where to place delete operator for dynamic variable - c++

I am trying to figure out where to place the delete pointerstatement in the program below. I want to clear the memory space that pointer is pointing to in order to avoid memory leaks. It seems that no matter where I put it, I get an error stating:
main(8282,0x7fff95d823c0) malloc: *** error for object 0x7fff582d3960: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
I am not sure how to fix this. Any help is appreciated.
full code:
#include <iostream>
#include <vector>
using namespace std;
int main() {
//initialize vector
vector<int> historyValues;
//initialize pointer and int variable
int *pointer;
pointer = new int;
pointer = 0;
int currentValue;
//make pointer point to the address of currentValue
pointer = &currentValue;
//increment pointer by 1 for a total of 10 times.
//since pointer is pointing at currentValue, currentValue should change also.
//push back the current value of currentValue into the vector.
for (int i = 0; i < 10; i++) {
*pointer += 1;
historyValues.push_back(currentValue);
}
//print final results
cout << "currentValue: " << currentValue << endl;
cout << "*pointer: " << *pointer << endl;
cout << "History of integers stored in currentValue: ";
for (int i = 0; i < historyValues.size(); i++) {
cout << historyValues[i] << " ";
}
cout << endl;
cout << "Program finished" << endl;
return 0;
}

The only place in this program where pointer refers to a dynamic memory block that can be freed is between pointer = new int; and pointer = 0; If you move the delete pointer between those two lines, you'll be fine.
The result of the change is then:
pointer = new int;
delete pointer;
pointer = 0;
However, you might as well just remove all three of those lines and start with pointer = &currentValue; because your code never uses the dynamically allocated int.
Also, your comment "increment pointer" is incorrect. You are incrementing the target of the pointer, not the pointer.

Related

C++ Access Violation reading location 0xDDDDDDCD when I try to delete an array UPDATED

I'm working on a homework assignment. I'm trying to overload the "=" operator for an Array class I'm creating so that it will assign a newly created array with the same values as another array. This seems to work. The array is created and the data is copied over. I also check the location of the arrays first element and it is different than the original so I don't think it's trying to delete an array that's already deleted.
I've tried messing around with my destructor, but I honestly have no idea where this is coming from. If anyone has any debugging strategies that might help, I'd love to hear them as well.
Driver.cpp
int main ()
{
//Initialize
int size = 0;
char fill = '\0';
//Get info about the array
std::cout << "How long should the array be?" << std::endl;
std::cin >> size;
std::cout << "Choose fill character." << std::endl;
std::cin >> fill;
//Create array & Print array details
Array* arr = new Array(size, fill);
std::cout << "The array size is: " << arr->size() << std::endl;
std::cout << "max size: " << arr->max_size() << std::endl;
std::cout << "The contents of the array is: ";
arr->printArr();
std::cout << std::endl;
//Create new array & set it's values equal to old array
Array* arr2 = new Array();
arr2 = arr;
//= OVERLOAD TESTING
std::cout << "The array size is: " << arr2->size() << std::endl;
std::cout << "max size: " << arr2->max_size() << std::endl;
std::cout << "The contents of the array is: ";
arr2->printArr();
//Deallocate memory
delete arr;
arr = nullptr;
delete arr2;
arr2 = nullptr;
//Checking for memory leaks
_CrtDumpMemoryLeaks();
return 0;
}
Array.cpp file
//Define MAX SIZE so that it can be easily changed.
#define MAX_SIZE_ 200
#include "Array.h"
#include <iostream>
#include <stdexcept>
Array::Array (void)
:data_ (new char[MAX_SIZE_]),
cur_size_ (0),
max_size_ (MAX_SIZE_)
{ }
//Overloaded Constructor
//Assigns the initial size of the array and fills each element with the character stored in fill.
Array::Array (size_t length, char fill)
: data_ (new char[length]),
cur_size_ (length),
max_size_ (length)
{
//Fill each element with the character passed in to the function.
for(int i = 0; i < length; i++)
{
this-> data_[i] = fill;
}
std::cout << &this->data_ << std::endl;
}
//Destructor
Array::~Array (void)
{
delete[] this->data_;
this->data_ = nullptr;
}
//Sets new array equal to rhs.
const Array & Array::operator = (const Array & rhs)
{
//Set current and max size values to new array.
this->max_size_ = rhs.max_size_;
this->cur_size_ = rhs.cur_size_;
//Copy data from rhs.data_ to new array's data_
for(int i = 0; i < rhs.cur_size_; i++)
{
this->data_[i] = rhs.data_[i];
}
return *this;
}
//Print the contents of the array.
void Array::printArr(void)
{
for (int i = 0; i < (this->cur_size_) ; i++)
{
std::cout << this->data_[i];
}
}
Expected Results: The program displays information about the different arrays, then deletes them with no memory leaks.
Actual Results: The program displays all the correct data for both arrays and is able to delete the first array without a hitch, but runs into an exception when calling:
delete[] this->data_;
on the second array.
> Exception thrown at 0x5D13DB1B (ucrtbased.dll) in driver.exe: 0xC0000005: Access violation reading location 0xDDDDDDCD
Thanks for any help!
When you do arr2 = arr; you copy the pointer (memorty address) hold by arr into arr2:
Array* arr2 = new Array();
arr2 = arr;
So after that call, both arr2 and arr hold the same pointer (point to the same object). As of that delete arr2; will delete the same object you already deleted when you did delete arr; two lines before:
delete arr;
arr = nullptr;
delete arr2;
So doing delete arr2; here causes already undefine behavior. At that point, anything could happen.

Memory leak and Memory_free_on_stack variable in c++ while running in checkmarx

I am trying to remediate the vulnerabilities that is deducted by checkmarx tool. Below is the sample code which is similar to my project that I am currently working on which is throwing memory leak and memory free on stack variables.
Memory is allocated in the called function and deleted in Main function.
If sum is allocated in the main function, there won't be any Memory leak or Memory free on stack variables. But I couldn't do the same in my actual code which involves some other variables to determine the size of the pointer variables before allocating.
void addition(int *a, int *b, int** sum)
{
*sum = new int[2]; //MEMORY LEAK is thrown here
if (*sum)
{
for (int i = 0; i < 2; i++)
{
cout << "Enter two numbers: " <<endl;
cin >> a[i] >> b[i];
(*sum)[i] = a[i] + b[i];
cout << "sum:" << (*sum)[i] << endl;
}
}
}
int main()
{
int* p1 = NULL;
p1=new int[2];
int* p2 = NULL;
p2 =new int[2];
int *sum = NULL; //MemoryFree_On_StackVariable is thrown here
addition(p1 ,p2, &sum);
for (int i = 0; i < 2; i++)
{
cout << "sum is " << sum[i] << endl;
}
if (p1)
{
delete[] p1;
p1 = NULL;
cout << "p1 is deleted" <<endl;
}
if (p2)
{
delete[] p2;
p2 = NULL;
cout << "p2 is deleted" <<endl;
}
if (sum != NULL)
{
delete[] sum;
sum = NULL;
cout << " sum is deleted" << endl;
}
return 0;
}
How could I rectify the vulnerability? are those vulnerability thrown rightly?
should the memory be allocated and deleted in same function?
To be honest, I don't know why in your case you have a memory leak because everything seems correct (to me at least).
But if you call addition() several times with the same sum pointer, you will create memory leaks for sure (except if you don't forget to delete[] sum; before each additional call to addition()) because you open the door to reallocate an already allocated memory which causes a memory leak.
I think this could be a reason to allocate/deallocate memory in the same scope. But in your case, I really don't understand.
I already had done similar things (delegate the deallocation task elsewhere) and I had no problems with it.
EDIT:
I don't know the checkmarx tool you are using. But it seems that it is not able to keep tracking pointers through function calls.
This is why it says that you try to free a stack variable in the main() function and why it says you don't delete an allocated variable in the addition() function.
This explains why it is expecting allocation/deallocation in the same block.
According to me, you don't have any error, but checkmarx is not working as it should do. Perhaps you can use Valgrind for your check instead.

Random behavior with Pointer de-referencing in C++

I have the following code:
#include <iostream>
using namespace std;
int main ()
{
int myvar = 5;
int * p;
cout << "Hello2" << endl;
*p = myvar;
cout << "Hello" << endl;
cout << p << endl;
//cout << &myvar << endl;
}
I know I am not doing the right thing by not initializing the pointer. I was just playing with pointers and noticed this. The issue is when I comment out the last line, the program executes normally. But as soon as I uncomment the line, I get a segmentation fault. I don't know why printing address of myvar is causing this? Has myvar been modified in any way because of pointer dereferencing? I am using C++11.
int* p;
*p = myvar;
You are creating an uninitialized pointer and then derferencing that pointer. This has undefined behavior because p has to point to something for it to be derferenced correctly. Therefore your program's behavior can't be reasoned with.
Segmentation Fault occurs when trying to access a virtual memory address that has no read permissions.
In your case, the local variable p holds uninitialized garbage from the stack.
you are dereferencing a memory address that might not be readable(e.g no read permissions, hence the segmentation fault when trying to access it).
I'm not entirely sure the purpose of your snippet, but the following code will work, and perhaps it will help:
int myvar = 5;
int *p = nullptr;
p = &myvar;
cout << myvar << endl;
cout << &myvar << endl;
cout << p << endl;
cout << *p << endl;
(Note: I used two lines for setting 'p' because that is how you did it in your snippet. You could easily just use: int *p = &myvar; )
Anyway, there are scope issues here as p will only be valid as long as myvar is in scope; however, this does illustrate the basics of pointers. myvar and *p will return the same value (the value being pointed to), and &myvar and p will return the same value (the location of value in memory.)

double pointer reference in C++

This may a stupid question, but I'm gonna ask it anyway:
Suppose you have a pointer: Object* pointer which points at a dynamically allocated object.
class PointClass
{
Array<Object*> m_array1;
Array<Object*> m_array2;
void Delete1()
{
for (int i = 0; i < m_array1.Length; i++)
{
delete m_array1[i];
}
}
void Delete2()
{
for (int i = 0; i < m_array2.Length; i++)
{
delete m_array2[i];
}
}
}
Now, you put your pointer both in m_array1 and in m_array2.
When you try to delete the arrays, in one of them you will have a pointer which points to a deallocated space, so you can't delete it again!
I can't just assign the pointers NULL after the deletion because it wouldn't affect the pointer in the other array.
How would you solve it?
Well the simplest way would be to use a reference-counting pointer, like those available in boost::smart_ptrs.
Otherwise, you need to assign owners to the pointers - you need to decide which class will be responsible for allocating/deleting that particular pointer. If for some reason you decide that should be this class, then you could remove the duplicates from the arrays by adding all the pointers to a set before enumerating them.
If you have to share pointers in this way, something like a ref counted pointer may work well.
See this site which gives an exposé of various 'smart-pointer' techniques.
Smart Pointers
My initial answer is: Don't do that.
If you absolutely have to for some reason, you could wrap it in a smart pointer
Best solved is not passing the same pointer to both arrays. :P If you really need to, and you also need to reflect that change to all other "same" pointers, a pointer-to-pointer will do.
#include <iostream>
struct Object{};
int main(){
Object* ptr = new Object;
Object** ptrptr = &ptr;
delete *ptrptr;
*ptrptr = 0;
// both print 0
std::cout << *ptrptr << std::endl;
std::cout << ptr << std::endl;
}
On Ideone.
Another way is with a reference-to-pointer.
int main(){
Object* ptr = new Object;
Object*& refptr = ptr;
delete refptr;
refptr = 0;
// both print 0
std::cout << refptr << std::endl;
std::cout << ptr << std::endl;
}
But the second best way is probably a ref-counted smart pointer.
How would you solve it?
By not storing the same pointer in two different places. Doing this creates a duplication of data, and confuses ownership semantics. Who owns the memory pointed to by pointer? Ownership is not clear.
Under normal circumstances, dynamically allocated objects should be owned by the same module that allocated it, and only that module will have direct access to the objects or delete the memory. That's not to say other modules can't get at the data.
As others have suggested use smart pointers to solve your problem. If you have to solve it by writing your own code, I would make each of the delete function also search the "other" array to delete all pointers in the first array that can be found in the other array. And it is a last option option as this would not be my first solution to implement anything as your approach
void Delete2()
{
for (int i = 0; i < m_array2.Length; i++)
{
for (int j = 0; j < m_array1.Length; j++)
{
if (m_array2[i] == m_array1[j])
{
delete m_array1[j]
m_array1[j] = NULL;
}
delete m_array2[i];
m_array2[i] = NULL;
}
}
Then look for ways to optimise it
If I understood your question, you have the same (valid) pointer stored in 2 different arrays.
The problem is that after you delete it on array1, you can't do it again in the second array.
One way to do this is change your array definition to store the memory address of the pointer itself, instead of storing the address of the allocated memory:
const int array_size = 3;
int** m_array1[array_size];
int** m_array2[array_size];
and the rest of the code could be implemented as:
void Delete1()
{
for (int i = 0; i < array_size - 1; i++) // delete all memory but leave the last intact
{
if (*(int*)m_array1[i])
{
cout << "Delete1: erasing #" << i << " with mem addr " << std::hex << *m_array1[i] << std::dec << endl;
delete *m_array1[i];
*m_array1[i] = NULL;
}
}
}
void Delete2()
{
for (int i = 0; i < array_size; i++)
{
if (*m_array2[i])
{
cout << "Delete2: erasing #" << i << " with mem addr " << std::hex << *m_array2[i] << std::dec << endl;
delete *m_array2[i];
*m_array2[i] = NULL;
}
else
{
cout << "Delete2: !!! memory at #" << i << " was already deleted." << endl;
}
}
}
int main()
{
int* num1 = new int(10);
int* num2 = new int(20);
int* num3 = new int(30);
cout << "main: storing " << std::hex << &num1 << " which points to " << num1 << std::dec << endl;
cout << "main: storing " << std::hex << &num2 << " which points to " << num2 << std::dec << endl;
cout << "main: storing " << std::hex << &num3 << " which points to " << num3 << std::dec << endl;
m_array1[0] = &num1;
m_array1[1] = &num2;
m_array1[2] = &num3;
m_array2[0] = &num1;
m_array2[1] = &num2;
m_array2[2] = &num3;
Delete1();
Delete2();
}
Outputs:
main: storing 0xbfc3818c which points to 0x87b6008
main: storing 0xbfc38188 which points to 0x87b6018
main: storing 0xbfc38184 which points to 0x87b6028
Delete1: erasing #0 with mem addr 0x87b6008
Delete1: erasing #1 with mem addr 0x87b6018
Delete2: !!! memory at #0 was already deleted.
Delete2: !!! memory at #1 was already deleted.
Delete2: erasing #2 with mem addr 0x87b6028

Newb C++ Class Problem

I am trying to get a grasp on pointers and their awesomeness as well as a better C++ understanding. I don't know why this wont compile. Please tell me what is wrong? I'm trying to initialize the pointer when an instance of the class is created. If I try with a normal int it works fine but when I tried to set it up with a pointer i get this in the console
Running…
Constructor called
Program received signal: “EXC_BAD_ACCESS”.
sharedlibrary apply-load-rules all
Any assistance is appreciated greatly.
Here is the code
#include <iostream>
using namespace std;
class Agents
{
public:
Agents();
~Agents();
int getTenure();
void setTenure(int tenure);
private:
int * itsTenure;
};
Agents::Agents()
{
cout << "Constructor called \n";
*itsTenure = 0;
}
Agents::~Agents()
{
cout << "Destructor called \n";
}
int Agents::getTenure()
{
return *itsTenure;
}
void Agents::setTenure(int tenure)
{
*itsTenure = tenure;
}
int main()
{
Agents wilson;
cout << "This employees been here " << wilson.getTenure() << " years.\n";
wilson.setTenure(5);
cout << "My mistake they have been here " << wilson.getTenure() <<
" years. Yep the class worked with pointers.\n";
return 0;
}
You don't ever create the int that the pointer points to, so the pointer is pointer to an area of memory that doesn't exist (or is used for something else).
You can use new to get a block of memory from the heap, new returns the address of the memory location.
itsTenure = new int;
So now itsTenure holds the memory location you can dereference it to set its value.
The changed constructor is as follows:
Agents::Agents()
{
cout << "Constructor called \n";
itsTenure = new int;
*itsTenure = 0;
}
But you must also remember to delete it using delete
Agents::~Agents()
{
cout << "Destructor called \n";
delete itsTenure;
}
You are just missing a new, in the constructor.
itsTenure = new int;
You don't need to make this a pointer, however. Why are you?
You have to allocate a block of memory for your int, and only then use the address of this block of memory (the pointer). This is done with new :
cout << "Destructor called \n";
itsTenure = new int;
*itsTenure = 0;
Then you have to release the memory in the destructor with delete:
cout << "Destructor called \n";
delete itsTenur;
*itsTenure = 0 does not initialize the pointer. It writes 0 to the location that itsTenure points to. Since you never specified where itsTenure points to, that might be anywhere and the behaviour is undefined (an access violation like you're getting being the most likely result).
You need to allocate memory for *tenure in the constructor:
Agents::Agents()
{
cout << "Constructor called \n";
itsTenure = new int;
*itsTenure = 0;
}