• Lucid Dreaming - Dream Views




    Results 1 to 25 of 165

    Hybrid View

    1. #1
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      Quote Originally Posted by Ynot View Post
      There's certain things that are the "right way" in C, but the "wrong way" in C++ (and vice versa)
      Arrays being the biggy
      Arrays are exactly the same in C and C++.

      He's right about the right and wrong ways being different in C and C++

      Critical section in C
      Code:
      void CriticalMethod(void){
         pthread_mutex_lock(somelock);
         //some critical stuff
         if (boolean){
            pthread_mutex_unlock(somelock);  //have to unlock in every exit path
            return;
         }
         //more critical stuff
         pthread_mutex_unlock(somelock);
      }
      Critical section in C++
      Code:
      //Autolock.h
      struct AutoLock{
         public:
            AutoLock(pthread_mutex_t * lock){
               pthread_mutex_lock(lock);
               mylock = lock;
            }
            ~AutoLock(void){
               pthread_mutex_unlock(mylock);
            }
         private:
            pthread_mutex_t mylock;
      }
      Code:
      void CriticalMethod(void){
         AutoLock lock(somelock);  //will automatically unlock when it returns
         //some critical stuff
         if (boolean){
            return;
         }
         //More critical stuff
      }

      ***********************


      What exactly would you like to know guitarboy, honoured you came to me

      I learned C first, then taught myself C++. I would recommend just learning C++, most programming is done in C++, and it's a lot easier. It's also a good thing to understand object oriented programming.

      Here's some things to understand, in order
      Primitive types: int, long, float, char
      Scope
      methods and functions
      pass-by-reference vs pass-by-value
      const
      Pointers
      Objects (classes and structs)
      Constructors and destructors

      Feel free to IM me about anything
      Last edited by ninja9578; 01-08-2010 at 01:00 AM.

    2. #2
      FBI agent Ynot's Avatar
      Join Date
      Oct 2005
      Gender
      Location
      Southend, Essex
      Posts
      4,337
      Likes
      14
      Quote Originally Posted by ninja9578 View Post
      Arrays are exactly the same in C and C++.
      I know they are the same
      but they're hailed in C, and frowned upon in C++

      From the man himself
      http://www2.research.att.com/~bs/bs_faq2.html#arrays
      (\_ _/)
      (='.'=)
      (")_(")

    3. #3
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      Oh, okay, you meant that in C++ you're supposed to use STL containers We still use arrays for various things though.

    4. #4
      Gentlemen. Ladies. slayer's Avatar
      Join Date
      Mar 2007
      Gender
      Location
      Right here... Reputation: 9999
      Posts
      4,902
      Likes
      473
      DJ Entries
      4
      Actually I have a question...

      I'm learning C++ and right now I'm reading about ++ and --

      The example it gives me is this...

      Example 1:
      Code:
      B=3;
      A=++B;
      // A contains 4, B contains 4
      Example 2:
      Code:
      B=3;
      A=B++;
      // A contains 3, B contains 4
      I don't really understand the whole ++ and -- and how it affects the A and B variables. Can someone explain how it adds up?

      Edit: Also, how does all this make up a video game engine?
      Last edited by slayer; 01-08-2010 at 01:41 AM.

    5. #5
      FBI agent Ynot's Avatar
      Join Date
      Oct 2005
      Gender
      Location
      Southend, Essex
      Posts
      4,337
      Likes
      14
      increment and decrement
      (Ie. add one, or subtract one)
      used all the time when looping and keeping count of things

      a++ means "return the value of a, and then increment it"
      ++a means "increment a, then return the value"

      subtle (but important) differences, however the differences between the pre & post incrementation are only important if you care about the value of a variable mid-operation

      Code:
      for (i = 0; i < max; i++)
      {
      
      }
      and
      Code:
      for (i = 0; i < max; ++i)
      {
      
      }
      are identical in operation
      (because we do not care about the value of 'i' mid-operation

      however,
      say we are keeping a count of something that we later need to step back through in reverse

      Code:
      void increment_something(int var)
      {
          old_var = var++;
      }
      here, we do care about the value of var mid-operation, therefore we store the "old value" of var and increment var

      You can of course do this
      Code:
      void increment_something(int var)
      {
          old_var = var;
          var++;
      }
      then we've split the operation into two
      and now we no longer care about the return value of the increment
      (\_ _/)
      (='.'=)
      (")_(")

    6. #6
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      Quote Originally Posted by Ynot View Post
      Code:
      for (i = 0; i < max; i++)
      {
      
      }
      and
      Code:
      for (i = 0; i < max; ++i)
      {
      
      }
      are identical in operation
      (because we do not care about the value of 'i' mid-operation
      Almost correct. In for loops you want to do the latter because in order to return, then increment, it has to keep a copy of it. Not a problem with ints, but with objects it can.

      ++ and -- are weird to learn.

      b = ++a; is like this:
      a = a + 1;
      b = a;

      b = a++; is like this:
      b = a;
      a = a + 1;


    7. #7
      Gentlemen. Ladies. slayer's Avatar
      Join Date
      Mar 2007
      Gender
      Location
      Right here... Reputation: 9999
      Posts
      4,902
      Likes
      473
      DJ Entries
      4
      Err...you're using some things in there I haven't learned yet. So I think you just made it more confusing for me...

      Edit: Ok that makes it a little easier to understand...

      So would it be something like this for the 2nd part?

      b = 1;
      b = a++;

      Would the result be: b = 1, a = 2? Because I already defined b to equal 1 and then a equals b which is 1, then I add a to a to get 2, right?
      Last edited by slayer; 01-08-2010 at 02:08 AM.

    8. #8
      FBI agent Ynot's Avatar
      Join Date
      Oct 2005
      Gender
      Location
      Southend, Essex
      Posts
      4,337
      Likes
      14
      follow a tutorial on C++
      I've always liked this one
      http://www.cprogramming.com/tutorial/lesson1.html

      It'll take you through the basics, and slowly build up to more complex things

      *edit*
      Just realised you're not the OP.....
      (\_ _/)
      (='.'=)
      (")_(")

    9. #9
      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
      Edit: Never mind. Note to self: Learn to see if there are more pages.

      But yeah, I also support you doing some smaller projects first.
      Last edited by khh; 01-09-2010 at 05:10 PM.
      April Ryan is my friend,
      Every sorrow she can mend.
      When i visit her dark realm,
      Does it simply overwhelm.

    10. #10
      Generic lucid dreamer Seeker's Avatar
      Join Date
      Oct 2003
      Gender
      Location
      USA
      Posts
      10,790
      Likes
      104
      Have you given any thought yet to C#?

      C is pretty much dead except for embedded solutions, C++ is still a great language and supports object oriented design, but with modern hardware, all but the most time critical tasks can be handled with C#. You also have the ability to call C++ unmanaged code from within wrappers if you have a section that needs to execute at it's fastest.

      My suggestion for a compiler is Visual Studio 2005 or 2008 (2003 is OK, but doesn't support the .NET 2.0 framework well). I've been using the Microsoft Cxx compilers since they were introduced and really like it. In fact I'm using VS2005 as we speak.
      you must be the change you wish to see in the world...
      -gandhi

    11. #11
      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
      Quote Originally Posted by Seeker View Post
      Have you given any thought yet to C#?
      C# is generally just used for Windows exclusive applications, though, and they require the .NET network to run. It's harder to write cross-platform applications. It's not as elegant at making native applications, though you're saved from hunting for external libraries or writing system-call wrappers yourself.

      Quote Originally Posted by Seeker View Post
      C is pretty much dead except for embedded solutions, C++ is still a great language and supports object oriented design, but with modern hardware, all but the most time critical tasks can be handled with C#. You also have the ability to call C++ unmanaged code from within wrappers if you have a section that needs to execute at it's fastest.
      I thought C was commonly used for time-critical operations still, such as graphics modules.

      Quote Originally Posted by Seeker View Post
      My suggestion for a compiler is Visual Studio 2005 or 2008 (2003 is OK, but doesn't support the .NET 2.0 framework well). I've been using the Microsoft Cxx compilers since they were introduced and really like it. In fact I'm using VS2005 as we speak.
      I do however agree that Visual Studio is an excellent set of coding tools. I use VS2008 professional myself (no, it's not pirated... but neither is it bought).
      April Ryan is my friend,
      Every sorrow she can mend.
      When i visit her dark realm,
      Does it simply overwhelm.

    12. #12
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      Even unsafe C# code doesn't run as quickly as C++.

      It's also very heavily dependent on the Windows runtime libraries. I think C# is an atrocious attempt at making C++ more like Java. C and it's derivatives are not meant to be safe, they're meant to do exactly what you tell them to do.

    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
    •