Why does this simple dynamic array experiment crash?
-
Any ideas why this code crashes with 799 elements or more but it works with 699 or less? I haven't found yet the limit.
#include #include int main() { /*1000 elements for the array*/ int n = 699; /* Dynamic array */ int* array = (int*) malloc(sizeof(char) * n); if (!array) { printf("Out of memory"); return 1; } for (int i = 0; i < n; i++) { array[i] = i; } for (int i = 0; i < n; i++) { printf("%d, ", array[i]); } return 0; }
-- modified at 2:51 Thursday 30th March, 2006
-
Any ideas why this code crashes with 799 elements or more but it works with 699 or less? I haven't found yet the limit.
#include #include int main() { /*1000 elements for the array*/ int n = 699; /* Dynamic array */ int* array = (int*) malloc(sizeof(char) * n); if (!array) { printf("Out of memory"); return 1; } for (int i = 0; i < n; i++) { array[i] = i; } for (int i = 0; i < n; i++) { printf("%d, ", array[i]); } return 0; }
-- modified at 2:51 Thursday 30th March, 2006
Hey when i tried it is working up to n=980. anything more than 980 application is crashing.
-
Hey when i tried it is working up to n=980. anything more than 980 application is crashing.
-
Any ideas why this code crashes with 799 elements or more but it works with 699 or less? I haven't found yet the limit.
#include #include int main() { /*1000 elements for the array*/ int n = 699; /* Dynamic array */ int* array = (int*) malloc(sizeof(char) * n); if (!array) { printf("Out of memory"); return 1; } for (int i = 0; i < n; i++) { array[i] = i; } for (int i = 0; i < n; i++) { printf("%d, ", array[i]); } return 0; }
-- modified at 2:51 Thursday 30th March, 2006
Your array variable is a pointer to int's but you're only allocating enough memory for n char's. char's are smaller than int's so you're running past the end of the memory you've allocated.
0 bottles of beer on the wall, 0 bottles of beer, you take 1 down, pass it around, 4294967295 bottles of beer on the wall. Awasu 2.2 [^]: A free RSS/Atom feed reader with support for Code Project.
-
Your array variable is a pointer to int's but you're only allocating enough memory for n char's. char's are smaller than int's so you're running past the end of the memory you've allocated.
0 bottles of beer on the wall, 0 bottles of beer, you take 1 down, pass it around, 4294967295 bottles of beer on the wall. Awasu 2.2 [^]: A free RSS/Atom feed reader with support for Code Project.
-
Your array variable is a pointer to int's but you're only allocating enough memory for n char's. char's are smaller than int's so you're running past the end of the memory you've allocated.
0 bottles of beer on the wall, 0 bottles of beer, you take 1 down, pass it around, 4294967295 bottles of beer on the wall. Awasu 2.2 [^]: A free RSS/Atom feed reader with support for Code Project.
-
Cristoff wrote:
orry for the stupid question!
Hey, we've all done it. Just be grateful you did here instead of when iterating through thousands of bank account balances :-)
0 bottles of beer on the wall, 0 bottles of beer, you take 1 down, pass it around, 4294967295 bottles of beer on the wall. Awasu 2.2 [^]: A free RSS/Atom feed reader with support for Code Project.