Howto design my method?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • g.vukoman@gmail.com

    Howto design my method?

    Hi all,

    I asking me how to design my method. Should I use pointer only? Or
    references? Or Values? I don't know...

    Below is a small method to read the mem amount of a machine. Fell free
    to edit the src below

    #include "MemSysInfo .h"

    namespace SysInfo {

    unsigned int * SysInfo::MemSys Info_t::GetSize () const { // SHOULD I
    RETURN A POINTER/REFERENCE/VALUE?

    using namespace std;
    using namespace boost;

    ifstream* const inFile = new ifstream("/proc/meminfo"); // SHOULD
    string* inLine = new string; // I
    const string* const outLine = new string("\\2"); // USE
    const regex* const rgx = new regex("^(MemTot al: )(\\d+)
    ( kB)?"); // POINTER ONLY?
    unsigned int* memSize = new unsigned int(0); // WHAT WOULD
    stringstream* stringToInt = new stringstream; // YOU USE?

    if(!inFile->is_open())
    throw "can_not_open_p roc_meminfo"; // SHOULD I DELETE POINTERS
    BEFORE THROW?
    else if(inFile == 0 || inLine == 0 || outLine == 0 || rgx == 0 ||
    memSize == 0 || stringToInt == 0)
    throw "no_memory_avai lable"; // SHOULD I DELETE POINTERS BEFORE
    THROW?


    while(getline(* inFile, *inLine))
    if(regex_match( *inLine, *rgx)) {
    *stringToInt << regex_replace(* inLine, *rgx, *outLine);
    *stringToInt >*memSize;
    break;
    }

    inFile->close();

    delete inFile;
    delete inLine;
    delete outLine;
    delete rgx;
    delete stringToInt;

    return(memSize) ;
    }

    } // namespace SysInfo

    Goran

  • Barry

    #2
    Re: Howto design my method?

    g.vukoman@gmail .com wrote:
    Hi all,
    >
    I asking me how to design my method. Should I use pointer only? Or
    references? Or Values? I don't know...
    >
    Below is a small method to read the mem amount of a machine. Fell free
    to edit the src below
    >
    #include "MemSysInfo .h"
    >
    namespace SysInfo {
    >
    unsigned int * SysInfo::MemSys Info_t::GetSize () const { // SHOULD I
    RETURN A POINTER/REFERENCE/VALUE?
    >
    using namespace std;
    using namespace boost;
    >
    ifstream* const inFile = new ifstream("/proc/meminfo"); // SHOULD
    string* inLine = new string; // I
    const string* const outLine = new string("\\2"); // USE
    const regex* const rgx = new regex("^(MemTot al: )(\\d+)
    ( kB)?"); // POINTER ONLY?
    unsigned int* memSize = new unsigned int(0); // WHAT WOULD
    stringstream* stringToInt = new stringstream; // YOU USE?
    >
    if(!inFile->is_open())
    throw "can_not_open_p roc_meminfo"; // SHOULD I DELETE POINTERS
    BEFORE THROW?
    else if(inFile == 0 || inLine == 0 || outLine == 0 || rgx == 0 ||
    memSize == 0 || stringToInt == 0)
    throw "no_memory_avai lable"; // SHOULD I DELETE POINTERS BEFORE
    THROW?
    >
    >
    while(getline(* inFile, *inLine))
    if(regex_match( *inLine, *rgx)) {
    *stringToInt << regex_replace(* inLine, *rgx, *outLine);
    *stringToInt >*memSize;
    break;
    }
    >
    inFile->close();
    >
    delete inFile;
    delete inLine;
    delete outLine;
    delete rgx;
    delete stringToInt;
    >
    return(memSize) ;
    }
    >
    } // namespace SysInfo
    >
    Goran
    >
    I would say use all value in your case, have you try?

    --
    Thanks
    Barry

    Comment

    • g.vukoman@gmail.com

      #3
      Re: Howto design my method?

      On Sep 19, 2:29 pm, Barry <dhb2...@gmail. comwrote:
      g.vuko...@gmail .com wrote:
      Hi all,
      >
      I asking me how to design my method. Should I use pointer only? Or
      references? Or Values? I don't know...
      >
      Below is a small method to read the mem amount of a machine. Fell free
      to edit the src below
      >
      #include "MemSysInfo .h"
      >
      namespace SysInfo {
      >
      unsigned int * SysInfo::MemSys Info_t::GetSize () const { // SHOULD I
      RETURN A POINTER/REFERENCE/VALUE?
      >
      using namespace std;
      using namespace boost;
      >
      ifstream* const inFile = new ifstream("/proc/meminfo"); // SHOULD
      string* inLine = new string; // I
      const string* const outLine = new string("\\2"); // USE
      const regex* const rgx = new regex("^(MemTot al: )(\\d+)
      ( kB)?"); // POINTER ONLY?
      unsigned int* memSize = new unsigned int(0); // WHAT WOULD
      stringstream* stringToInt = new stringstream; // YOU USE?
      >
      if(!inFile->is_open())
      throw "can_not_open_p roc_meminfo"; // SHOULD I DELETE POINTERS
      BEFORE THROW?
      else if(inFile == 0 || inLine == 0 || outLine == 0 || rgx == 0 ||
      memSize == 0 || stringToInt == 0)
      throw "no_memory_avai lable"; // SHOULD I DELETE POINTERS BEFORE
      THROW?
      >
      while(getline(* inFile, *inLine))
      if(regex_match( *inLine, *rgx)) {
      *stringToInt << regex_replace(* inLine, *rgx, *outLine);
      *stringToInt >*memSize;
      break;
      }
      >
      inFile->close();
      >
      delete inFile;
      delete inLine;
      delete outLine;
      delete rgx;
      delete stringToInt;
      >
      return(memSize) ;
      }
      >
      } // namespace SysInfo
      >
      Goran
      >
      I would say use all value in your case, have you try?
      >
      --
      Thanks
      Barry
      Yes, I tried it all. By value, by reference and by pointer. All
      works... But why should I use values? When do I use values, pointers
      or references? Are there rules I understand?

      Goran

      Comment

      • tragomaskhalos

        #4
        Re: Howto design my method?

        On Sep 19, 1:01 pm, g.vuko...@gmail .com wrote:
        Hi all,
        >
        I asking me how to design my method. Should I use pointer only? Or
        references? Or Values? I don't know...
        >
        Below is a small method to read the mem amount of a machine. Fell free
        to edit the src below
        >
        #include "MemSysInfo .h"
        >
        namespace SysInfo {
        >
        unsigned int * SysInfo::MemSys Info_t::GetSize () const { // SHOULD I
        RETURN A POINTER/REFERENCE/VALUE?
        >
        using namespace std;
        using namespace boost;
        >
        ifstream* const inFile = new ifstream("/proc/meminfo"); // SHOULD
        string* inLine = new string; // I
        const string* const outLine = new string("\\2"); // USE
        const regex* const rgx = new regex("^(MemTot al: )(\\d+)
        ( kB)?"); // POINTER ONLY?
        unsigned int* memSize = new unsigned int(0); // WHAT WOULD
        stringstream* stringToInt = new stringstream; // YOU USE?
        >
        if(!inFile->is_open())
        throw "can_not_open_p roc_meminfo"; // SHOULD I DELETE POINTERS
        BEFORE THROW?
        else if(inFile == 0 || inLine == 0 || outLine == 0 || rgx == 0 ||
        memSize == 0 || stringToInt == 0)
        throw "no_memory_avai lable"; // SHOULD I DELETE POINTERS BEFORE
        THROW?
        >
        while(getline(* inFile, *inLine))
        if(regex_match( *inLine, *rgx)) {
        *stringToInt << regex_replace(* inLine, *rgx, *outLine);
        *stringToInt >*memSize;
        break;
        }
        >
        inFile->close();
        >
        delete inFile;
        delete inLine;
        delete outLine;
        delete rgx;
        delete stringToInt;
        >
        return(memSize) ;
        >
        }
        } // namespace SysInfo
        >
        Goran
        Wow, whatever gave you the idea to do all that
        dynamic allocation ?!

        In your example, use value for everything,
        including the return type.

        People coming from a Java background tend to
        dynamically allocate objects inappropriately ,
        but you've even done it for unsigned int.
        If you're using someone else's code as
        inspiration for this coding style, burn
        it and forget you ever saw it. Seriously.



        Comment

        • Barry

          #5
          Re: Howto design my method?

          g.vukoman@gmail .com wrote:
          On Sep 19, 2:29 pm, Barry <dhb2...@gmail. comwrote:
          >g.vuko...@gmai l.com wrote:
          >>Hi all,
          >>I asking me how to design my method. Should I use pointer only? Or
          >>references? Or Values? I don't know...
          >>Below is a small method to read the mem amount of a machine. Fell free
          >>to edit the src below
          >>#include "MemSysInfo .h"
          >>namespace SysInfo {
          >>unsigned int * SysInfo::MemSys Info_t::GetSize () const { // SHOULD I
          >>RETURN A POINTER/REFERENCE/VALUE?
          >> using namespace std;
          >> using namespace boost;
          >> ifstream* const inFile = new ifstream("/proc/meminfo"); // SHOULD
          >> string* inLine = new string; // I
          >> const string* const outLine = new string("\\2"); // USE
          >> const regex* const rgx = new regex("^(MemTot al: )(\\d+)
          >>( kB)?"); // POINTER ONLY?
          >> unsigned int* memSize = new unsigned int(0); // WHAT WOULD
          >> stringstream* stringToInt = new stringstream; // YOU USE?
          >> if(!inFile->is_open())
          >> throw "can_not_open_p roc_meminfo"; // SHOULD I DELETE POINTERS
          >>BEFORE THROW?
          >> else if(inFile == 0 || inLine == 0 || outLine == 0 || rgx == 0 ||
          >>memSize == 0 || stringToInt == 0)
          >> throw "no_memory_avai lable"; // SHOULD I DELETE POINTERS BEFORE
          >>THROW?
          >> while(getline(* inFile, *inLine))
          >> if(regex_match( *inLine, *rgx)) {
          >> *stringToInt << regex_replace(* inLine, *rgx, *outLine);
          >> *stringToInt >*memSize;
          >> break;
          >> }
          >> inFile->close();
          >> delete inFile;
          >> delete inLine;
          >> delete outLine;
          >> delete rgx;
          >> delete stringToInt;
          >> return(memSize) ;
          >>}
          >>} // namespace SysInfo
          >>Goran
          >I would say use all value in your case, have you try?
          >>
          >--
          >Thanks
          >Barry
          >
          Yes, I tried it all. By value, by reference and by pointer. All
          works... But why should I use values? When do I use values, pointers
          or references? Are there rules I understand?
          >
          Goran
          >
          I think this is a long story. You better read a textbook to have a more
          comprehensive understanding

          --
          Thanks
          Barry

          Comment

          • g.vukoman@gmail.com

            #6
            Re: Howto design my method?

            On Sep 19, 2:44 pm, tragomaskhalos <dave.du.verg.. .@logicacmg.com >
            wrote:
            On Sep 19, 1:01 pm, g.vuko...@gmail .com wrote:
            >
            >
            >
            Hi all,
            >
            I asking me how to design my method. Should I use pointer only? Or
            references? Or Values? I don't know...
            >
            Below is a small method to read the mem amount of a machine. Fell free
            to edit the src below
            >
            #include "MemSysInfo .h"
            >
            namespace SysInfo {
            >
            unsigned int * SysInfo::MemSys Info_t::GetSize () const { // SHOULD I
            RETURN A POINTER/REFERENCE/VALUE?
            >
            using namespace std;
            using namespace boost;
            >
            ifstream* const inFile = new ifstream("/proc/meminfo"); // SHOULD
            string* inLine = new string; // I
            const string* const outLine = new string("\\2"); // USE
            const regex* const rgx = new regex("^(MemTot al: )(\\d+)
            ( kB)?"); // POINTER ONLY?
            unsigned int* memSize = new unsigned int(0); // WHAT WOULD
            stringstream* stringToInt = new stringstream; // YOU USE?
            >
            if(!inFile->is_open())
            throw "can_not_open_p roc_meminfo"; // SHOULD I DELETE POINTERS
            BEFORE THROW?
            else if(inFile == 0 || inLine == 0 || outLine == 0 || rgx == 0 ||
            memSize == 0 || stringToInt == 0)
            throw "no_memory_avai lable"; // SHOULD I DELETE POINTERS BEFORE
            THROW?
            >
            while(getline(* inFile, *inLine))
            if(regex_match( *inLine, *rgx)) {
            *stringToInt << regex_replace(* inLine, *rgx, *outLine);
            *stringToInt >*memSize;
            break;
            }
            >
            inFile->close();
            >
            delete inFile;
            delete inLine;
            delete outLine;
            delete rgx;
            delete stringToInt;
            >
            return(memSize) ;
            >
            }
            } // namespace SysInfo
            >
            Goran
            >
            Wow, whatever gave you the idea to do all that
            dynamic allocation ?!
            >
            In your example, use value for everything,
            including the return type.
            >
            People coming from a Java background tend to
            dynamically allocate objects inappropriately ,
            but you've even done it for unsigned int.
            If you're using someone else's code as
            inspiration for this coding style, burn
            it and forget you ever saw it. Seriously.
            The method was just a theoretical example by me :-) I just need a
            feeling for C++ I still miss. Why should I use stack? Is it faster?
            It's easier but this is not the point. Will my program crash if I use
            to much stack? E.g. by usage of more than 640KB? Why exists references
            as pointers already exists? Questions, questions, questions... :-)

            Thanks

            Goran

            Comment

            • tragomaskhalos

              #7
              Re: Howto design my method?

              On Sep 19, 1:58 pm, g.vuko...@gmail .com wrote:
              >
              The method was just a theoretical example by me :-) I just need a
              feeling for C++ I still miss. Why should I use stack? Is it faster?
              It's easier but this is not the point. Will my program crash if I use
              to much stack? E.g. by usage of more than 640KB? Why exists references
              as pointers already exists? Questions, questions, questions... :-)
              >
              Thanks
              >
              - Short answer: I wouldn't worry about running out of stack on
              modern architectures (unless you are doing some sort of embedded
              work). If you have a large multidimensiona l array on the stack
              you could run into problems I suppose, but this is best managed
              via a vector anyway, which will by default manage its memory
              dynamically behind the scenes.

              - References solve some problems that are awkward or ugly with
              pointers, but often the choice of which to use is as much based
              on idiom as technical consideration.

              - Pointers are useful for hanging on to objects which need
              arbitrary lifetimes and that are too expensive to copy (or should
              not be copied for semantic reasons).

              But as Barry sagely points out, this is really too broad a
              subject for a pat answer in an n.g.. Good C++ textbooks will
              not only explain this stuff far better than I can but will have
              copious examples of good idiomatic code which will give you
              a feel for the right way to do things.


              Comment

              • g.vukoman@gmail.com

                #8
                Re: Howto design my method?

                On Sep 19, 3:27 pm, tragomaskhalos <dave.du.verg.. .@logicacmg.com >
                wrote:
                On Sep 19, 1:58 pm, g.vuko...@gmail .com wrote:
                >
                >
                >
                The method was just a theoretical example by me :-) I just need a
                feeling for C++ I still miss. Why should I use stack? Is it faster?
                It's easier but this is not the point. Will my program crash if I use
                to much stack? E.g. by usage of more than 640KB? Why exists references
                as pointers already exists? Questions, questions, questions... :-)
                >
                Thanks
                >
                - Short answer: I wouldn't worry about running out of stack on
                modern architectures (unless you are doing some sort of embedded
                work). If you have a large multidimensiona l array on the stack
                you could run into problems I suppose, but this is best managed
                via a vector anyway, which will by default manage its memory
                dynamically behind the scenes.
                >
                - References solve some problems that are awkward or ugly with
                pointers, but often the choice of which to use is as much based
                on idiom as technical consideration.
                >
                - Pointers are useful for hanging on to objects which need
                arbitrary lifetimes and that are too expensive to copy (or should
                not be copied for semantic reasons).
                >
                But as Barry sagely points out, this is really too broad a
                subject for a pat answer in an n.g.. Good C++ textbooks will
                not only explain this stuff far better than I can but will have
                copious examples of good idiomatic code which will give you
                a feel for the right way to do things.
                Thanks to all! Looking out for good C++ books :-)

                Goran

                Comment

                • Jerry Coffin

                  #9
                  Re: Howto design my method?

                  In article <1190206729.132 372.69280@y42g2 000hsy.googlegr oups.com>,
                  g.vukoman@gmail .com says...

                  [ ... ]
                  The method was just a theoretical example by me :-) I just need a
                  feeling for C++ I still miss. Why should I use stack? Is it faster?
                  It's easier but this is not the point. Will my program crash if I use
                  to much stack? E.g. by usage of more than 640KB? Why exists references
                  as pointers already exists? Questions, questions, questions... :-)
                  You should use the stack because it keeps your code simple, exception
                  safe. As a bonus, allocating stack space is frequently faster than
                  allocating from the free store.

                  Depending on your system, the stack may easily have a limit, but you can
                  usually increase it if needed (though needing to do so is quite rare).

                  References are different from pointers, and provide some capabilities
                  that would be difficult to simulate with pointers. An obvious instance
                  is when you're doing operator overloading:

                  x >y;

                  writes to y, but you don't want to have to explicitly take its address,
                  which would be necessary if you were using a pointer instead.

                  --
                  Later,
                  Jerry.

                  The universe is a figment of its own imagination.

                  Comment

                  Working...