Results 1 to 14 of 14

Thread: stupid C++

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2001
    Posts
    525

    Angry stupid C++

    newbie here to C++

    can someone tell my this would fail??

    p1=p1+4
    p2=p1-1

    for ( source = 17; source != p2; source--)

    it works if i said

    for ( source = 17; source != 3; source--)

  2. #2
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    is p1 initialized?

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    May 2001
    Posts
    525
    i believe

    Code:
    int	p1,p2;

  4. #4
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    Originally posted by jsun9
    i believe

    Code:
    int	p1,p2;
    You declared the variables, but there is NOTHING inside.

    p1=p1+4
    How are you excepting that : p1 = nothing + 4 will ever work?
    \m/\m/

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    May 2001
    Posts
    525
    wha? how am I supposed to increment p1 by 4?

  6. #6
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    Originally posted by jsun9
    wha? how am I supposed to increment p1 by 4?
    What you have to understand is that int p1 is ONLY a int and contains NOTHING inside because you still didnt set it to anything..so if it is NOTHING you can't increment it. Simply put you must do int p1=0; or whatever..

    I think this is quite obvious..
    \m/\m/

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    May 2001
    Posts
    525
    thanks.......

  8. #8
    Hyperactive Member Comreak's Avatar
    Join Date
    Feb 2001
    Location
    Dis
    Posts
    319
    For future reference, only global variables (variables declared outside a function, including the main() function) are automatically initialized. Everything else is filled with whatever ramdom junk is in memory (including values of previous variables).
    C.O.M.R.E.A.K.: Cybernetic Obedient Machine Responsible for Exploration and Accurate Killing

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Not even global variables are initialized. There is no automatic initialization for primitives anywhere in C++.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10
    Hyperactive Member Comreak's Avatar
    Join Date
    Feb 2001
    Location
    Dis
    Posts
    319
    Originally posted by CornedBee
    Not even global variables are initialized. There is no automatic initialization for primitives anywhere in C++.
    You sure about that?

    http://cplus.about.com/library/weekly/aa101902k.htm

    Global integers(for example) are consistently initialized to zero in both GCC and MVC++.NET.
    Last edited by Comreak; Dec 19th, 2003 at 02:08 PM.
    C.O.M.R.E.A.K.: Cybernetic Obedient Machine Responsible for Exploration and Accurate Killing

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Ok, you're right. They are initialized.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  12. #12
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    Why global vars are initialized and the others are not?
    \m/\m/

  13. #13
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Because the C++ standard says so?

    I guess it's a speed issue. Local variables would have to be initialized every time the function is called, global vars get initialized once by the exe loader.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  14. #14
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906
    Originally posted by PT Exorcist
    You declared the variables, but there is NOTHING inside.


    How are you excepting that : p1 = nothing + 4 will ever work?
    Well actually they contain whatever was in those 4 particular bytes of memory occupied by the integer last. Junk basically, stuff like:
    -839295
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width