Alex Rivera | Logout

can anyone explain why size_t type is used with an example?

Asked 2012-03-10T23:33:28.630
9

I was wondering why this size_t is used where I can use say int type. Its said that size_t is a return type of sizeof operator. What does it mean? like if I use sizeof(int) and store what its return to an int type variable, then it also works, it's not necessary to store it in a size_t type variable. I just clearly want to know the basic concept of using size_t with a clearly understandable example.Thanks

c++ c
Edit
Report

1 Answer

14

size_t is guaranteed to be able to represent the largest size possible, int is not. This means size_t is more portable.

For instance, what if int could only store up to 255 but you could allocate arrays of 5000 bytes? Clearly this wouldn't work, however with size_t it will.

answered 2012-03-10T23:41:51.970

Your Answer