Created
September 28, 2012 22:41
-
-
Save travisperson/3802439 to your computer and use it in GitHub Desktop.
Realloc in C++ with new
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
class Test { | |
public: | |
int a,b,c; | |
Test() { a=b=c=10; } | |
}; | |
int main() { | |
Test *t; | |
t = new Test; | |
std::cout << "t = new Text\n"; | |
std::cout << "t = " << t << "\n"; | |
t = new(t) Test[5]; | |
std::cout << "t.a = 5\n"; | |
std::cout << "t.c = 5\n"; | |
t->a = 5; | |
t->c = 5; | |
std::cout << "t = new(t) Text[5]\n"; | |
std::cout << "t = " << t << "\n"; | |
std::cout << "for ( int i = 0; i < 5; i++ )\n"; | |
for ( int i = 0; i < 5; i++ ) | |
{ | |
std::cout << " &t["<<i<<"] = "<<&t[i]<<"\n"; | |
} | |
std::cout << "t->a = "<<t->a<<"\n"; | |
std::cout << "t->b = "<<t->b<<"\n"; | |
std::cout << "t->c = "<<t->c<<"\n"; | |
return 0; | |
} | |
/* | |
t = new Text | |
t = 0x134d010 | |
t.a = 5 | |
t.c = 5 | |
t = new(t) Text[5] | |
t = 0x134d010 | |
for ( int i = 0; i < 5; i++ ) | |
&t[0] = 0x134d010 | |
&t[1] = 0x134d01c | |
&t[2] = 0x134d028 | |
&t[3] = 0x134d034 | |
&t[4] = 0x134d040 | |
t->a = 5 | |
t->b = 10 | |
t->c = 5 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment