What does the operator delete[] do? [duplicate] - c++

This question already has answers here:
delete vs delete[] operators in C++
(7 answers)
Closed 5 years ago.
#include <iostream>
using namespace std;
class A{
public:
int s;
// ~A(){}
};
int main(){
A *c = new A[10];
delete c;
return 0;
}
Code above can run successfully, but when i code will get an error. Who can tell me why?

The behaviour of your code is undefined.
You must write delete[] c; if c is a pointer to memory allocated with new[].
(Interestingly, some compilers sort out this mess for your, but don't rely on that since then you're not writing portable C++).

Related

Why is *a++; not incrementing the value a points to? [duplicate]

This question already has answers here:
Does *p++ increment after dereferencing? [duplicate]
(5 answers)
Closed 6 months ago.
Hellow
I am a beginner in coding who is learning pointer. this is a simple code of passing pointers
to functions but I am getting wrong input.
#include<iostream>
using namespace std;
void increment(int *a){
*a++;
}
int main(){
int a=2;
//int *p=&a;
increment(&a);
cout<<a;
return 0;
}
output is showing 2;
What happened here? The C++ grammar effectively defines something we commonly refer to as "operator precedence" (since it's defined by the grammar, you won't find chapter in the C++ standard where the precedence would be listed in a convenient way, but there are websites that worked it out).
In *a++;, the pointer will be increased (a++) and thus pointing to an invalid position. After that, the invalid pointer will be dereferenced (* operator), causing undefined behavior (UB). If you're learning, you want to get familiar with undefined behavior, because anything can happen.
To fix it, use parentheses to specify precedence explicity: (*a)++;.
Maybe you want to learn about references instead? Don't use raw pointers and pointer arithmetic. Compare your code to this code:
#include<iostream>
using namespace std;
void increment(int& a){
a++;
}
int main(){
int a = 2;
increment(a);
cout << a;
return 0;
}
Also: Why is "using namespace std;" considered bad practice?

There is something strange with the c++ "delete" command [duplicate]

This question already has answers here:
Pointers in c++ after delete
(3 answers)
Closed 1 year ago.
If deleting the pointer, why is the output 5 5?(I'm new)
#include <iostream>
using namespace std;
int main(){
int* i = new int(5);
cout<< *i <<endl;
delete i;
cout<< *i <<endl;
return 0;
}
Delete doesn't set 0 or any value to the memory i is pointing to. It just flags it as free so something can use it later. This leads to undefined behaviour

where does struct object get memory in main function? [duplicate]

This question already has answers here:
Are data members allocated in the same memory space as their objects in C++?
(6 answers)
Closed 4 years ago.
how does tmp get memory from the machine, from heap or stack?
I thought it was from the stack, but it seem that the code can run properly
#include<bits/stdc++.h>
using namespace std;
struct node {
int a[1000000];
};
int main() {
node tmp;
memset(tmp.a, -1, sizeof(tmp.a));
cout << tmp.a[0];
return 0;
}
In stack, since it's an automatic variable to the main function.
PS: This code doesn't compile, for example with this error: error: type 'node' does not provide a subscript operator: cout << tmp[0];.

how many memory leak we have here? [duplicate]

This question already has answers here:
What is The Rule of Three?
(8 answers)
Closed 6 years ago.
i couldn't find my answer with googling so im asking here :
Assume that we have code like this and we don't want to overload the copy constructor
#ifndef ARRAY_H
#define ARRAY_H
class Array {
public:
Array(int = 10);
int* ptr;
int size;
}
#endif // ARRAY_H
and we have:
#include <iostream>
#include "Array.h"
using namespace std;
Array::Array(int A)
{
size = (A > 0) ? A : 10;
ptr = new int[size];
for(int i=0; i<size; i++)
{ ptr[i] = i;}
}
and main() is :
Array newArray1(10);
Array newArray2(8);
newArray2 = newArray1;
now our professor said we have dangle problem here because we have same address for both
newArray1 and newArray2
my question is if we delete newArray1 memory : have we memory leak except dangle??? what happened to newArray2's ptr's memory which ptr was pointing to it before getting newArray1 address??? is this part of memory exist now ??? and have we other memory leaks problem ???
To answer your question: You have no destructor delete[]ing the memory you allocate to ptr in the constructor. So yes, you have a leak.
A much better option would be to simply use a std::unique_ptr rather than manual memory management.
Also, why are you not using the constructors initialization list but instead assigning to members in the constructor body?

Error when declaring local array [duplicate]

This question already has answers here:
Getting a stack overflow exception when declaring a large array
(8 answers)
Closed 8 years ago.
Something like this throws an error:
using namespace std;
int main()
{
int test[1000000] = {};
}
Something like this doesn't:
using namespace std;
int test[1000000] = {};
int main()
{
}
Why is that? A million ints isn't even too memory-demanding.
The first one allocates space on the stack. The second one allocates space in the data segment at compile/link time. The stack is of limited size.
Stack is not dynamic, but you can also do this
int* arr = new int[1000000];
but don't forget to delete it because this declares array in the heap which is dynamic memory and by deleting it from heap you prevent the memory leak.
Example :
delete arr;
This is just alternative how to use memory