• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Continuing: C++ help because im terrible at it :(

  • Thread starter Thread starter CU
  • Start date Start date

CU

Platinum Member
Code:
int count_char(const char *stringToSearch, int letterCounts[26])
{
   for (size_t i = 0; stringToSearch[i]; ++i)
   {
      // Convert the charactors to their uppercase equivelents.
      int val = toupper(stringToSearch[i]);
      letterCounts[val - 'A'] += 1;
   }
   return 0;
}

OK ignoring that it went overboard for a trivial C++ assignment. I wanted to try and clarify something. Having int val = ... inside the loop is the same as having it declared before the loop only because it is a basic type of a fixed size. If val was some class that allocated memory this would not be the case because I believe val would be constructed and destructed each pass through the loop, thus slowing it down. That being the case would it not be better to always define the class before the loop in case later val gets changed from int to myCoolInt class or something.

Code:
class Test{
  public:
    Test(int i) {c = new char[i];}
    ~Test() {delete[] c;}
    int x;
    char* c;
};

int main()
{
  for(int i=0; i<5; ++i)
  {
    Test t(10);
    t.x = i;
  }
  return 0;
}

vs

Code:
class Test{
  public:
    Test(int i) {c = new char[i];}
    ~Test() {delete[] c;}
    int x;
    char* c;
};

int main()
{
  Test t(10);
  for(int i=0; i<5; ++i)
  {
    t.x = i;
  }
  return 0;
}

Looking at the assembly from the above seems to support what I said.
 
Code:
int count_char(const char *stringToSearch, int letterCounts[26])
{
   for (size_t i = 0; stringToSearch[i]; ++i)
   {
      // Convert the charactors to their uppercase equivelents.
      int val = toupper(stringToSearch[i]);
      letterCounts[val - 'A'] += 1;
   }
   return 0;
}

OK ignoring that it went overboard for a trivial C++ assignment. I wanted to try and clarify something. Having int val = ... inside the loop is the same as having it declared before the loop only because it is a basic type of a fixed size. If val was some class that allocated memory this would not be the case because I believe val would be constructed and destructed each pass through the loop, thus slowing it down. That being the case would it not be better to always define the class before the loop in case later val gets changed from int to myCoolInt class or something.

Code:
class Test{
  public:
    Test(int i) {c = new char[i];}
    ~Test() {delete[] c;}
    int x;
    char* c;
};

int main()
{
  for(int i=0; i<5; ++i)
  {
    Test t(10);
    t.x = i;
  }
  return 0;
}

vs

Code:
class Test{
  public:
    Test(int i) {c = new char[i];}
    ~Test() {delete[] c;}
    int x;
    char* c;
};

int main()
{
  Test t(10);
  for(int i=0; i<5; ++i)
  {
    t.x = i;
  }
  return 0;
}

Looking at the assembly from the above seems to support what I said.

You are correct, a classes constructor would have to be used every time the loop iterates through. The stack memory would only be allocated once, though, heap memory would not be. This could be good or bad depending on what what you are shooting for. If you need to, for example, have the class setup in a certain way at the beginning of each loop, this could be good and desirable. On the other hand, if you are only using 1 or 2 functions in the class and don't care about the state of the class, it may be a bad thing.

Now, for an arrays that are used only in the array, they certainly should be allocated within the array. The stack space is only allocated once and deallocated at the end of the loop, which is what you want. Moving the array to the outside of the function keeps it alive, potentially needlessly, for the rest of the functions life (not that allocating stack space takes THAT much time.) The real reason to avoid stack allocations is that stack space is more limited than Heap space (~1MB in windows and linux).

So, in upperlevel function calls (main()) you should probably avoid allocating stack space as much as possible.
 
Just do

Code:
int count_char(const char *stringToSearch, int letterCounts[26])
{
   for (size_t i = 0; stringToSearch[i]; ++i)
   {
      // Convert the charactors to their uppercase equivelents.
      letterCounts[toupper(stringToSearch[i]) - 'A'] += 1;
   }
   return 0;
}

or give val some meaningful name so there's some purpose to separating it out like that (meaningful names are your friend if you ever have to go back to your code at a later date). Also, note that if you declare val inside the loop it won't exist outside of the loop (only matters if you need it outside the loop).
 
Back
Top