Thread: Problem with my c++ program

  1. #16

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by userxbw
    The actual size of the third array is known ahead of time and it is staying that size, not growing some time later on within the program before it ends. Vector is not really needed, it is a one time shot, not create 2 array's put them into a bigger array, then add more later before program ends. where a vector which (dynamically) can grow in size would come in.

    for the first 2 arrays, if data gathering was an unknown size at start time then vector would come in, but he already asks what size do you want ahead of time. it then becomes known ahead of time, therefore, again vector is not needed. Unless he wants to learn how to grow the array(s) dynamically one at a time.
    You miss the point as to why we use containers rather than do manual memory management, except of course when we are implementing the said containers or specialised data structures/smart pointers for which existing containers or existing smart pointers might not be suitable as underlying implementation. I happened to link to this article just now in another thread, so I might as well repeat it here... read Stroustrup's answer to the FAQ: How do I deal with memory leaks?

    A std::vector<int> is the typical replacement for a dynamic array implemented using new[]/delete[]. Granted, it does more than what is needed in this case, and if one really wanted there are alternatives that do manage their own memory, but generally it is the most typical solution to reach for here.
    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. #18
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by laserlight View Post
    You miss the point as to why we use containers rather than do manual memory management, except of course when we are implementing the said containers or specialised data structures/smart pointers for which existing containers or existing smart pointers might not be suitable as underlying implementation. I happened to link to this article just now in another thread, so I might as well repeat it here... read Stroustrup's answer to the FAQ: How do I deal with memory leaks?

    A std::vector<int> is the typical replacement for a dynamic array implemented using new[]/delete[]. Granted, it does more than what is needed in this case, and if one really wanted there are alternatives that do manage their own memory, but generally it is the most typical solution to reach for here.
    try to think like the instructor, looks like he is trying to just teach shorting here not what you're trying to show me. if he uses a vector it totally eliminates the need to use anyone of the three types of sorting the array, just call to sort, start,end then return it, then print it. the exercise has been made totally useless. teachers do not jump in head first then work their way backwards to the basics.
    Last edited by userxbw; 10-02-2017 at 05:44 PM.

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by userxbw
    try to think like the instructor, looks like he is trying to just teach shorting here not what you're trying to show me. (...) teachers do not jump in head first then work their way backwards to the basics.
    That was my point when I suggested that it was enough to use the original solution involving fixed size arrays, not dynamic memory allocation. Once you want to touch dynamic memory allocation, then either it should have already been taught after the use of containers, or you should suggest the use of an appropriate container instead. The use of dynamic memory allocation is not the basics, hence once you suggest it, it is perfectly valid for me to suggest the use of std::vector<int> instead, since "he is trying to just teach shorting here not what you're trying to show me".

    I recommend that you read Stroustrup's essay on Learning Standard C++ as a New Language (PDF) to understand why this is so.

    Quote Originally Posted by userxbw
    if he uses a vector it totally eliminates the need to use anyone of the three types of sorting the array, just call to sort, start,end then return it, then print it. the exercise has been made totally useless.
    That is not true. The use of std::vector does not eliminate the need to combine the input and sort it. If it could do that, then so could the use of new[]/delete[], or the use of fixed size arrays.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is the problem in this program ?
    By mysterious in forum C Programming
    Replies: 8
    Last Post: 10-28-2012, 06:39 AM
  2. Replies: 4
    Last Post: 10-16-2008, 07:30 PM
  3. What's the problem about this program?
    By Mathsniper in forum C Programming
    Replies: 18
    Last Post: 12-18-2006, 07:44 AM
  4. Math Equation Program (I can't find the problem with my program!)
    By masked_blueberr in forum C Programming
    Replies: 14
    Last Post: 07-06-2005, 11:53 AM
  5. BIG problem-O with a C program
    By JohnMayer in forum C Programming
    Replies: 13
    Last Post: 07-20-2002, 03:41 PM

Tags for this Thread