This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Variable not getting memory allocated

Hello members,

I have following code built successfully with MDK-ARM v4.7.

void FS_update_version_info (void)
{ char *file_src, *file_dest;

strcpy(&file_src[0], folder_path_config);
strcat(file_src, file_path_config);
strcpy(&file_dest[0], folder_path_config);
strcat(file_dest, file_path_config_temp);
}

When I debug the code, I found that variable "file_dest" is not getting memory allocated, hence it is not getting updated with the assigned value. I tried increasing stack/heap values from startup file. Also tried increasing IRAM1 option from Target->Options dialog but no use. Any idea what is the reason for this? I am using RL-TCP, RL-USB and RL-FlashFS libraries in my code.

Thanks in advance for any help.

Mike.

Parents
  • You have two auto variables located on the stack.

    That means that there have been space allocated for them on the stack.

    But since the two variables are of the data type pointer-to-char, it is only the pointers that have memory allocated.

    You (!) then need to allocate memory blocks and assign these two pointers to point at your allocated memory blocks, before you can start to use the pointers as destination for your string-copy.

    This is standard C.

    Memory doesn't get randomly allocated from the heap, unless the developer explicitly calls any function that performs heap allocation.

    strcpy() and strcat() only copies data - they both assume that the destination pointer already points to valid memory - your two pointers have never been assigned any valid address to valid buffer space for strcpy() and strcat() to place any data.

Reply
  • You have two auto variables located on the stack.

    That means that there have been space allocated for them on the stack.

    But since the two variables are of the data type pointer-to-char, it is only the pointers that have memory allocated.

    You (!) then need to allocate memory blocks and assign these two pointers to point at your allocated memory blocks, before you can start to use the pointers as destination for your string-copy.

    This is standard C.

    Memory doesn't get randomly allocated from the heap, unless the developer explicitly calls any function that performs heap allocation.

    strcpy() and strcat() only copies data - they both assume that the destination pointer already points to valid memory - your two pointers have never been assigned any valid address to valid buffer space for strcpy() and strcat() to place any data.

Children