Click here to Skip to main content
15,890,609 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello friends,

I wonder that situation.
I have some codes like that.Like you see these codes include malloc and calloc ,and I never will use free() in MCU ,in this case is it ok to use these code in my project?

What I have tried:

<pre> 

for(int i=0;i<4096;i++)
   {
      m[i] = qSin(2*3.14*50*t) + sin(2*3.14*100*t) + 2*qrand()/(RAND_MAX);
      t = t + ts;
   }
   emxArray_real_T *Mshifted,*f; 

   emxInit_real_T(&Mshifted, 2);
   emxInit_real_T(&f, 2);
   emxEnsureCapacity((emxArray__common *)Mshifted, 0, (int32_T)sizeof(real_T));
   emxEnsureCapacity((emxArray__common *)f, 0, (int32_T)sizeof(real_T));
    Func1(m,ts,df,Mshifted,f);


void emxInit_real_T(emxArray_real_T **pEmxArray, int numDimensions)
{
  emxArray_real_T *emxArray;
  int i;
  *pEmxArray = (emxArray_real_T *)malloc(sizeof(emxArray_real_T));
  emxArray = *pEmxArray;
  emxArray->data = (double *)NULL;
  emxArray->numDimensions = numDimensions;
  emxArray->size = (int *)malloc((unsigned int)(sizeof(int) * numDimensions));
  emxArray->allocatedSize = 0;
  emxArray->canFreeData = true;
  for (i = 0; i < numDimensions; i++) {
    emxArray->size[i] = 0;
  }
}


void emxEnsureCapacity(emxArray__common *emxArray, int oldNumel, int elementSize)
{
  int newNumel;
  int i;
  void *newData;
  newNumel = 1;
  for (i = 0; i < emxArray->numDimensions; i++) {
    newNumel *= emxArray->size[i];
  }

  if (newNumel > emxArray->allocatedSize) {
    i = emxArray->allocatedSize;
    if (i < 16) {
      i = 16;
    }

    while (i < newNumel) {
      i <<= 1;
    }

    newData = calloc((unsigned int)i, (unsigned int)elementSize);
    if (emxArray->data != NULL) {
      memcpy(newData, emxArray->data, (unsigned int)(elementSize * oldNumel));
      if (emxArray->canFreeData) {
        free(emxArray->data);
      }
    }

    emxArray->data = newData;
    emxArray->allocatedSize = i;
    emxArray->canFreeData = true;
  }
}
Posted
Updated 16-Jul-18 11:36am
Comments
Dave Kreskowiak 16-Jul-18 16:25pm    
Your question isn't really answerable. We have no idea if the code is good for whatever purpose you intend to use it for.

But, if you're using malloc and calloc, but never frreing the memory, you're probably going to run the micro out of memory, or any other machine for that matter if the code runs long enough and your allocating memory inside a loop.
Emrah Duatepe 16-Jul-18 16:32pm    
I mean , malloc and calloc function cause a fragmentation if free() that memory.If I use these code unless free().Will I encounter any problem I wonder.
Dave Kreskowiak 16-Jul-18 16:39pm    
It's impossible to tell until you actually write code around this and test it in your own situation.

Just by looking at what you posted, there is no way anyone is going to be able to tell you if it's going to work in your own project.
Emrah Duatepe 17-Jul-18 12:50pm    
Okay ,thank you for information.

1 solution

You should not use code you "do not understand".

That is how you introduce malware to your system.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900