Last active
August 29, 2015 14:21
-
-
Save azz/7d6866b69ffe6713874d to your computer and use it in GitHub Desktop.
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> | |
using namespace std; | |
int main() { | |
int sz; | |
cout << "enter size: "; | |
cin >> sz; | |
int* x = new int[sz]; | |
cout << "allocated " << (sz * sizeof(int)) << " bytes at " << x << endl; | |
delete [] x; | |
return 0; | |
} |
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> | |
using namespace std; | |
int main() { | |
int sx, sy, sz; | |
cout << "enter size (x y z): "; | |
cin >> sx >> sy >> sz; | |
int*** x = new int**[sx]; | |
for (int i = 0; i < sx; ++i) { | |
x[i] = new int*[sy]; | |
for (int j = 0; j < sy; ++j) { | |
x[i][j] = new int[sz]; | |
} | |
} | |
cout << "allocated " << (sx*sy*sz * sizeof(int)) << " bytes (not including pointers)"; | |
for (int i = 0; i < sx; ++i) { | |
for (int j = 0; j < sy; ++j) { | |
delete [] x[i][j]; | |
} | |
delete [] x[i]; | |
} | |
delete [] x; | |
cout << "de-allocated x" << endl; | |
return 0; | |
} |
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> | |
#include <assert.h> | |
#define SX 8 | |
#define SY 20 | |
using namespace std; | |
int main() { | |
int sz; | |
cout << "enter size: "; | |
cin >> sz; | |
assert(sz > 0); | |
int* x[SX][SY]; // On the stack | |
for (int i = 0; i < SX; ++i) | |
for (int j = 0; j < SY; ++j) | |
x[i][j] = new int[sz]; // In the heap | |
x[2][4][0] = 5; // etc. | |
//^ ^ ^--- This dimension is dynamically allocated | |
//| +-------This dimeision is static, ie. on the stack | |
//+----------This dimension is also static (no need for de-allocation) | |
// free | |
for (int i = 0; i < SX; ++i) | |
for (int j = 0; j < SY; ++j) | |
delete [] x[i][j]; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment