2
\$\begingroup\$

I am writing this program to a Microchip PIC.

 char * we_are_display = "hello";
 char ahimouz[2];
 sprintf(ahimouz,"%s %s",we_are_display,"5");
 WriteStringToLCD(ahimouz);

This program works despite the memory allocated to array ahimouz is only 2 bytes. Why is that so?

enter image description here

\$\endgroup\$
6
  • \$\begingroup\$ I think this is a programming question and hence it is off-topic. \$\endgroup\$
    – nidhin
    Jun 13, 2014 at 10:38
  • \$\begingroup\$ I would change char ahimouz[2] to char *ahimouz, try this. \$\endgroup\$ Jun 13, 2014 at 11:02
  • \$\begingroup\$ Dear Makouda, the message you got from StackOverflow might mean that you've asked too many low quality questions and the site has blocked your account because of that. So, it has nothing to do with this question being on-topic there or here. You have already asked a question that has 5 downvotes here. If you're not careful with the quality of your questions, you won't be able to ask questions here, too. \$\endgroup\$
    – Ricardo
    Jun 13, 2014 at 13:55
  • 3
    \$\begingroup\$ The question ban doesn't transfer from SO to this site. Moreover, I don't see anything wrong with this question: embedded programming is on topic. \$\endgroup\$
    – clabacchio
    Jun 13, 2014 at 15:42
  • 1
    \$\begingroup\$ @Electropepper - your comment is just plain wrong! Instead of an insufficiently sized array that the OP is using, you are instead proposing writing through an uninitialized pointer. You haven't allocated any memory to receive the output of sprintf! \$\endgroup\$
    – DoxyLover
    Jun 13, 2014 at 17:32

1 Answer 1

6
\$\begingroup\$

In your example you're not out of memory, you simply haven't allocated enough memory to hold the string. The remainder will end up in a memory area that's available to the processor but the compiler isn't aware of. While it may not cause a problem with this simple example in practice it can overwrite other variables and memory areas and should be avoided.

Also as Majenko mentioned in a comment you may want to take a look at snprintf which is a safer version to use because it also includes a parameter to specify the buffer length. Not all PIC compilers support it but if your one does the syntax would be:

snprintf(ahimouz, sizeof(ahimouz), "%s %s", we_are_display, "5");

That would prevent the possibility of other memory areas being overwritten, although it would also only display h because your buffer size is only large enough to hold a single character plus the null terminator.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ You may want to expand the answer to mention that snprintf() is preferred over sprintf() to prevent these kind of buffer overruns. \$\endgroup\$
    – Majenko
    Jun 13, 2014 at 10:54
  • \$\begingroup\$ @Majenko, good point, not sure that Microchip C8 supports it but no doubt others would. \$\endgroup\$
    – PeterJ
    Jun 13, 2014 at 11:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.