int pointer in class

This is a discussion on int pointer in class within the C++ Programming forums, part of the General Programming Boards category; Hello.. Im a little confused.. What would be the proper way to allocate memory for the new struct fd_array? And ...

  1. #1
    l2u
    l2u is offline
    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,006
    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 06: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, 04:45 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 10:42 PM
  3. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 01: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

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