-2

I'm new on C and embedded systems and I need to reallocate a variable type char which is an array by definition.

I have:

char payLoad[1];
chrNum = 16 + 
    strlen("\"message\":")          + 
    strlen(strMsg)                  +
    strlen(", \"status":")          +
    strlen(strStatus)               +
//here I need to realloc my payLoad to message size chrNum
// after using this information i need to come back the array to 1 again

Tried to use some examples as

  (realloc(payLoad, sizeof(char *) * chrNum))

But my program blocks in this line.

GIJOW
  • 2,307
  • 1
  • 17
  • 37
  • 2
    You allocated `payLoad` on the stack. So you can't do this. The example snippet is also not a [MCVE](http://stackoverflow.com/help/mcve). – Martin Zabel Feb 15 '16 at 21:48
  • I understand, I would like to have more code to post, but I'm lost – GIJOW Feb 15 '16 at 21:49
  • What I mean by a MCVE is, post a complete `main()` without any syntax errors. Just left the part free where you want to do the reallocation. – Martin Zabel Feb 15 '16 at 21:52
  • Embedded systems typically don't support dynamic memory allocation. Your question lacks more information than just a [mcve]. See [ask]. – too honest for this site Feb 15 '16 at 21:53
  • 2
    Realloc can be used to relocate the memory block whic was allocated in heap previously with call to calloc or malloc. – c_mnon Feb 15 '16 at 21:54
  • Does not even use `realloc` correctly (apart from invalid context). The return value is the new memory pointer. – Weather Vane Feb 15 '16 at 21:55
  • 1
    Unless your definition of an embedded system is "laptop PC" or some such, it probably doesn't make any sense for you at all to use realloc. Instead you should simply reserve a buffer large enough for the worst-case scenario. That way you'll save memory (no monstrous heap appearing in your RAM, slaughtering all your memory) and execution speed (no dynamic allocation calls). [See this](http://electronics.stackexchange.com/questions/171257/realloc-wasting-lots-of-space-in-my-mcu/171581#171581). – Lundin Feb 16 '16 at 10:27
  • Arm in the question is my architecture mcu. I don't have too much ram, payload as variable is called is because it will be sent by http. I need to know the content length, so a buffer larger enough can cause some problems about that. – GIJOW Feb 16 '16 at 11:50

1 Answers1

3

You have payload defined as a char array, and you can't resize an array defined at compile time. You need to define it as a pointer so you can dynamically allocate memory:

char *payLoad = NULL, *temp;
chrNum = 16 + 
    strlen("\"message\":")          + 
    strlen(strMsg)                  +
    strlen(", \"status":")          +
    strlen(strStatus);
temp = realloc(payLoad, chrNum + 1);
if (temp == NULL) {
    perror("realloc failed");
    exit(1);
}
payload = temp;
dbush
  • 205,898
  • 23
  • 218
  • 273
  • 1
    Oh my, another question which falls back to "why does an array not behave like a pointer". – too honest for this site Feb 15 '16 at 21:54
  • thanks. The error live temp = realloc(payLoad, chrNum + 1); and payLoad = temp; now. A value of type "void *" cannot be assigned to an entity of type "char" – GIJOW Feb 15 '16 at 22:03
  • @GIJOW that was a typo. `temp` should be `*temp`. – dbush Feb 15 '16 at 22:06
  • yes got it. Thank you very much to clarify. @Olaf, sorry to bother you but as is in my first line, I'm NEW on this – GIJOW Feb 15 '16 at 22:08
  • @MartinZabel Thanks. Fixed. – dbush Feb 15 '16 at 23:19
  • consider using `snprintf` (both to calculate the length, and do the printing), as well as being easier to read it will reduce the risk of a typo or other accidental mismatch between the calculation and the output – M.M Feb 15 '16 at 23:40
  • thanks you @M.M I'm studying it right in this moment. I think it can be a good solution. – GIJOW Feb 16 '16 at 01:10