Thread: Terminator in integer Array.

  1. #16
    Registered User
    Join Date
    May 2020
    Posts
    23
    Hi, I'm Bill, a new member and a Unix guy.

    I don't know why you need the terminator when you know how many numbers are in the array. Would you ever need to count numbers like this, say?

    Code:
    counter = 0;
              while (a[counter] != terminator)
                  counter++;

  2. #17
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > don't know why you need the terminator when you know how many numbers are in the array.
    Because that's what the assignment said to use.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #18
    Registered User
    Join Date
    May 2020
    Posts
    23
    Quote Originally Posted by Salem View Post
    > don't know why you need the terminator when you know how many numbers are in the array.
    Because that's what the assignment said to use.
    I know that, but I wonder purpose the terminator serves when there's already a way to tell whether you've reached the end of the list that the array stores. Why does the teacher want the students to use a terminator then? To speed up a sequential search, I add the target to the end of a list. Then I know that the list contains it when there's another instance of the target before the added one.

    Code:
    bool contains(int list[], const int target, const int length)
    {
       register int place = 0;
    
       list[length + 1] = target;
       while (list[place] != target)
          place++;
       return place < length + 1;
    }
    Last edited by BillMcEnaney; 06-01-2020 at 02:02 AM.

  4. #19
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Why does the teacher want the students to use a terminator then?
    Maybe they're trying to teach how strlen() works.
    Maybe they're trying to convey one of several methods of solving the same problem.

    > I add the target to the end of a list.
    As your your approach, what if your int list[] were const?
    For that matter, how do you know there is even room to append something to the array (even temporarily).
    Even if there is room, how do you know you're not trashing some user data.
    Code:
    int list[] = {1, 2, 3, 4, 5, 6, 7, 8, 9 };
    if( contains(list,10,5) )  // trashes my data
    if( contains(list,10,9) )  // out of bound access
    Principle of least astonishment - Wikipedia


    And 'register' stopped being useful in the 1990's, when every half-decent compiler started implementing decent optimisation options.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #20
    Registered User
    Join Date
    May 2020
    Posts
    23
    Quote Originally Posted by Salem View Post
    > Why does the teacher want the students to use a terminator then?
    Maybe they're trying to teach how strlen() works.
    Maybe they're trying to convey one of several methods of solving the same problem.

    > I add the target to the end of a list.
    As your your approach, what if your int list[] were const?
    For that matter, how do you know there is even room to append something to the array (even temporarily).
    Even if there is room, how do you know you're not trashing some user data.
    Code:
    int list[] = {1, 2, 3, 4, 5, 6, 7, 8, 9 };
    if( contains(list,10,5) )  // trashes my data
    if( contains(list,10,9) )  // out of bound access
    Principle of least astonishment - Wikipedia


    And 'register' stopped being useful in the 1990's, when every half-decent compiler started implementing decent optimisation options.
    If the array had been const, I couldn't have added an element to it. I know I assumed the array had room for terminator, the second instance of the target. I haven't tried to run that function. But I ignored whether anyone else would do that. I posted it only to show a way to use a terminator.

    I use "const" partly because I'm a huge fan of purely functional programming in Haskell. Haskell is excellent, I think, partly because it prevents function side effects. Sadly, though, C is permissive enough to allow subtle unwanted ones.

    What about the keyword "register?" Well, it still seemingly helps when I run gcc's C compiler.
    Last edited by BillMcEnaney; 06-02-2020 at 02:10 AM.

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by BillMcEnaney
    I use "const" partly because I'm a huge fan of purely functional programming in Haskell. Haskell is excellent, I think, partly because it prevents function side effects.
    The thing is that declaring those two parameters as const is only useful to the implementation of the function, i.e., to prevent an accidental modification of parameters that should not be modified, but for such a small function it arguably doesn't really help. To the caller, it makes no difference either way, whereas it would have made a difference for the first parameter, i.e., to assure the caller that contains will indeed merely return a result indicating whether the array contains the search item, without modifying the array... yet you omitted it precisely because you intended to do something that callers normally wouldn't expect from a function with such a job.

    Quote Originally Posted by BillMcEnaney
    Sadly, though, C is permissive enough to allow subtle unwanted ones.
    ... like modifying the content of an array in a function named contains?

    Quote Originally Posted by BillMcEnaney
    What about the keyword "register?" Well, it still seemingly helps when I run gcc's C compiler.
    Maybe if you didn't enable optimisations at all. A quick check of a program that correctly uses your contains function shows that compiling with gcc 9.3.0 at -O1 or higher produces correspondingly identical assembly output with or without the register keyword, i.e., it doesn't help. Not even in the slightest.
    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

  7. #22
    Registered User
    Join Date
    May 2020
    Posts
    23
    Laserlight, thank you for pointing out my mistake. I should have declared the array as constant and found another way to stop the loop. Before writing the function, I should have thought carefully, too, because I want to write functional programs in C when I can. Even if my computer won't allocate registers for register variables, I'll still use the keyword "register" because I may need to compile my programs on platforms where that word will get registers allocated.

    Sadly, although I'm a perfectionist, I'm sometimes impulsive. So I suppose I wrote my function impulsively.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. integer null terminator
    By vead in forum C Programming
    Replies: 6
    Last Post: 02-11-2018, 08:42 PM
  2. function terminator
    By programmerc in forum C Programming
    Replies: 3
    Last Post: 02-17-2013, 08:08 AM
  3. array and string terminator
    By bobknows in forum C++ Programming
    Replies: 2
    Last Post: 03-08-2011, 07:45 PM
  4. Terminator 4
    By abachler in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 05-23-2009, 11:15 PM
  5. Terminator 3..nope
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 30
    Last Post: 07-18-2003, 07:42 AM

Tags for this Thread