int i(0) in a for loop

This is a discussion on int i(0) in a for loop within the C++ Programming forums, part of the General Programming Boards category; I recently came across this in some code: Code: for (int i(0); i<10; i++) cout << i << endl; What ...

  1. #1
    FOX
    Join Date
    May 2005
    Posts
    188

    int i(0) in a for loop

    I recently came across this in some code:
    Code:
    for (int i(0); i<10; i++)
            cout << i << endl;
    What does i(0) do? I've never seen anything like it before. It looks like some kind of strange function declaration to me, but the code works as intended so that can't be it.

    A search on the forums give hardly any results, so it can't be widely used?

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    it seems to be initializing i to zero, but I would suggest not using it...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212
    it's said that int i(0) ( direct initialization ) is more efficient than int i = 0 ( copy initialization );

  4. #4
    Banned
    Join Date
    Jun 2005
    Posts
    594
    looks fimilar
    like how you can do

    string mystring ("my string");


    i guess work the same way without using =,
    i dont see anything wrong with it use.

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    except int isn't a class.

    edit: it's standard code... I just remembered that it's usually used in class constructors to initialize data members. It's an initialization method, so it really shouldn't work after initialization, but you could try it out...

    edit2: yeah, it's used only in initialization, and it is called direct initialization.
    Last edited by major_small; 07-13-2005 at 12:25 AM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by ^xor
    I recently came across this in some code:
    Code:
    for (int i(0); i<10; i++)
            cout << i << endl;
    What does i(0) do?
    It takes up one more character than "i=0". Other than that, it's the same.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    FOX
    Join Date
    May 2005
    Posts
    188
    Quote Originally Posted by Antigloss
    it's said that int i(0) ( direct initialization ) is more efficient than int i = 0 ( copy initialization );
    What's the difference between direct initialization and copy initialization? I know some assembly and I can't even imagine what the difference would be.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Here? Probably nothing. Your compiler will usually optimize things fairly good. Often times better than you would were you trying. In cases like this, the difference isn't worth the time spent talking about it. But I'm sure some one here, if they feel motivated enough, will vomit up some assembly output of the two, which usually depends on your compiler flags anyway..


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Quote Originally Posted by Antigloss
    it's said that int i(0) ( direct initialization ) is more efficient than int i = 0 ( copy initialization );
    No, these are equally efficient.

  10. #10
    FOX
    Join Date
    May 2005
    Posts
    188
    I still don't see what the difference is. AFAIK, there is no single processor instruction that can create and initialize a variable. You first make room on the stack by subtracting the size from esp (x86), and then set the memory to 0 with another instruction. What could i(0) possibly be doing differently? In fact, the assembly code generated is exactly the same with GCC.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,269
    I'm not too sure of this myself, so what I write is from the book "C++ Gotchas" by Stephen Dewhurst, 2003. (Gotcha #56, pages 153-156)
    Dewhurst gives the following class example:
    Code:
    class Y {
    public:
    	Y(int);
    	~(Y);
    };
    He then presents 3 initializations of a Y object:
    Code:
    Y a(1066);
    Y b = Y(1066);
    Y c = 1066;
    Dewhurst then proceeds to claim that:
    In point of fact, all three of these initializations will probably result in the same object code being generated, but they're not equivalent. The initialization of a is known as a direct initialization, and it does precisely what one might expect. The initialization is accomplished through a direct invocation of Y::Y(int).

    The initializations of b and c are more complex. In fact, they're too complex. These are both copy initializations. In the case of the initialization of b, we're requesting the creation of an anonymous temporary of type Y, initialized with the value 1066. We then use this anonymous temporary as a parameter to the copy constructor for class Y to initialize b. Finally, we call the destructor for the anonymous temporary.

    ...

    The semantics of the initialization of c are the same, but the creation of the anonymous temporary is implicit.
    Dewhurst proceeds to change the declaration of Y to include a copy constructor that calls abort(). He then notes that chances are, all three initializations work without terminating the process.

    He explains this by writing:
    What gives is that the standard explicitly allows the compiler to perform a program transformation to remove the temporary generation and copy constructor call and to generate the same code as in the case of a direct initialization. Note that this is not a simple "optimization", since the actual behavior of the program is altered (in this case, we didn't terminate the process). Most C++ compilers will perform the transformation, but they're not required to do so by the standard.
    Now, for the int i(0) example, I think the situation is different. We are dealing with a fundamental type in C++, and furthermore using an integer literal to initialize an integer. In Dewhurst's example, it would be like using a Y literal to initialize a Y object - but of course Y literals dont exist in C++.
    Last edited by laserlight; 07-13-2005 at 08:49 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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. memory leak
    By aruna1 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 11:28 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  4. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 06:20 AM
  5. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM

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