A few questions

This is a discussion on A few questions within the C++ Programming forums, part of the General Programming Boards category; Q1: Code: string s; cin >> s; how can I include all the input words? f.ex if I write hello ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Location
    oslo
    Posts
    26

    A few questions

    Q1:
    Code:
    string s;
    cin >> s;
    how can I include all the input words?
    f.ex if I write hello world and try to cout << s; only "hello" will be couted.

    Is there some standard string methods for this, or do I need to use chars? I dont really want to
    reinvent the wheel if I dont have to.



    Q2:
    How can I delete a char array like this: char a[2]; ?
    This is what I tried:

    Code:
    char a[2];
    a[0] = 'a';
    a[1] = 'b';
    a[2] = '\0';
        
    char *b;
    b = a;
    cout << b << endl; // just to check the value to be displayed before we try to delete it
    delete[] b;
    cout << "trying to delete b, b is now:" << endl;
    cout << b << endl; // it still has the same value, why? whats wrong?

    Hope anyone can help!
    Dag
    life is too short smart but hard !

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,205
    A1: Use std::getline(std::cin, yourString);
    A2: You only delete memory that has been allocated with new, example:
    Code:
    char* temp = new char[15];
    // do stuff...
    delete[] temp;

  3. #3
    Registered User
    Join Date
    Feb 2005
    Location
    oslo
    Posts
    26
    great
    thank you!
    life is too short smart but hard !

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Code:
    cout << b << endl; // it still has the same value, why? whats wrong?
    Another problem with this bit of code is that, thanks to the char* overload of <<, it tries to dereference the pointer. Now, if what b pointed to had been dynamically allocated in the first place, it would have been freed, and dereferencing the pointer would probably have resulted in an access violation. Some day in the future, when you least expect it.
    Null out invalid pointers and don't access them.

    That said, deleting a pointer that was not dynamically allocated just might crash the program, too.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,805
    1. Use getline is very nice.
    Code:
    #include <iostream>
    #include <string>
    int main(void)
    {
      std::string myString;
      std::getline(cin,myString);  //saying get input from cin and save it in string
      return 0;
    }
    2. You are not allocating memory for b your setting it to a so your delete is not really doing anything. What you are doing is not what delete is meant for, it is meant for things like this
    Code:
    #include <iostream>
    
    int main()
    {
      int *ptr;
      ptr = new int;  //allocating memory for ptr
      *ptr = 10;       //setting the value of ptr to 10
      std::cout<<*ptr<<std::endl;
      delete ptr;
      return 0;
    }
    Woop?

  6. #6
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,805
    How exactly does delete work? I was playing with it a little and it seems that you can still access the memory that was allocated. I know this isn't smart and you should set your pointer to NULL after a delete but I still wonder.
    Woop?

  7. #7
    Registered User
    Join Date
    Feb 2005
    Location
    oslo
    Posts
    26
    2. You are not allocating memory for b your setting it to a so your delete is not really doing anything. What you are doing is not what delete is meant for, it is meant for things like this


    Code:
    #include <iostream>
    
    int main()
    {
      int *ptr;
      ptr = new int;  //allocating memory for ptr
      *ptr = 10;       //setting the value of ptr to 10
      std::cout<<*ptr<<std::endl;
      delete ptr;
      return 0;
    }
    well, it was a I was suppose to delete... since I have a char array that stores 2 elements, ex

    Code:
    char a[2];
    a[0] = 'a';
    a[1] = 'b';
    now that those are set, I want to remove the values in them so I can reset them via other functions in my program..

    when I changed my test code, adding allocation with new as Shakti were saying, it worked well. This is what I used:

    Code:
        char *a = new char[2];
        a[0] = 'a';
        a[1] = 'b';
        a[2] = '\0';
        
        char *b;
        b = a;
        cout << b << endl;
        delete[] b;
        cout << "trying to delete b, b is now:" << endl;
        cout << b << endl;
    About the part that I should leave pointers to the allocated object alone, instead doing stuff with the "original" I dont really understand... but thats me which probably is stupid

    Dag
    life is too short smart but hard !

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    >>now that those are set, I want to remove the values in them so I can reset them via other functions in my program..

    The memory for an array is allocated enmasse, all or none. You can't selectively delete some memory. Initially, the value at a given memory location is random garbage. It only becomes useful when you put something useful there. However, once you put something there, it will remain there until the memory is destroyed. Therefore if you have an array of char as follows:

    a[0] = 'a';
    a[1] = 'b';
    a[2] = 'c';

    and you want to "delet" a[1] and change it to c and then "delete" a[2] completely, you would do this:

    a[1] = 'c';

    Unfortunately, you can't selectively delete a[2] without also deleting a[0] and a[1]. There will always be some value associated with a[2]. It may be garbage, it may be outdated, it may be valid; but there will always be something. To deal with this you can keep track of the number of valid indexes in the array. If you want to get rid of a value that isn't the last value in the array you should somehow mark that the index isn't valid or shift all values to the right of the removed index to the left, etc. If you use dynamic memory you can copy the remaining data into a holding array, delete memory for the original array, and redeclare memory for the original array, now downsized to fit the number of valid elements, and then recopy the information in the holding array into the original array, if you want to keep using the same "name" of the array that you started with.
    _________________________________________

    if you do something like this:

    type *a = new a;
    type *b = a;
    delete b;

    you will delete the memory assigned to a originally so you should assign NULL or some other valid value to both a and b before you use them again. Deleting NULLed pointers is not dangerous as NULL is always valid, but it has no value either, so using NULLed pointers in some circumstances is illogical or leads to undefined behaviour so checking for NULL value before using the pointer if you aren't sure what value it has is always a good practice. Likewise, deleting values you don't want to delete or stranding values in memory without a way to access them later on can be problematic. It can be tricky to keep of dynamic memory, so using it with appropriate caution is recommended.
    You're only born perfect.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by prog-bman
    How exactly does delete work? I was playing with it a little and it seems that you can still access the memory that was allocated. I know this isn't smart and you should set your pointer to NULL after a delete but I still wonder.
    Implementation-dependent. It may vary from OS to OS and even from compiler to compiler.

    Generally, though, the list of free memory blocks that the allocator manages is updated to mark your block as free again. So as long as the OS doesn't mark the memory page inaccessible again (which it won't do as long as at least one other block of memory is allocated within, and probably not then), the memory is still freely modifiable and readable. It's just that you can't know when the allocator decides to give the same memory out again, or if it overwrites it, or anything. In other words, modifying freed memory invokes the dreaded undefined behaviour: it might crash, it might "work", it might work a hundred thousand times and crash the next, it might work until you make a program change and have totally forgotten this piece of code.
    Or it might make the proverbial demons come out of your nose.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >char a[2];
    >a[0] = 'a';
    >a[1] = 'b';
    >a[2] = '\0';
    There is no a[2]. Arrays start at index 0, so your elements run from 0 to 1. So if you want room for the string terminator:
    Code:
    char a[3];
    a[0] = 'a';
    a[1] = 'b';
    a[2] = '\0';
    To clear the string:
    Code:
    a[0] = '\0';

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 08:44 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM

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