• Lucid Dreaming - Dream Views




    Page 4 of 5 FirstFirst ... 2 3 4 5 LastLast
    Results 76 to 100 of 102
    Like Tree3Likes

    Thread: Ask me about C / C++

    1. #76
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      Just you you know, you should never do something like
      Code:
      a = a + 1;
      Way to complicated, that gets transformed into this:
      Code:
      mov [a], ax
      addi ax, 1
      push ax             ;put it into temp storage
      pop ax              ;get it out of temp storage
      mov ax, [a]
      5 lines of assembly is way too much.

      The C++ way of adding a value to itself is
      Code:
      a += 1;
      Which gets assembled to

      Code:
      mov [a], ax
      addi ax, 1
      mov ax, [a]
      Bypassing the temporary storage you get down to three processor commands.

      The processor has specialized functions for adding and subtracting 1 though so to add one to a variable.

      Code:
      ++a;
      will be assembled into this
      Code:
      mov [a], ax
      inc ax         ;much faster than addi
      mov ax, [a]


      Realistically, the optimizer will do that for you, but it's better to do it explicitly.
      Last edited by ninja9578; 04-08-2010 at 04:09 PM.

    2. #77
      Ex Tech Admin Achievements:
      Created Dream Journal Tagger First Class Veteran First Class 10000 Hall Points Populated Wall Referrer Gold Made lots of Friends on DV
      slash112's Avatar
      Join Date
      Nov 2008
      Gender
      Location
      Sunny Scotland
      Posts
      5,113
      Likes
      1567
      DJ Entries
      29
      Ahh, I never knew that.

      And... You say it is "++a", I thought it was "a++"...

      Well, whichever it is, I never used that because I was experimenting with different numbers.

    3. #78
      Member
      Join Date
      Apr 2010
      LD Count
      17
      Gender
      Posts
      40
      Likes
      2
      I think that ++a; does it before, while a++; would do it after.

      If that's right, then this:
      Code:
      int a = 0;
      int b = ++a;
      Would make a and b equal to 1, while having it after a would make b equal 0 and a equal 1;

    4. #79
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      Yes. But behind the scenes, it's a little more complicated than that.

      Code:
      int a = 0;
      int b = a++;
      //b equals 0 here, while a = 1
      Here's what actually happened.
      a was created and initialized to zero
      a was copied
      a was incremented
      b was created, then initialized to the copy

      Code:
      int a = 0;
      int b = ++a;
      //a and b both equal 1
      a was created and initialized to 0
      a was incremented
      b was created and initialized to zero

      The difference is that copy. Copying takes time and in the case a few posts ago, a copy is not needed and just wastes ram and cpu power. Optimizing software is all about removing extra steps.
      spacechase0 likes this.

    5. #80
      Ex Tech Admin Achievements:
      Created Dream Journal Tagger First Class Veteran First Class 10000 Hall Points Populated Wall Referrer Gold Made lots of Friends on DV
      slash112's Avatar
      Join Date
      Nov 2008
      Gender
      Location
      Sunny Scotland
      Posts
      5,113
      Likes
      1567
      DJ Entries
      29
      Ohhhhhh, I seee.

    6. #81
      Member
      Join Date
      Apr 2010
      LD Count
      17
      Gender
      Posts
      40
      Likes
      2
      I had no idea, I guess I need to learn more about optimization then.

      Thanks for the information.

    7. #82
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      Yeah, it's good to learn some of that. When learning a language, always ask why one technique is better than another. Optimizers are specialists, there are very few of us, but we are in high demand. I'm probably in the less than 1% of programmers who can optimize at an assembly level.

    8. #83
      Member Achievements:
      1 year registered Veteran First Class 5000 Hall Points

      Join Date
      Sep 2004
      Gender
      Location
      Seattle, WA
      Posts
      2,503
      Likes
      217
      Use ++a when you don't explicitly need the pre-saved value. People who put a++ in all their loops are a pet peeve of mine. It doesn't really matter in the integer case, but if you're writing some insane iterator that has to do real work with lots of data, it will make a difference.

    9. #84
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      Me too, I ever seen post-increments in loops for Firefox and other huge programs.

    10. #85
      Jellyfish, not jam.
      Join Date
      Dec 2008
      Gender
      Location
      Vancouver, BC, Canada
      Posts
      8
      Likes
      0
      When creating an array, is it possible to define all of its contents manually in one go? I know that in python, you can do
      Code:
      mylist = [item1, item2, item3]
      Can this be done in C++, or would I have to manually access each index, as in
      Code:
      int mylist[3];
      mylist[0] = item1;
      mylist[1] = item2;
      mylist[2] = item3;
      Thanks!
      And life is also too short to not try eating feces at least once. -Delilah

    11. #86
      khh
      khh is offline
      Remember Achievements:
      1000 Hall Points Veteran First Class
      khh's Avatar
      Join Date
      Jun 2009
      Gender
      Location
      Norway
      Posts
      2,482
      Likes
      1309
      Yeah, that's possible
      Code:
      int yourlist = { item1, item2, item3 };
      April Ryan is my friend,
      Every sorrow she can mend.
      When i visit her dark realm,
      Does it simply overwhelm.

    12. #87
      Jellyfish, not jam.
      Join Date
      Dec 2008
      Gender
      Location
      Vancouver, BC, Canada
      Posts
      8
      Likes
      0
      Sweeeeet. No idea why I couldn't find that in the tutorials I was using.

      Thanks!
      And life is also too short to not try eating feces at least once. -Delilah

    13. #88
      khh
      khh is offline
      Remember Achievements:
      1000 Hall Points Veteran First Class
      khh's Avatar
      Join Date
      Jun 2009
      Gender
      Location
      Norway
      Posts
      2,482
      Likes
      1309
      Ninja, do you know of any good assembly language tutorials? Know it's not a C/C++ question, but I figured you're the person to ask. See, I'll be learning it at the university next semester, so I'd like to get a jump on it. Being the most computer literate guy, I know some of my friends will ask me for help, so it'd be good to actually know some.
      April Ryan is my friend,
      Every sorrow she can mend.
      When i visit her dark realm,
      Does it simply overwhelm.

    14. #89
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      Yes, "The Art of Assembly" by Randall Hyde. You should be able to find it on Amazon.

    15. #90
      khh
      khh is offline
      Remember Achievements:
      1000 Hall Points Veteran First Class
      khh's Avatar
      Join Date
      Jun 2009
      Gender
      Location
      Norway
      Posts
      2,482
      Likes
      1309
      Ok, thanks. I'll check it out.
      April Ryan is my friend,
      Every sorrow she can mend.
      When i visit her dark realm,
      Does it simply overwhelm.

    16. #91
      Antagonist Achievements:
      1 year registered Veteran First Class Made lots of Friends on DV Referrer Bronze 10000 Hall Points
      Invader's Avatar
      Join Date
      Jan 2004
      Location
      Discordia
      Posts
      3,239
      Likes
      535
      I have a question about a piece of code that's supposed to convert a 32 bit number into its base 10 form. Each digit of the 32 bit number is represented as an array value:

      N[0] = 0
      N[1] = 1
      N[2] = 0

      ..and so on to N[31]. I use 'if' to check each array value and add the base ten value of that digit onto variable 'temp' (that I use for something else earlier) to end up with the base10 number.

      My puzzlement is due to the fact that the program only functions properly when the if statement uses negative logic, as below.

      Code:
      d=2147483648;
      for(k=0; k<32; ++k){
      	if(N[k]!=0){
      		temp = temp + d;
      	}
      	d/=2;
      }
      If however I use if(N[k]=1) then the program treats every array value (binary digit) as if it's 1, and I end up with 4294967295, or 232-1. This isn't the first time I've had to use a negative statement in order to get it to work. Bear in mind I'm still "fresh out of the box" new to c++ and don't know very much.

    17. #92
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      if(N[k]=1) is not doing what you think it is What your code is doing is assigning N[k] to 1, and then checking the result of that assignment (which is 1) hence, it's always true.

      You want if(N[k]==1)

      a = b; //assignment
      a == b; //comparison

    18. #93
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      Take a look for yourself

      Code:
      #include <iostream>
      
      int main(int, char **){
         int a = 5;
         int b;
         std::cout << b = a << std::endl;
      }

    19. #94
      Antagonist Achievements:
      1 year registered Veteran First Class Made lots of Friends on DV Referrer Bronze 10000 Hall Points
      Invader's Avatar
      Join Date
      Jan 2004
      Location
      Discordia
      Posts
      3,239
      Likes
      535
      Wow. That makes a good deal of sense now! Thanks ninja.

      Oh, and while I'm here, I don't suppose you'd know something about urls? What my lil' program did was convert an IP address (like google.com's 66.102.7.99) into four 8-bit binary numbers and then grouped them as a 32 bit number, and converted them back to base ten, which in this example's case would yield 1113982819. I can take that number and enter it as the url and it'll take me to google.com. When I try that for, say, dreamviews.com (IP 208.43.31.59) I get the number 3492486971. That number however returns a 400 error (bad request). I know zip about networking. Seems fascinating though that the numbers can be played with in ways the browser will still understand.

    20. #95
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      What are you doing to try an get those web pages? A browser won't let you do that. You using libcurl?

    21. #96
      Ex Tech Admin Achievements:
      Created Dream Journal Tagger First Class Veteran First Class 10000 Hall Points Populated Wall Referrer Gold Made lots of Friends on DV
      slash112's Avatar
      Join Date
      Nov 2008
      Gender
      Location
      Sunny Scotland
      Posts
      5,113
      Likes
      1567
      DJ Entries
      29
      So yea, this is nowhere near finished or anything, I'm just using this so I can get the loop working.
      It doesn't exit the loop when "done" is said on the first input, which is what I am trying to get to work.

      By the way, the code is for a thing I'm doing which has a shitload of links which need to be wrapped in "page[x]="link";"
      Just to make my life easier for it.

      So uhhh, what on earth is wrong with it? I cannot figure it out.


      Code:
      #include <iostream>
      #include <sstream>
      
      using namespace std;
      
      int main()
      {
      
      
          string link0;
          string link1;
          string link2;
      
          string codepart0;
          string codepart1;
          string codepart2;
      
      
          cout << "Enter links" << endl;
      
      while (link0 != "done")
      {
      
      
          cin >> link0;
          codepart0 = "page[0]=\"" + link0 + "\"" + ";";
      
          cin >> link1;
          codepart1 = "page[1]=\"" + link1 + "\"" + ";";
      
          cin >> link2;
          codepart2 = "page[2]=\"" + link2 + "\"" + ";";
      }
      
      cout << codepart0 << "\n" << codepart1 << "\n" << codepart2 << endl;
      
      
      
          return 0;
      }

    22. #97
      DuB
      DuB is offline
      Distinct among snowflakes DuB's Avatar
      Join Date
      Sep 2005
      Gender
      Posts
      2,399
      Likes
      362
      It's not exiting the loop because it's checking for the "done" string before it ever retrieves the user input in the body of the loop.

      In other words, at the point when it checks for "done" (i.e., just prior to entering the loop), link0 has been defined but nothing has been assigned to it yet because it hasn't reached cin. So it proceeds to run through the entire loop regardless of input received. It doesn't check for "done" again until after the loop restarts, i.e., after the 3rd cin.

      A simple solution is to do the first cin before entering the loop. That way it is checking a variable that has received an assignment before running through its normal course, giving it the chance to exit if it receives "done."

      Does that make sense?
      slash112 likes this.

    23. #98
      Ex Tech Admin Achievements:
      Created Dream Journal Tagger First Class Veteran First Class 10000 Hall Points Populated Wall Referrer Gold Made lots of Friends on DV
      slash112's Avatar
      Join Date
      Nov 2008
      Gender
      Location
      Sunny Scotland
      Posts
      5,113
      Likes
      1567
      DJ Entries
      29
      Ahhhhh, yea, that makes sense. Thanks.

      There ain't much I can do about that, because of the way I need to do the thing as a whole.

      Imma need to totally change my approach.

    24. #99
      Antagonist Achievements:
      1 year registered Veteran First Class Made lots of Friends on DV Referrer Bronze 10000 Hall Points
      Invader's Avatar
      Join Date
      Jan 2004
      Location
      Discordia
      Posts
      3,239
      Likes
      535
      Quote Originally Posted by ninja9578 View Post
      What are you doing to try an get those web pages? A browser won't let you do that. You using libcurl?
      I'm not using libcurl. I just ping the web address to get an IP and enter that manually into the program, which then converts a 32 bit number into base 10. 1113982819 will take you to google if you enter that into the url bar in Firefox, I assume it'd do the same thing in any browser.. Er, did I misunderstand the question?

    25. #100
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      Oh, well it doesn't do that in Safari. Your conversion was correct though.

    Page 4 of 5 FirstFirst ... 2 3 4 5 LastLast

    Bookmarks

    Posting Permissions

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