Warning
If you've not lean't about using pointers for dynamic memory yet and about the heap memory then I suggest you do so!
Just make sure that you keep hold of the pointer value under array and make sure you delete it once you're finished with your data.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int size;
std::cin >> size;
int* array = newint[size];
//Do stuff with my data here
delete array;
//This frees the memory back to the system
array = NULL;
/*
This sets array to 0 instead of the position where the data used to be
(freed with delete), this is to prevent you accidentally accessing
unpredictable data sources.
*/
@giblit.. I thought it was (typename)malloc(size) for the C compatible version... Anyways you have again forgot to mention that this must return memory back to the system when it's finished with free(ptr)
I wasn't telling him how to do it I was just mentioning a way. Also your code has a memory leak line 6 should be delete[] array; Remember when you use new[] operator you must use delete[] operator.
is the same way in c also coz i using c language..
If you're writing in C, and not C++, then you'll need to use malloc and free. As SatsumaBenji said, you should learn about pointers, and about dynamic memory allocation.