cancel
Showing results for 
Search instead for 
Did you mean: 

sprintf causes hard fault.

Trevor Jones
Senior

I use "sprintf (string,"comment"); everywhere

now for some reason, I am getting a hardfault. in an innocuous print.

length += sprintf(String + length, "Received Packet from ");

length is only 15.

String is declared as

char String[256];

I guess it is using malloc,

How do we check the allocations are being cleared ?

I never use malloc anywhere, so its not me...

21 REPLIES 21

As you know, I'm not a big fan of the VS or GNU/GCC​

With heap analysis there are several approaches, one is to understand the linked list and allocation method hidden below the pointer handed to you, another ​is wrap the malloc/free adding your own structure, tracking size and alloc/free counts so you can identify imbalances, leaks and orphans. On the free side you want to track if the same memory allocation is freed more than once. And filling the released memory with an invalidating pattern liable to fault quickly, ie 0xCDCDCDCD, and be recognizable as such.

You'd need to look​ into and understand the implementation details of your chosen toolset.

The failure in other places suggests a deeper underlying issue.​

Lacking trace, add a lot of sanity checking to pointers, stack depth checks, ​and instrumentation, to confine the problem and identify failure conditions quickly so the culprits more readily pinpointed.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Trevor Jones
Senior

will check for VisualStudio's malloc implementation,

would like to work a function to check stack/heap levels / leaks and Freeing the same memory multiple times.