Lot of questions.

This is a discussion on Lot of questions. within the C++ Programming forums, part of the General Programming Boards category; Can you tell me if i made any small errors or if i did it completely wrong? You made at ...

  1. #31
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,650
    Can you tell me if i made any small errors or if i did it completely wrong?
    You made at least three mistakes, and I'll point out the first and third: i was not initialised, so the first printing of *p prints garbage; *p = 0 assigns 0 to the object pointed to by p, but at that point p points to a destroyed object. You mean to write p = 0;

    The second mistake will be pointed out by the compiler.

    EDIT:
    Okay, there is a fourth mistake: x is not initialised either.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #32
    C++ noob Ryan0773's Avatar
    Join Date
    Jan 2009
    Posts
    43
    Quote Originally Posted by laserlight View Post
    The second mistake will be pointed out by the compiler.
    Would this mistake be the redeclaration of int*p?


    What do you meen they werent initialized?

  3. #33
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,650
    Quote Originally Posted by Ryan0773
    Would this mistake be the redeclaration of int*p?
    Yes.

    Quote Originally Posted by Ryan0773
    What do you meen they werent initialized?
    They contain garbage values. You should write say,
    Code:
    int i = 123;
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #34
    C++ noob Ryan0773's Avatar
    Join Date
    Jan 2009
    Posts
    43
    How could i fix the problem of redeclaration? When the compiler tells me thats the problem it doesn't have a suggestion that could fix it.

    Well i guess they were garbage values (if that means that they didn't have any value) but that was just an example, i guess this is what it would look like with values:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int i(25 + 4);
        int x(4);
        int *p;
        p = &i;
        cout<< *p <<"\n";
        cout<< *p <<"\n";
        int *p = new int(x);
        p = &x;
        cout<< *p <<"\n";
        delete p;
        p = 0;
        cin.get();
    }
    the redeclaration error is still there though.


    PS: You told me there were four error, the redeclaration, and the two garbage values...whats the last one?

    Nvm, the last one was the p = 0 thing
    Last edited by Ryan0773; 01-04-2009 at 12:29 PM. Reason: Oops

  5. #35
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,650
    Quote Originally Posted by Ryan0773
    How could i fix the problem of redeclaration?
    Change:
    Code:
    int *p = new int(x);
    to:
    Code:
    p = new int(x);
    Quote Originally Posted by Ryan0773
    PS: You told me there were four error, the redeclaration, and the two garbage values...whats the last one?
    I counted the garbage values as one each.

    However, there is another mistake:
    Code:
    p = &x;
    You should not be assigning the address of x to p since p points to dynamically allocated memory.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #36
    C++ noob Ryan0773's Avatar
    Join Date
    Jan 2009
    Posts
    43

    Talking Thanks alot!

    It worked, thanks alot! I still dont fully understand why a pointer would be needed (can they copy large amounts of code or just one simple integer?)but at least i know how to work them. Again thanks alot! Ill be sure to try and get through the other tutorials!

  7. #37
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,650
    Quote Originally Posted by Ryan0773
    It worked, thanks alot!
    No problem, but you might want to post your complete final code in case there are other (undetected) mistakes.

    Quote Originally Posted by Ryan0773
    I still dont fully understand why a pointer would be needed
    It is not needed in this case, actually

    Quote Originally Posted by Ryan0773
    can they copy large amounts of code or just one simple integer?
    The point is that a pointer points to an object. Copying a pointer is cheap even if copying the object (which could be composed of multiple sub-objects) is not.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #38
    C++ noob Ryan0773's Avatar
    Join Date
    Jan 2009
    Posts
    43
    Quote Originally Posted by laserlight View Post
    No problem, but you might want to post your complete final code in case there are other (undetected) mistakes.
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int i(25 + 4);
        int x(4);
        int *p;
        p = &i;
        cout<< *p <<"\n";
        cout<< *p <<"\n";
        p = new int(x);
        p = &x;
        cout<< *p <<"\n";
        delete p;
        p = 0;
        cin.get();
    }
    This is the final code. It should make it so that the first 2 numbers it shows are 29 and the last one it shows is 4. after that, the new int is deleted and p point to nothing (0) Then it waits for a keypress to exit, correct?


    Quote Originally Posted by laserlight View Post
    The point is that a pointer points to an object. Copying a pointer is cheap even if copying the object (which could be composed of multiple sub-objects) is not.
    What do you mean by cheap?

  9. #39
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,650
    There is still that last mistake. This:
    Code:
    p = new int(x);
    p = &x;
    cout<< *p <<"\n";
    should be:
    Code:
    p = new int(x);
    cout<< *p <<"\n";
    Quote Originally Posted by Ryan0773
    What do you mean by cheap?
    Few bytes to copy.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #40
    C++ noob Ryan0773's Avatar
    Join Date
    Jan 2009
    Posts
    43
    Quote Originally Posted by laserlight View Post
    There is still that last mistake. This:
    Code:
    p = new int(x);
    p = &x;
    cout<< *p <<"\n";
    should be:
    Code:
    p = new int(x);
    cout<< *p <<"\n";
    it doesn't seem to be a major mistake but i guess it would save some time (or is it a big mistake?)

    Thanks again for your help!

    I still dont really understand what you mean by cheap, is it less work for the computer? Like a shortcut?
    Last edited by Ryan0773; 01-04-2009 at 05:57 PM. Reason: Forgot something

  11. #41
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    Quote Originally Posted by Ryan0773 View Post
    I still dont really understand what you mean by cheap, is it less work for the computer? Like a shortcut?
    Say you have a class that holds all the points of a 3d object. For a really detailed character, an object of that class might hold several hundred x, y and z coordinate triplets. Now say you want to pass an object of that class to the function that displays it on the screen.

    The way variables are passed to functions, a copy is made of what you pass. So, for example, when you pass a simple int to a function, the program makes a copy of that int for the function to use. If you want to pass one of the 3d objects, it would make a copy of all that data, which could end up being several thousand bytes of data.

    An alternative is to use a pointer to the object to tell the function where to look for the data. So now the program makes a copy of the pointer for the function to use, and then the function looks at the data that pointer is pointing to. Instead of making a copy of hundreds or thousands of bytes of data, it only has to copy a single pointer and the program is much quicker, especially if you are drawing a 3d scene with dozens of 3d objects having to be passed to the display function several times per second. With a pointer now, the program is working on processing your data rather than spending time copying it all over the place too.

    In C++ we prefer to use references, but there are still times when pointers are needed. Under the hood, a reference is doing almost the same thing that a pointer does, but the program takes care of most of the dereferencing and other things that make pointers complicated so you can focus on making the program do what you want.
    abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments "

  12. #42
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,650
    Quote Originally Posted by Ryan0773
    it doesn't seem to be a major mistake but i guess it would save some time (or is it a big mistake?)
    It is a major mistake. By assigning the address of x to p, p now points to an object that will be automatically destroyed. As such, delete p; becomes a mistake. Furthermore, the object that was created on the previous line cannot be destroyed because there is no way to access it.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #43
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Perhaps have a look at: http://cslibrary.stanford.edu/104/
    It's quite silly, but it explains the basics.
    Also, the tutorial poisons your mind. I don't know why they insist on using T * in C++, but know that it is possible to write T*, as well.
    Instead of:
    int *p
    You can write:
    int* p
    The later reinforces that the actual type of the variable is int*. If you dereference it, you get an int, but the pointer itself has a type, which is important.

    You must also learn the difference between a value and an address. Pointers store addresses. Dereferencing addresses (ie pointers), you get the values.
    Everything allocated by new must be freed with delete. It is not automatic. If you assign a new address to a pointer which carries an address from new, that address is lost forever and you cannot free the memory you allocated. That memory is now lost. It will still be there in the program, taking up space, without the ability of being used. It's a memory leak.

    Here are a few uses of pointers:
    - Dynamic memory allocation. new returns a pointer to the memory allocated (this is usually rare in C++, however, as std::vector & co is typically used for dynamic memory).
    - Passing large objects around without copying (although, in C++, typically references are used for this).
    - Allow multiple parts of your program to access the same data everywhere, without copying (then it would be multiple instances of that data).
    - Allow multiple parts of your program to change some shared data and make sure all other parts get the new data (since all parts point to a single source of the data, if the data is then changed...).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #44
    C++ noob Ryan0773's Avatar
    Join Date
    Jan 2009
    Posts
    43
    Quote Originally Posted by jEssYcAt View Post
    An alternative is to use a pointer to the object to tell the function where to look for the data. So now the program makes a copy of the pointer for the function to use, and then the function looks at the data that pointer is pointing to. Instead of making a copy of hundreds or thousands of bytes of data, it only has to copy a single pointer and the program is much quicker, especially if you are drawing a 3d scene with dozens of 3d objects having to be passed to the display function several times per second.
    So the pointer makes the program find the class with the 3d object and instead of the program copying the entire script of the object, using up thousands of bytes, the program gets the information through the pointer and uses it to get the object where you want it. Is that correct?

  15. #45
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,650
    Quote Originally Posted by Ryan0773
    So the pointer makes the program find the class with the 3d object and instead of the program copying the entire script of the object, using up thousands of bytes, the program gets the information through the pointer and uses it to get the object where you want it. Is that correct?
    Yes, I think you got the gist of the idea.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 3 of 7 FirstFirst 1234567 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 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
  2. 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
  3. Questions.
    By anonytmouse in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-19-2004, 02:16 PM
  4. C++ test questions
    By Mister C in forum C++ Programming
    Replies: 9
    Last Post: 09-08-2002, 12:05 PM

Tags for this Thread


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