Global functions & variables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shatemo5
    New Member
    • Dec 2007
    • 1

    #1

    Global functions & variables

    Hi,
    I'm new in C++ programimng :(
    & I have a couple of questions to ask about :-

    1- I have to build 5 clases & in each one I have to overide the opretaros new & delete ...so in each time I call the "new operator" it should print in a file how many bytes my object size is & the total size that I have righty now from this object in my program ... So I think that I have to have 5 fields of int for ech class so I could save the total bytes that in my program right now from this object & could increase to this variable & decrease acoordinglly whether I call new/delete opretaor,,,

    My question is,,in which type should these fields be,,static int/static const int ?!?!
    should ech field be inside it's class ? or should I make these fields global !! which mean to build an incapsulation class to all my fife clases ,,So these classes are a member in the new one & additional to them I have my 5 fields so in ech time I update the appropriate field ???


    2- I have been asked to write a global functions,,how can I do it ??


    thanks a lot
    have a nice day
  • cypherzero
    New Member
    • Jul 2007
    • 15

    #2
    When you overload the new operator in a class you are given the size of the class then. You could also use sizeof(x).

    The most basic overloading would look like this:

    Code:
    struct MyStruct
    {
    
    public:
        void operator new( size_t sizetoallocate)
        {
        return malloc(sizetoallocate);
        }
        void operator delete ( void* lpvoid)
        {
        free(lpvoid);
        }
    };
    More detail can be found on MSDN ( http://msdn2.microsoft .com/en-us/library/t48aek43(VS.71) .aspx ).

    Static is similar to global - there is only one value that all the objects of a class access. If 'running_total_ of_memory' were non-static each class would have a different 'running_total_ of_memory', which wouldn't make any sense. Alternativley 'running_total_ of_memory' could be global.

    As for global functions, that just means not in a class. int main() is usually a global function.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Originally posted by cypherzero
      struct MyStruct
      {

      public:
      void operator new( size_t sizetoallocate)
      {
      return malloc(sizetoal locate);
      }
      void operator delete ( void* lpvoid)
      {
      free(lpvoid);
      }
      };
      This doesn't do anything. The new operator calls malloc before calling constructors and delete calls the destructors before calling free. That makes this code redundant.

      Don't confuse the new operator with operator new. The new operator calls operator new. An operator new overload should be for a separate memory manager for your class. Like maybe you are using a private heap.

      Comment

      Working...