strange behavior of placement new [duplicate] - c++

This question already has answers here:
Why doesn't my program crash when I write past the end of an array?
(9 answers)
Closed 9 years ago.
class A
{
public:
static void * operator new (size_t,void *p)
{
return p;
}
int i;
};
int main()
{
void *p = malloc(sizeof(A));
cout<<p<<endl;
A *a= new (p) A;
a->i = 10;
cout<<a<<endl;
cout<<a->i<<endl;
a->i = 100;
cout<<a->i<<endl;
}
output:
0x1e0e010
0x1e0e010
10
100
But I change the code of operator new to
static void * operator new (size_t,void *p)
{
return p+1024;
}
it doesn't crash and its output is:
0x25c4010
0x25c4410
10
100
I am using ubuntu13.10 and gcc4.8.1
Thanks

The short answer: Undefined behavior is undefined.

Related

Bypasses initialisation in goto statement [duplicate]

This question already has answers here:
Goto before variable initialization causes compiler error
(4 answers)
initialization of 'unused' is skipped by 'goto label' - why do I get it for std::string but not for int?
(2 answers)
Closed 12 days ago.
I've come across an error that I thought should have been a warning:
char* p;
int main()
{
if (p) goto continue_func;
int a = 3;
continue_func:
int b = 2;
}
The error I get:
initialization of 'a' is skipped by 'goto continue_func'
transfer of control bypasses initialization of:
I don't see why this is illegal. If the case had been something like this:
char* p;
int main()
{
if (p) goto continue_func;
int a = 3;
continue_func:
int b = 2;
int c = a + b;
std::cout << c;
}
Then I understand that a is being used. But in the first example it's not. Is this truly illegal C++, and why?

How to return nothing from an integer function in C++? [duplicate]

This question already has answers here:
In a non-void function I want to return nothing
(2 answers)
When and how should I use exception handling?
(7 answers)
Closed 6 months ago.
Consider the following code:
#include <iostream>
int test(int a){
if (a > 10){
return a;
}
else{
std::cout << "Error!";
return nothing;
}
}
int main(){
std::cout << test(9);
return 0;
}
What I want is that The integer function test(int a), return a if a > 10, otherwise return Error!. but since this is an integer function, it must return an integer value, but I want that it print Error and return nothing. Is there a way for do this? (Also note that I don't want to use a void function)
#include <stdexcept>
int test(int a){
if (a > 10){
return a;
}
else{
throw std::invalid_argument( "a is smaller or eq than 10" );
}
}

Valgrind reporting "brk segment overflow in thread #1" [duplicate]

This question already has answers here:
Valgrind reporting a segment overflow
(5 answers)
Closed 6 years ago.
I wonder what this message implies:
==18151== brk segment overflow in thread #1: can't grow to 0x4a26000
Note that the code runs just fine and the output is correct. Should I just ignore this message? And what does it mean?
I think you can ignore it. I got the message in a new allocation in some code that seemed to work perfectly and I also get the message it in the following code:
#include <vector>
struct Something
{
Something() : a1(0), b1(0) { }
unsigned short a1;
unsigned short b1;
};
const int allocsize = 10000;
struct Tester
{
Tester()
{
for (int u = 0; u < allocsize; ++u)
data.push_back(new Something[519]);
}
~Tester()
{
for (int u = 0; u < allocsize; ++u)
delete[] (data[u]);
}
std::vector<Something*> data;
};
void test()
{
Tester t;
// while (true) {;}
}
int main()
{
test();
return 0;
}
I also noticed that others experience the same issue:
Valgrind reporting a segment overflow

Why the dangling pointer is giving the size of the previously pointed variable? [duplicate]

This question already has answers here:
Can a local variable's memory be accessed outside its scope?
(20 answers)
Closed 7 years ago.
class B
{
public:
B():a(0), b(0) { }
B(int x):a(x), b(0) { }
private:
int a;
int b;
};
class A
{
public:
A(B* ptr):pB(ptr) { }
void modifypB()
{
delete pB;
pB = NULL;
}
void printBSize()
{
if( pB != NULL )
cout<<"pB pointing to Obj size:"<<sizeof(*pB)<<endl;
else
cout<<"pB pointing to Obj size:"<<sizeof(*pB)<<endl;
}
private:
B *pB;
};
void main()
{
B *bObj = new B(10);
cout<<"Size of bObj:"<<sizeof(*bObj)<<endl;
A aObj(bObj);
cout<<"Size of aObj:"<<sizeof(aObj)<<endl;
cout<<"Before De-allocating: ";
aObj.printBSize();
aObj.modifypB();
cout<<"After De-allocating: ";
aObj.printBSize();
}
Output:
Size of bObj: 8
Size of aObj: 4
Before De-allocating: pB pointing to Obj size: 8
After De-allocating: pB pointing to Obj size: 8
Why the size of *pB is 8, even after de-allocation ?
Why the size of *pB is 8, even after de-allocation ?
sizeof(*pB) is evaluated at compile time based on the type of *pB. Its value does not depend on the value of pB at run time.
You are printing sizeof(B) in both branches of your if statement, which is 8 on your platform.
sizeof(*p) gives you the size of the object which the compiler has determined at compile-time and not at runtime.
Try this:
int main(void)
{
std::string *string_pointer = 0;
std::cout << sizeof(*string_pointer) << std::endl;
return 0;
}
This will not segfault but print the size of the std::string-class.

memcmp strangely fails returning array [duplicate]

This question already has answers here:
Can a local variable's memory be accessed outside its scope?
(20 answers)
Closed 9 years ago.
I have C++ code that looks like this:
static int* ArrayGenerator()
{
int temp[1] = {9};
return temp;
}
static int* ArrayGenerator(int i)
{
//parameter is just for demonstration
int temp[1] = {9};
return temp;
}
int _tmain(int argc, _TCHAR* argv[])
{
int arr1[1] = {9};
printf("arrays are %s equal\n\n", (memcmp(arr1, ArrayGenerator(), 1) == 0) ? "" : "not");
printf("arrays are %s equal\n\n", (memcmp(arr1, ArrayGenerator(1), 1) == 0) ? "" : "not");
}
The first gives me 'are equal' the second gives me 'are not equal'.
Why is this?
You cannot return local pointers from a function. When you return temp from the functions it passes out of scope and the memory is no longer valid. This causes undefined behavior. I already explained this here