Home > Software design >  Overwriting global char array vs global char pointer in a loop in C
Overwriting global char array vs global char pointer in a loop in C

Time:10-19

I have an infinite while loop, I am not sure if I should use a char array or char pointer. The value keeps getting overwritten and used in other functions. With a char pointer, I understand there could be a memory leak, so is it preferred to use an array?

char *recv_data = NULL;

int main(){
.....
  while(1){
   .....
   recv_data = cJSON_PrintUnformatted(root);

  .....
  
  }
} 

or

char recv[256] = {0};

int main(){
.....
  while(1){
   .....
   strcpy(recv, cJSON_PrintUnformatted(root));

  .....
  
  }
} 

CodePudding user response:

The first version should be preferred.

  1. It doesn't have a limit on the size of the returned string.
  2. You can use free(recv_data) to fix the memory leak.

The second version has these misfeatures:

  1. The memory returned from the function can't be freed, because you never assigned it to a variable that you can pass to free().
  2. It's a little less efficient, since it performs an unnecessary copy.

CodePudding user response:

Based on how you used it, the cJSON_PrintUnformatted returns a pointer to a char array. Since there are no input arguments, it probably allocates memory inside the function dynamically. You probably have to free that memory. So you need the returned pointer in order to deallocate the memory yourself.

The second option discards that returned pointer, and so you lost your only way to free the allocated memroy. Hence it will remain allocated -> memroy leak.

But of course this all depends on how the function is implemented. Maybe it just manipulates a global array and return a pointer to it, so there is no need to free it.

CodePudding user response:

Indeed, the second version has a memory leak, as @Barmar points out.

However, even if you were to fix the memory leak, you still can't really use the first version of your code: With the first version, you have to decide at compile-time what the maximum length of the string returned by cJSON_PrintUnformatted(). Now,

  • If you choose a value that's too low, the strcpy() function would exceed the array bounds and corrupt your stack.
  • If you choose a value that's so high as to be safe - you might have to exceed the amount of space available for your program's stack, causing a Stack Overflow (yes, like the name of this site). You could fix that using a strncpy(), giving the maximum size - and then what you'd have is a truncated string.

So you really don't have much choice than using whatever memory is pointed to by the cJSON_PrintUnformatted()'s return value (it's probably heap-allocated memory). Plus - why make a copy of it when it's already there for you to use? Be lazy :-)

PS - What should really happen is for the cJSON_PrintUnformatted() to take a buffer and a buffer size as parameters, giving its caller more control over memory allocation and resource limits.

  • Related