Thread: Probable flaw

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    1

    Question Probable flaw

    Dear Sir,
    I am beginner in C++, and your book has been greatly helpful in learning this programming language. In your book, I have found an example which seems to have flaws.
    Under the topic Dynamic Memory Allocation, there is an example to show a technique of reallocating memory as the values grow.
    You say that the program reads in numbers from the user, and if the user enters more numbers than can fit in the array, the function int *growArray (int* p_values, int cur_size) will resize it.
    In my attempt to understand the program I found that condition if (size == next_element + 1) never becomes true as next_element always remains 0, and so there is no chance of calling the function. But the program repeatedly prints the first element deceiving the user to make them think what the program is not doing.
    We can, however, call function by incrementing next_element (i.e next_element++) and then check the if condition, but doing that also doesn’t solve the problem since the function doesn’t return the new value of cur_size (to size ) when it does return p_new_values;, but program does “allow the user to enter more new values” as next_element now exceeds the value of size and hence If condition never becomes true again.

    I appreciate the effort you have put into the book to teach C++ to novice programmers like me. I hope to hear suggestions on writing the program correctly.
    Thank you.

    Sincerely,
    Bikash Kunwar

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Maybe this thread will be of some help: http://cboard.cprogramming.com/progr...-part-1-a.html

    It sounds like you're having a problem that is addressed on that thread, under the heading "Page 148: Code - sample code 36, resize_array.cpp 1" (or maybe the one after).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how it works..!! is it a flaw in c ??
    By veera in forum C Programming
    Replies: 7
    Last Post: 08-12-2013, 08:41 PM
  2. Design flaw in C++?
    By @nthony in forum C++ Programming
    Replies: 24
    Last Post: 10-20-2008, 07:47 PM
  3. design flaw
    By Bleech in forum Windows Programming
    Replies: 2
    Last Post: 10-23-2007, 11:10 PM
  4. yet another unexplainable flaw
    By stormbreaker in forum C++ Programming
    Replies: 4
    Last Post: 06-01-2003, 12:56 PM
  5. is this a design flaw
    By blight2c in forum C++ Programming
    Replies: 5
    Last Post: 03-20-2002, 12:33 AM

Tags for this Thread