This is the mail archive of the ecos-devel@sources.redhat.com mailing list for the eCos project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: strcat breaks printf


Curtis Whitley wrote:

One other detail. Normally in C/C++, compiled text strings are automatically
null-terminated.
The code below contains this:  ";%d:%f:%f\0"
It could probably be written as: ";%d:%f:%f"
which would save a byte of "precious" memory!

That is correct.




One exception to this rule (if I recall correctly) is this (note array sizes in brackets):

static const char is_null_terminated[11]     = { "0123456789" };
static const char is_not_null_terminated[10] = { "0123456789" };

Please correct me if I am wrong on this!!

You are correct - kindof. By fixing the size you are pre-allocating how much static memory each will occupy. These are treated more as static arrays than as strings. So
static const char is_null_terminated[20] = { "0123456789" };
will in fact occupy 20 characters, the last 10 being null as most compilers will pad the remainder of the fixed-size array with zeros. Hence you achieve the effect of null terminating the "strings". I don't know if ANSI standards guarantee this for compliant compilers.


-- Alex





-----Original Message----- From: ecos-devel-owner@sources.redhat.com [mailto:ecos-devel-owner@sources.redhat.com]On Behalf Of Alex Schuilenburg Sent: Monday, August 16, 2004 7:03 PM To: wayneg@ananzi.co.za Cc: ecos-devel@sources.redhat.com Subject: Re: strcat breaks printf


Wayne Gemmell wrote:



Hi all

I am writing my first embedded software on ecos. I am also new to C I'm a

REAL


newb. My problem is with the following code...

<code>
smsBody = malloc(161);
       tmpStr = malloc(100);
       reset_log_read();
       smsBody = "tcc";


This is your problem.  You are setting smsBody to point to a static
string and throwing away the malloc'ed memory.  What you want is
           strcpy(smsBody,"tcc");



       puts("Hello");
       for (j=0;j < 1;j++)
       {
               tmpTime = rec->timestamp;
       puts("Hello");
               sprintf(tmpStr,";%d:%f:%f\0",(unsigned int) tmpTime,
(float)rec->la,(float) rec->lo);
               printf("tmp : %s\n",tmpStr);
               strcat(smsBody,tmpStr);


This is where you overwrite the contents of what is in memory after the
static "tcc" string.

-- Alex





}

       puts("Hello");
</code>

And my output is as follows...

Hello
Hello
tmp : ;78453:-2614.993896:2822.247070
78453:-2614.993896:2822.247070
707078453:-2614.993896:2822.247070

This is the smsBody output from further on in the program.
tcc;78453:-2614.993896:2822.247070
So the above seems to work. I just don't understand the garbage output
whenever I use puts or printf.

Could anyone give me any pointers?

Regards
Wayne Gemmell






--
Managing Director / CEO                              eCosCentric Limited
http://www.ecoscentric.com/                 The eCos and RedBoot experts
Phone: +44 1223 291156                            Cell:  +44 7811 364531
Email: alexs@ecoscentric.com                       Fax:  +44 7779 032911

Confidentiality: The information in this e-mail and any attachment is
confidential. It is intended only for the named recipient(s). If you
are not a named recipient, please notify the sender immediately and
do not read, use, copy or disseminate this information.

Conditions: Any offer contained within this communication is subject
to contract and formal approval by the legal entity giving the offer.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]