• Lucid Dreaming - Dream Views




    Results 1 to 25 of 102
    Like Tree3Likes

    Thread: Ask me about C / C++

    Hybrid View

    1. #1
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      Well, there is a better way to do it, it's always best to allocate memory that goes together all at once. Remember, using a C-cast you can change anything into anything else.

      Code:
      int** array = (int**)malloc(width * height * sizeof(int));
      Then to delete it simply:
      Code:
      free(array);


      Looking at your code at a quick glance, I don't see anything incorrect. I would run it through the debugger and put a breakpoint on each delete[]

      Also, it's good practice to explicity declare what type you're using.
      Code:
      unsigned n = 0; //not standard C++
      unsigned int n = 0;  //correct

    2. #2
      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
      Wow, quick reply

      Quote Originally Posted by ninja9578 View Post
      Well, there is a better way to do it, it's always best to allocate memory that goes together all at once. Remember, using a C-cast you can change anything into anything else.

      Code:
      int** array = (int**)malloc(width * height * sizeof(int));
      Then to delete it simply:
      Code:
      free(array);
      I have heard that it is good practise not to use the C functions in a C++ program, though I might have misinterpreted this. Would it be possible to use something like
      Code:
      int** array = static_cast<int**>(new int[width*height]);
      delete[] array;
      Also, how would that code know that I wanted a table of the dimentions width x height, and not height x width or something else entirely? I would want it to function identically to a static array that could be created with
      Code:
      int array[width][height];
      if width and height were constants.

      Quote Originally Posted by ninja9578 View Post
      Also, it's good practice to explicity declare what type you're using.
      Code:
      unsigned n = 0; //not standard C++
      unsigned int n = 0;  //correct
      Yeah, usually I define shorts like UINT in a header file. I did think that "unsigned" was standard C++ for "unsigned int", though, like "long" is standard for "long int". :p
      But since you have pointed out it isn't, I'll try and use fully qualified variable names from now on :p

      Quote Originally Posted by ninja9578 View Post
      Looking at your code at a quick glance, I don't see anything incorrect. I would run it through the debugger and put a breakpoint on each delete[]
      I just added a cout statement, and found that "delete[] table[0];" worked, and then it crashed on "delete[] table[1];" I tried having n count down from width-1 instead, but the result was the same, it crashed on the second deallocation.
      April Ryan is my friend,
      Every sorrow she can mend.
      When i visit her dark realm,
      Does it simply overwhelm.

    3. #3
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      No, using C functions is very common. C functions are very very fast. Don't use C headers. ie #include <ctime> instead of #include <time.h>

      Anyway, if I were you, I would write a structure to handle an array for you and a method to get specific items:

      Code:
      #include <iostream>
      
      struct Array {
      public:
          Array(unsigned int width, unsigned int height){
      	   w = width; h = height;
      	   array = (int*)malloc(width * height * sizeof(int));
          }
          ~Array(void){
      	   free(array);
          }
          inline int & at(unsigned int x, unsigned int y){
      	   assert(x < w);
      	   assert(y < h);
      	   return array[w * y + x];
          }
      private:
          int * array;
          unsigned int w, h;
      };
      
      int main(){
          Array test(100, 150);
          test.at(12, 43) = 15;
          std::cout << test.at(12, 43) << std::endl;
          return 0;
      }

    4. #4
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      Oops. I forgot a const at operator

      Code:
          inline const int & at(unsigned int x, unsigned int y) const {
      	   assert(x < w);
      	   assert(y < h);
      	   return array[w * y + x];
          }
      Did you get how everything in my code works? This is how arrays are done behind the scenes anyway and because it's just a wrapper and the only functions are inline, the compiler will probably optimize away the structure.

    5. #5
      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
      Mostly, yeah. But I don't get why you use assert. I thought the point of an assert statement was to halt program execution. Would it not be more suitable to use an if statement?

      Also, what does the const operators do in the last bit you posted? I have never learned to use them, except in variable declarations.
      April Ryan is my friend,
      Every sorrow she can mend.
      When i visit her dark realm,
      Does it simply overwhelm.

    6. #6
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      Yes, asserts halt operation, but only in debugging mode. In release mode, they are compiled out, so no checks are done. This helps you make your code correct before you release it.

      const at the end of a function means that object that it's being called on will not change. When variables are declared as const, it means that they can't be changed. So you can only call const functions.

      Code:
      #include <iostream>
      
      class foo {
      public:
          foo(void){};
          ~foo(void){};
          void Test(void) const {
      	   //not allowed to change anything in this class
      	   std::cout << "Test const" << std::endl;
      	   //bar = 1;  <- This is illegal
          }
          void Test(void){
      	   //can manipulate the object
      	   std::cout << "Test" << std::endl;
      	   bar = 1; //This is fine
          }
      private:
          int bar;
      };
      
      int main(){
          foo one;
          one.Test();  //foo is not const, so the non-const method will be called
          const foo two;
          two.Test();  //foo is const, so the const method is called
          
          return 0;
      }

    7. #7
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      const essentially means "read only" so
      operator [] lets you read and write to the class
      operator [] const lets you read from the class only

    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
    •