Thread: Learning C++ alone, A few questions

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    12

    Learning C++ alone, A few questions

    I'm self learning C++ at the moment with very little programming experience. A few questions came up while I was reading through a C++ tutorial and please do keep in mind I essentially just started with programming (Although my join date may indicate otherwise);

    1. I was reading about the the 'new' and 'delete' operators and it seemed to be in the format of

    Code:
    pointer = new type [int]
    I had just finished reading a sections on arrays and pointers before that and I was wondering why I cannot just use the 'new' operator on an array to add elements to it? If new only works on pointers then is there no way to add additional elements to a multi-dimensional variable?

    The delete operator seems to delete all the new elements added with 'new', is there no way to specify an element to delete (Such as the last element)?

    2. Throughout the tutorial I'm going through, I've been making a bunch of console programs. How exactly are other programs made using C++, like if I wanted to make a simple text editor such as notepad for example. I don't want a console window popping up =/ A link to an explanation of this subject is good enough if I am asking too much.

    3. I'm using the Dev-C++ Bloodshed IDE Compiler because it was a quick 5-7mb DL. What's a better compiler to be using both for practicality and learning?

    Also as an aside, if I have further questions while learning C++, should I continue posting in this topic or make a new topic each time?
    Last edited by LegendsEnd; 05-05-2007 at 08:20 PM.
    Rawr.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    why I cannot just use the 'new' operator on an array to add elements to it? If new only works on pointers then is there no way to add additional elements to a multi-dimensional variable?
    As I understand it, new[] allocates a contiguous block of memory. Consequently, you cannot be guaranteed that there will be space at the end of that block, so the only way to properly expand the array is to allocate a larger block and copy over.

    The delete operator seems to delete all the new elements added with 'new', is there no way to specify an element to delete (Such as the last element)?
    new is paired with delete, and new[] is paired with delete[]. In a way, what you are asking for can be done with a dynamic array of pointers. For example:
    Code:
    int** pointers = new int*[10]; // Allocate the array of pointers.
    // Allocate each element in the array of pointers.
    for (int i = 0; i < 10; ++i)
    {
        pointers[i] = new int(i); // Initialise each element to i.
    }
    
    delete pointers[9]; // Delete the last element.
    pointers[9] = 0;    // Set to null so we can check that it is deleted.
    
    // Deallocate each element of the array of pointers.
    for (int i = 0; i < 10; ++i)
    {
        delete pointers[i]; // It is safe to delete a null pointer.
    }
    delete[] pointers; // Deallocate the array of pointers.
    That said, the problem with this is that with many deletions in the middle of the array, you would probably end up with many gaps in the array, and perhaps in such a case you would be looking for a linked list type of data structure instead. If you are only going to do many deletions from the end, then perhaps you could keep track of the size of the array and just ignore elements outside of the boundary instead of taking this array of pointers approach.

    How exactly are other programs made using C++, like if I wanted to make a simple text editor such as notepad for example.
    You would use the appropriate API for your platform, or a cross platform GUI library/toolkit/framework such as Qt or wxWidgets.

    What's a better compiler to be using both for practicality and learning?
    The compiler that Dev-C++ uses by default is good enough. If you are looking for a development environment under active development, then perhaps Code Blocks would be more suitable. Alternatively, if you are on Windows, you can download the free MS Visual C++ 2005 Express, which also has a suitable compiler.

    Also as an aside, if I have further questions while learning C++, should I continue posting in this topic or make a new topic each time?
    Make a new topic for each specific question.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    To respond to your second question, if you want to create GUI applications in C/C++, it is much much more complicated than console applications.

    First, GUI development depends on a specific API (which, as I understand it, is a set of libraries which simplifies the interface between the machine and the programmer), and because APIs are not uniform across platforms, you need a specific API for every flavor of OS: Windows, Mac OS, Linux, whatever. If you want to write a GUI application for every one of those platforms, you will have to learn every API that is supported for those environments: either MFC, .NET, or Win32 for Windows, QT for KDE, GTK for Gnome, and I have no idea what they use in Mac. There's a library called WxWidgets which aims for platform neutrality, but if a user's computer doesn't have it installed, whatever you coded with this library will not work on their computer.

    To give an example of how complicated even writing the canonical "Hello, World!" program with a Win32 API is (and I don't think it's much less complicated for the other libraries either), have a gander at this code:
    http://www.paulgriffiths.net/program/c/winhellosrc.html

    So if you want maximum bang in terms of GUI development with minimum effort, I'd recommend Java. As long as a user has JRE installed on his machine, your application will (probably) run on it.

    If you're going to stick to MS Windows, I heard Visual Basic .NET or C# isn't half bad.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    More information about your first question... In C++ you rarely will actually want to use new to allocate space for an array. Standard C++ has containers that should be used instead. The implementations of these containers (like vector) are safer, less buggy, and less error-prone than managing memory yourself. Unfortunately, many teaching books/tutorials/instructors still fail to teach the use of standard containers.

    Knowing how new/delete and new[]/delete[] work is important, but you should remember that they should be used all that often in C++. (Actually, new is used often, but usually in conjunction with smart pointers that handle the delete for you. new[] and delete[] are completely handled by the containers.)

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    12
    Quote Originally Posted by cunnus88 View Post
    To respond to your second question, if you want to create GUI applications in C/C++, it is much much more complicated than console applications.

    First, GUI development depends on a specific API (which, as I understand it, is a set of libraries which simplifies the interface between the machine and the programmer), and because APIs are not uniform across platforms, you need a specific API for every flavor of OS: Windows, Mac OS, Linux, whatever. If you want to write a GUI application for every one of those platforms, you will have to learn every API that is supported for those environments: either MFC, .NET, or Win32 for Windows, QT for KDE, GTK for Gnome, and I have no idea what they use in Mac. There's a library called WxWidgets which aims for platform neutrality, but if a user's computer doesn't have it installed, whatever you coded with this library will not work on their computer.

    To give an example of how complicated even writing the canonical "Hello, World!" program with a Win32 API is (and I don't think it's much less complicated for the other libraries either), have a gander at this code:
    http://www.paulgriffiths.net/program/c/winhellosrc.html

    So if you want maximum bang in terms of GUI development with minimum effort, I'd recommend Java. As long as a user has JRE installed on his machine, your application will (probably) run on it.

    If you're going to stick to MS Windows, I heard Visual Basic .NET or C# isn't half bad.
    Thanks for the in-depth reply cunnus, that does indeed look complicated. The reason I want to learn C++ instead of Java is because I will likely be learning Java anyways in the fall when I start CS100/CS200 in University (Mandatory first year course for some reason). I'm learning C++ instead of C# because most programmer related jobs seem to require knowledge of C++. I'm not looking into one of these jobs but it's a beneficial skill I would think to have anyways. Do you know of a tutorial which goes through the process of using a Windows API? I havn't even heard of an API until now lol.

    More information about your first question... In C++ you rarely will actually want to use new to allocate space for an array. Standard C++ has containers that should be used instead. The implementations of these containers (like vector) are safer, less buggy, and less error-prone than managing memory yourself. Unfortunately, many teaching books/tutorials/instructors still fail to teach the use of standard containers.

    Knowing how new/delete and new[]/delete[] work is important, but you should remember that they should be used all that often in C++. (Actually, new is used often, but usually in conjunction with smart pointers that handle the delete for you. new[] and delete[] are completely handled by the containers.)
    Is there a tutorial explaining and teaching the usage of containers? I ran a search on this website but the closest thing I found was: http://www.cprogramming.com/tips/sho...ount=30&page=1

    Not as in-depth as I had hoped.
    Rawr.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Here are the links to the tutorials from this site: http://www.cprogramming.com/tutorial.html#stltutorial

    I haven't really read them, but they've got more information than the tip you found. Ideally, you'd be learning this from a book, but even then there are only a few of those that teach it well.

    For more information online try searching for C++ vector tutorials or std::vector.

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    Do you know of a tutorial which goes through the process of using a Windows API?
    Unfortunately, I haven't done much API programming, and what I have done was in VB 6. Never touched GUI programming again.

    There are two things I do know. Whenever I visited a software company where there was a lot of windows programming going on, they always had "Programming Windows" by Charles Petzold on their shelves. I've briefly thumbed through it, and it is not a tutorial.

    Second, the Win32 programmers I know consider themselves C programmers, not C++ programmers. A lot of them say they get by almost without ever using the STL or classes. And if they do, it's just the vector class.

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. Want to start learning raw packets in C
    By Lateralus in forum Networking/Device Communication
    Replies: 1
    Last Post: 06-08-2005, 12:55 PM
  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. questions about new and delete
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2001, 01:48 PM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM