code_junkie

Is there any way to determine the size of a C++ array programmatically? And if not, why?

Nov 11th, 2011
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. int* arr = new int[256];
  2. printf("Size of arr: %dn", sizeof(arr));
  3.  
  4. int *arr = new int[256];
  5. int *p = &arr[100];
  6. printf("Size: %dn", sizeof(p));
  7.  
  8. int main()
  9. {
  10. std::vector<int> v(256);
  11. printf("size of v is %i capacity is %in", sizeof(int) * v.size(), sizeof(int) * v.capacity());
  12. }
  13.  
  14. const int arrSize = 256;
  15. int main()
  16. {
  17. int array[arrSize];
  18. printf("Size of array is %i", sizeof(int) * arrSize);
  19. }
  20.  
  21. string* p = new string[5];
  22. delete[5] p;
  23.  
  24. size_t* p = new size_t[10];
  25. cout << p[-1] << endl;
  26. // Or
  27. cout << p[11] << endl;
  28.  
  29. char *ar = new char[100]
Add Comment
Please, Sign In to add comment