2021年3月22日星期一

C allocation - why is visual studio 2019 asking me to allocate memory and codeBlocks is not?

I'm trying to learn to use Visual Code 2019. My problem is that when I run a code I get this error message, "expected constant expression" and when I run the same code in CodeBlocks the function works with no problem.

Doing a google search I learned that I need to allocate memory for this line of code monster temp[l2 + h2];

My questions are how do I malloc this line? and why is visual studio 2019 asking me to allocate memory and codeBlocks is not?

void m_s_m(monster *list, int l1, int h1, int l2, int h2, int *comparisons, int *copies, int *block_copies, int *mallocs, int use_name, int use_weight)  {      monster temp[l2+h2];      int index = l1;      int k=l1;      (*mallocs)++;      (*block_copies)++;      while(l1<=h1 && l2<=h2)      {          (*comparisons)++;          if(use_name == 1)          {              if(strcmp(list[l1].name, list[l2].name)<0)              {                  (*copies)++;                  temp[index] = list[l1];                  l1 = l1++;              }              else              {                  (*copies)++;                  temp[index] = list[l2];                  l2 = l2++;              }          }          if(use_weight == 1)          {              if(list[l1].weight<list[l2].weight)              {                  (*copies)++;                  temp[index] = list[l1];                  l1 = l1++;              }              else              {                  (*copies)++;                  temp[index] = list[l2];                  l2 = l2++;              }          }          index++;      }      if(l1>h1)      {          (*block_copies)++;          while(l2<=h2)          {              (*copies)++;              (*comparisons)++;              temp[index] = list[l2];              index++;              l2++;          }      }      else      {          (*block_copies)++;          while(l1<=h1)          {              (*copies)++;              (*comparisons)++;              temp[index] = list[l1];              index++;              l1++;          }      }      (*block_copies)++;      while(k<index)      {          (*copies)++;          list[k]=temp[k];          k++;      }    }    
https://stackoverflow.com/questions/66756988/c-allocation-why-is-visual-studio-2019-asking-me-to-allocate-memory-and-codebl March 23, 2021 at 11:45AM

没有评论:

发表评论