Some stupid questions (operator new, size_t)

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • Some stupid questions (operator new, size_t)

      I have been encountering the size_t structure for std:: strings and in numerous codes. What is it like? Which header uses it? How do I use it?

      Also, just found out that new is an operator and hence can be overloaded. But how?

      Thanks in advance.
      Act in haste and repent at leisure.
      Code too soon and debug forever.
    • RE: Some stupid questions (operator new, size_t)

      size_t is a standard type, and is defined as an unsigned integer. It is the return value of the sizeof() operator and tends to be defined all over the place.

      On overloading new - it goes like this to overload the global new:

      void *operator new(size_t size)
      {
      // put your memory allocation code here
      }

      You can also define it as a member of a class - usually a base class of some kind, to redirect memory allocations for particular data structures to custom memory pools. Games do that a lot since the default memory allocation scheme is ususally a little pokey for the high (and strange) demands of computer games.
      Mr.Mike
      Author, Programmer, Brewer, Patriot
    • From what I have read size_t is basically just an abstract type that can be used to hold a size value. It used (pre-standard) to be a signed int, but now most implimentations have it as an unsigned long. However it's exact size is undefined by the standard, as this allows it to become bigger as time goes on. It is defined to be an unsigned variable as there isn't much point in having a negative size, well at least not one that I can see. Oh yeah and it's defined in stdio.h according to "C: A Reference Manual - 5th Edition".