problem with new operator

This is a discussion on problem with new operator within the C++ Programming forums, part of the General Programming Boards category; i got a problem usin new operator when allocating desired number of rooms in a hotel manaement system cin>>totrooms; nofroom=new ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    17

    problem with new operator

    i got a problem usin new operator when allocating desired number of rooms in a hotel manaement system

    cin>>totrooms;
    nofroom=new int[totrooms];
    file.write((char*).......
    please help me.
    codeffects software solutions

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    There isnt enough info here to help with anything....post more code

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well I don't know where you are stuck but are you doing something like this:

    Code:
    class myClass {
        void *operator new (size_t size) {
            return malloc(size*sizeof(myClass));
        }
    };

  4. #4
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    What kind of problems are you having??
    error, you are not getting the result you want or what?

  5. #5
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378
    allocating desired number of rooms in a hotel manaement system
    U R trying to allocate memory for your # of rooms.
    An example might be:
    Code:
    #include <malloc.h>
    #include <memory.h>
    
    class Blanks
    {
    public:
        Blanks(){}
        void *operator new( size_t stAllocateBlock, char chInit );
    };
    void *Blanks::operator new( size_t stAllocateBlock, char chInit )
    {
        void *pvTemp = malloc( stAllocateBlock );
        if( pvTemp != 0 )
            memset( pvTemp, chInit, stAllocateBlock );
        return pvTemp;
    }
    For discrete objects of type Blanks, the global operator new function is hidden. Therefore, the following code allocates an object of type Blanks and initializes it to 0xa5:
    Code:
    int main()
    {
        Blanks *a5 = new( 0xa5 ) Blanks;
    
        return a5 != 0;
    }
    The argument supplied in parentheses to new is passed to Blanks::operator new as the chInit argument. However, the global operator new function is hidden, causing code such as the following to generate an error:

    Blanks *SomeBlanks = new Blanks;

    Pls let me know if this helps!
    ;)

    Disable Smilies in This Post by Salem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. two dot operator problem HELP!
    By Amyaayaa in forum C++ Programming
    Replies: 3
    Last Post: 03-13-2008, 11:45 AM
  2. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 08:38 AM
  3. Serializing problem.. (can't use >> operator)
    By RancidWannaRiot in forum Windows Programming
    Replies: 2
    Last Post: 10-29-2005, 12:10 PM
  4. evaluating unary negation operator in an infix expression??
    By YevGenius in forum C++ Programming
    Replies: 7
    Last Post: 05-12-2004, 02:18 PM
  5. Weird Problem with return with operator +
    By KonArtis in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2004, 04:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21