Thread: int pointer in class

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    int pointer in class

    Hello..
    Im a little confused.. What would be the proper way to allocate memory for the new struct fd_array? And how should I check if its already allocate and relocate it? Should I use sockset *ns = new sockset.. ?

    Thanks a lot

    Code:
    struct sockset {
    	unsigned int fd_count;
    	SOCKET *fd_array;
    };

  2. #2
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    memory allocation in C++ is performed via the new operator. It
    returns NULL if it fails. General form:

    Code:
    datatype *pointer;
    
    if ((pointer = new datatype) == NULL)
        cout << "Alocation failed";
    for arrays, it would be new datatype [size]

    delete is used to free the memory, or []delete if an array was allocated to the pointer (no need to specify the size inside of [])

    Code:
    delete pointer;
    There is no realloc equivalent in C++, so if you need to resize,
    allocate new memory, copy the stuff from the old memory and
    delete the old pointer.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Richie T
    memory allocation in C++ is performed via the new operator. It
    returns NULL if it fails.
    http://parashift.com/c++-faq-lite/fr....html#faq-16.6
    [edit=pokey googling]
    Quote Originally Posted by l2u
    And how should I check if its already allocate and relocate it?
    http://www.research.att.com/~bs/bs_faq2.html#renew
    Last edited by Dave_Sinkula; 05-18-2006 at 05:32 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Thats so weird - i just came across this in the tips section and was
    coming back to correct it!
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 12:05 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM