Help designing data structure

This is a discussion on Help designing data structure within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by kotoko Knowing this, do smart pointers make more sense? (Yes a database makes even more but I ...

  1. #16
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,648
    Quote Originally Posted by kotoko View Post
    Knowing this, do smart pointers make more sense? (Yes a database makes even more but I want to put that aside for now)
    It depends. If the actual object is guaranteed not to go out of scope*, a raw pointer is sufficient. Otherwise, use a smart pointer; managing memory directly (via new/delete) is *never* acceptable- violating RAII is the surest path to undefined behavior! Anyway, I'd recommend using std::tr1::shared_ptr or the like.

    *That can be trickier than it sounds. For example, if you store your objects in an std::vector, a pointer to one of it's elements could become invalidated once another object is added to the vector. On the other hand, an std::list would work fine. Point is, choose your data structure carefully, and be sure to RTFM.
    Code:
    #include <ip.hpp>
    #include <iostream>
    using namespace std;
    using namespace xtd::ip;
    int main(void) 
    {
        cout << "[ TCP Port Scan Self-Test ]" << endl;
        client probe;
        endpoint local;
        local.address = "127.0.0.1";
        local.protocol = IPPROTO_TCP;
        for(local.port = 0; local.port < (1 << 16); ++local.port)
        {
            if(probe.open(local))
                cout << "Listening: ";
            else
                cout << "No Response: ";
            cout << local.port << endl;
        }    
    }

  2. #17
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by kotoko View Post
    @tabstop I think we might be having what we call in Portuguese a deaf men conversation. What I'm saying is I do not want to duplicate. What I understand from your suggestion is that I will have to duplicate. What I'm asking is is this really what you are suggesting (to have duplicate).
    I have said, and will continue to say, exactly the opposite. My suggestions have been built around not duplicating any objects. (Or, I suppose, at least are consistent with them -- you may have to consistently take the pointer option when a choice was presented.)
    Last edited by tabstop; 12-30-2010 at 06:54 PM.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Packet Container Class
    By ChaoticXSinZ in forum C++ Programming
    Replies: 2
    Last Post: 11-01-2010, 12:07 AM
  2. pthread question how would I init this data structure?
    By mr_coffee in forum C Programming
    Replies: 2
    Last Post: 02-23-2009, 11:42 AM
  3. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  4. C diamonds and perls :)
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 05-16-2003, 10:19 PM
  5. Dynamic Data Structure -- Which one is better?
    By Yin in forum C++ Programming
    Replies: 0
    Last Post: 04-10-2002, 11:38 PM

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