2024-03-12 09:07 AM
Dear Sirs,
I want to edit a string, but the system goes to hard fault (bus fault Imprecise error).
Here below the code:
typedef struct CC_PACKED
{
...
const char *name;
...
} _objd;
static const char acName2005_19[] = "Cycle 0 delta stop";
const _objd SDO2005[] =
{....
{0x19, DTYPE_UNSIGNED32, 32, ATYPE_RW | ATYPE_RXPDO, acName2005_19, 0, 0, MAX_U32, 0, &Modbus_register, C_2005_2006_x_R, C_2005_2006_x_W},
};
in a separate function, since the name is const, I transfer the string to the RAM memory and I try to edit it:
const _objd* objd = ....;
char* s;
u8 name_length = strlen(objd->name) + 1;
s = (char*)malloc(name_length);
strcpy(s, (objd + nsub)->name);
the strcpy sends me in hard fault.
Anybody can please help?
2024-03-12 01:03 PM - edited 2024-03-12 01:04 PM
With method #2, where it fails? at which index?
Perhaps your malloc() is broken (allocates from wrong memory). Try to copy to a static array.
2024-03-12 01:18 PM - edited 2024-03-12 01:28 PM
Still not working :thinking_face:..
I removed the free() at the end of the function, and it seemed to work.
It worked only once, the second time I test the code, it crashes. Maybe because the heap gets full?!
2024-03-14 01:28 AM
Dear All,
thank you for your help.
I removed the malloc and I used an array of char with fixed size and all is working fine.
I use 4 kB of heap and 4 kB of stack.
Total SRAM memory is 64 kB.
Maybe I have some problem with memory allocation and it does not allow me to allocate it dynamically..
2024-03-14 02:33 AM
Looks so. 4K heap, and 64KB total, with the GNU library isn't serious.