Re-Learning

This is a discussion on Re-Learning within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by Kramer55 Another quick question, what is the point of having size_t in this code? I know it ...

  1. #16
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Kramer55
    Another quick question, what is the point of having size_t in this code?

    I know it returns the size, but what is the point of having the size?
    It doesn't "return" the size. A size_t is a type, an unsigned integral type. The result of the sizeof operator has type size_t.

    http://www.lysator.liu.se/c/rat/c3.html#3-3-3-4:
    The type of sizeof, whatever it is, is published (in the library header <stddef.h>) as size_t, since it is useful for the programmer to be able to refer to this type. This requirement implicitly restricts size_t to be a synonym for an existing unsigned integer type, thus quashing any notion that the largest declarable object might be too big to span even with an unsigned long. This also restricts the maximum number of elements that may be declared in an array, since for any array a of N elements,

    N == sizeof(a)/sizeof(a[0])

    Thus size_t is also a convenient type for array sizes, and is so used in several library functions. (See 4.9.8.1, 4.9.8.2, 4.10.3.1, etc.)
    It is suited to an example more like this.
    Code:
    #include <iostream>
    #include <cstddef>
    
    using namespace std;
    
    int main()
    {
       int array[] = {1,2,3,4,5,6,7,8,9,10};
       for ( size_t i = 0; i < sizeof array / sizeof array[0] ; ++i )
       {
          cout << array[i] << endl;
       }
       return 0;
    }
    Then you don't need to hard-code the array size in both the definition and the loop.

    [edit]
    Quote Originally Posted by Tronic
    When you have this code:
    Code:
    for(int i = 0; i < i < 10; i++)
    {
    
    }
    It will iterate 10 times, or more specifically, every time i is less than the value 10, and greater than or equal to the value of zero. Sometimes people confuse the "byte size" of a variable, with the "max-value" of a variable.

    Say you have the code..
    Code:
    #include <iostream>
    #include <conio.h>
    
    int main()
    {
      char *name;
      std::cout << "Enter in name\n" 
                    << "> ";
        std::cin >> name;
      for(char *c = name; *c < sizeof(name); c+=sizeof(char))
      {
    	  std::cout << *c;
      }
      getch();
    return 0;
    }
    What a train wreck of an example.
    Last edited by Dave_Sinkula; 04-20-2005 at 03:11 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  2. #17
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >What a train wreck of an example.
    I doubt this one will make the FAQ.

  3. #18
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    How about taking my example code as an example that wasn't intended to run correctly and get rid of your anal tendancies a wee bit? I was showing him a concept of how incrementing by the size of a variable vs. and index would work generally, get off my case.
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  4. #19
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    [sarc]good thing the rep system is no longer in place eh? lol [sarc/]


  5. #20
    Registered User
    Join Date
    Mar 2005
    Posts
    39
    yeah i wish rep was back

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Machine Learning with Lego Mindstorms
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 01-30-2009, 02:34 PM
  2. Best Approach for Learning
    By UCnLA in forum C Programming
    Replies: 5
    Last Post: 03-21-2008, 03:35 AM
  3. Need Help On a Simple Bank Program
    By oobootsy1 in forum C# Programming
    Replies: 9
    Last Post: 08-08-2005, 11:51 AM
  4. Learning Rate Of C++
    By Krak in forum C++ Programming
    Replies: 27
    Last Post: 01-29-2003, 01:53 PM
  5. API learning reference (for those who are learning)
    By steve_i83 in forum Windows Programming
    Replies: 1
    Last Post: 11-04-2001, 01:58 AM

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