Dynamic Allocation of Memory

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • omariqbalnaru@inbox.com

    Dynamic Allocation of Memory

    Is there anyway to allocate memory dynamically according to the input?
    For example if the user has to input his name e.g. in this case Omar
    Iqbal Naru is inputted by the user. How to allocate memory dynamically
    to store this name in a character array including the spaces?

  • Victor Bazarov

    #2
    Re: Dynamic Allocation of Memory

    omariqbalnaru@i nbox.com wrote:
    Is there anyway to allocate memory dynamically according to the input?
    For example if the user has to input his name e.g. in this case Omar
    Iqbal Naru is inputted by the user. How to allocate memory dynamically
    to store this name in a character array including the spaces?
    There are different way of doing that. One is to ask the input for the
    number of symbols available, then allocate, then read. Another is to
    use 'std::string' and 'std::getline', which will fill the string with
    the input dynamically expanding the string. Then you can query the
    string for its contents.

    What I think you should avoid using all means is dynamically allocating
    memory yourself. Use standard containers and strings as much as you can.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • Jim Langston

      #3
      Re: Dynamic Allocation of Memory

      <omariqbalnaru@ inbox.comwrote in message
      news:1164212117 .684426.108540@ j44g2000cwa.goo glegroups.com.. .
      Is there anyway to allocate memory dynamically according to the input?
      For example if the user has to input his name e.g. in this case Omar
      Iqbal Naru is inputted by the user. How to allocate memory dynamically
      to store this name in a character array including the spaces?
      Well, a few ways, depending on your input. What are you inputing to?

      A normal way would be to do:

      std::string Name;
      std::getline( std::cin, Name );

      Now, the std::string internally contains a dynamic buffer. This is actually
      doing what you are asking about in a container behind the scenes.

      Now, if for some reason you need to store this Name in a char array instead
      of a std::string, you could do this:

      std::string Name;
      std::getline( std::cin, Name );
      // Remember we need to reserve one space for the null terminator
      char* NameArray = new char[Name.length() + 1];
      strcpy( NameArray, Name.c_str() );
      // remember to delete[] the new allocated memory when done
      delete[] NameArray;

      It all depends on what you are actually trying to achieve. It sounds to me,
      however, that std::string will work for you without having to use new
      yourself.


      Comment

      • omariqbalnaru

        #4
        Re: Dynamic Allocation of Memory

        How to use the string class while reading from an ifstream?

        Jim Langston wrote:
        <omariqbalnaru@ inbox.comwrote in message
        news:1164212117 .684426.108540@ j44g2000cwa.goo glegroups.com.. .
        Is there anyway to allocate memory dynamically according to the input?
        For example if the user has to input his name e.g. in this case Omar
        Iqbal Naru is inputted by the user. How to allocate memory dynamically
        to store this name in a character array including the spaces?
        >
        Well, a few ways, depending on your input. What are you inputing to?
        >
        A normal way would be to do:
        >
        std::string Name;
        std::getline( std::cin, Name );
        >
        Now, the std::string internally contains a dynamic buffer. This is actually
        doing what you are asking about in a container behind the scenes.
        >
        Now, if for some reason you need to store this Name in a char array instead
        of a std::string, you could do this:
        >
        std::string Name;
        std::getline( std::cin, Name );
        // Remember we need to reserve one space for the null terminator
        char* NameArray = new char[Name.length() + 1];
        strcpy( NameArray, Name.c_str() );
        // remember to delete[] the new allocated memory when done
        delete[] NameArray;
        >
        It all depends on what you are actually trying to achieve. It sounds to me,
        however, that std::string will work for you without having to use new
        yourself.

        Comment

        • omariqbalnaru

          #5
          Re: Dynamic Allocation of Memory

          And please tell me that will it cater for the spaces too?

          omariqbalnaru wrote:
          How to use the string class while reading from an ifstream?
          >
          Jim Langston wrote:
          <omariqbalnaru@ inbox.comwrote in message
          news:1164212117 .684426.108540@ j44g2000cwa.goo glegroups.com.. .
          Is there anyway to allocate memory dynamically according to the input?
          For example if the user has to input his name e.g. in this case Omar
          Iqbal Naru is inputted by the user. How to allocate memory dynamically
          to store this name in a character array including the spaces?
          Well, a few ways, depending on your input. What are you inputing to?

          A normal way would be to do:

          std::string Name;
          std::getline( std::cin, Name );

          Now, the std::string internally contains a dynamic buffer. This is actually
          doing what you are asking about in a container behind the scenes.

          Now, if for some reason you need to store this Name in a char array instead
          of a std::string, you could do this:

          std::string Name;
          std::getline( std::cin, Name );
          // Remember we need to reserve one space for the null terminator
          char* NameArray = new char[Name.length() + 1];
          strcpy( NameArray, Name.c_str() );
          // remember to delete[] the new allocated memory when done
          delete[] NameArray;

          It all depends on what you are actually trying to achieve. It sounds to me,
          however, that std::string will work for you without having to use new
          yourself.

          Comment

          • Gavin Deane

            #6
            Re: Dynamic Allocation of Memory

            Please do not top-post. Thank you. Rearranged.
            omariqbalnaru wrote:
            omariqbalnaru wrote:
            Jim Langston wrote:
            <omariqbalnaru@ inbox.comwrote in message
            news:1164212117 .684426.108540@ j44g2000cwa.goo glegroups.com.. .
            Is there anyway to allocate memory dynamically according to the input?
            For example if the user has to input his name e.g. in this case Omar
            Iqbal Naru is inputted by the user. How to allocate memory dynamically
            to store this name in a character array including the spaces?
            >
            Well, a few ways, depending on your input. What are you inputing to?
            >
            A normal way would be to do:
            >
            std::string Name;
            std::getline( std::cin, Name );
            How to use the string class while reading from an ifstream?
            Exactly the same way as the code above. The std::getline function works
            with any kind of istream. std::cin is an istream and so is any ifstream
            object. So just replace std::cin with the name of your ifstream object
            in the above code and you have what you need.
            And please tell me that will it cater for the spaces too?
            It will. std::getline reads up to the line terminator which, by
            default, is \n. So if the whole line is the input stream you read from
            contains spaces, the whole line including spaces will be read.

            Gavin Deane

            Comment

            • omariqbalnaru

              #7
              Re: Dynamic Allocation of Memory

              I tried this:

              string a;

              getline(cin, a, '\n');

              a string ends up being an empty string. Please help.

              Comment

              • omariqbalnaru

                #8
                Re: Dynamic Allocation of Memory

                struct studentnode
                {
                string name;
                int rollnumber;
                studentnode *next;
                studentnode *previous;
                courselist *listofcourses;
                };

                coursenode c;

                cout<<"Enter the name of the student followed by a full stop"<<endl;
                getline(cin, s.name);
                or
                getline(cin, s.name, '\n');

                both result in s.name ending up as an empty string.

                Comment

                • BobR

                  #9
                  Re: Dynamic Allocation of Memory


                  omariqbalnaru wrote in message
                  <1164820457.429 064.19990@14g20 00cws.googlegro ups.com>...
                  >struct studentnode{
                  string name;
                  int rollnumber;
                  studentnode *next;
                  studentnode *previous;
                  courselist *listofcourses;
                  };
                  >coursenode c;
                  >cout<<"Enter the name of the student followed by a full stop"<<endl;
                  >getline(cin, s.name);
                  >or
                  >getline(cin, s.name, '\n');
                  >both result in s.name ending up as an empty string.
                  Well, duh! 's.name' does not exist! <G>
                  There is something you are not telling.

                  Try this program, (console mode):

                  // -------
                  #include <iostream>
                  #include <ostream>
                  #include <sstream>
                  #include <string>

                  struct studentnode{
                  std::string name;
                  int rollnumber;
                  studentnode *next;
                  studentnode *previous;
                  // courselist *listofcourses; // non-existant
                  studentnode(std ::string const &nm) : name(nm){}
                  };

                  int main(){
                  std::istringstr eam siscin("Billybo b Quincy Humperdinger \n");

                  std::cout<<"Ent er the name of the student followed by a full stop"
                  <<std::endl;
                  std::string name;
                  std::getline( siscin, name );

                  std::cout<<" name ="<< name <<std::endl;

                  studentnode Sn( name );
                  std::cout<<" Sn.name ="<< Sn.name <<std::endl;

                  return 0;
                  } // main()
                  // -------

                  /* - output -
                  Enter the name of the student followed by a full stop
                  name =Billybob Quincy Humperdinger
                  Sn.name =Billybob Quincy Humperdinger
                  */

                  If that works, comment-out the first line in main() (istringstream) and
                  change getline to:

                  std::getline( std::cin, name );

                  --
                  Bob R
                  POVrookie


                  Comment

                  Working...