Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #76052

Re: how to do this?

From James Kuyper <jameskuyper@verizon.net>
Newsgroups comp.lang.c
Subject Re: how to do this?
Date 2015-11-14 19:07 -0500
Organization Self
Message-ID <5647CCC9.4060001@verizon.net> (permalink)
References <n2849g$a6p$1@dont-email.me> <daplreFi0ttU6@mid.individual.net> <n28f7r$pmq$1@dont-email.me> <5647C2C4.9070704@verizon.net> <n28g35$smg$1@dont-email.me>

Show all headers | View raw


On 11/14/2015 06:30 PM, bilsch01 wrote:
> On 11/14/2015 03:24 PM, James Kuyper wrote:
>> On 11/14/2015 06:16 PM, bilsch01 wrote:
>>>>
>>>>     // What goes in the last character?
>>>>
>>>
>>> There's EOF.  9*256=2304

Your existing code does nothing with the last character. Do you mean
that a future version of your program will put EOF in that location? If
so, why?
EOF is an error code returned by several functions when they reach the
end of a file or experience an I/O error. It is not something that you
normally have any reason to store in an array that's being read from a
file or written to a file. Reading a file into an array will not place
EOF into the array when the end of the file is read; writing from an
array containing EOF will not cause the output file to end at that location.

...
>> This is automatic allocation:
>> 	char vec1[9];
>> 	// work with vec1.
>>
>> This is dynamic allocation:
>> 	char *vec1 = malloc(9);
>> 	if(vec1)
>>          {
>>               // work with vec1
>>               free(vec1);
>>          }
>>
>> In general, automatic allocation is a lot simpler. Dynamic allocation
>> may be necessary if the amount of memory needed is large, because many
>> systems have far more memory available for dynamic allocation than for
>> automatic allocation. It's also more appropriate when there's a lot of
>> variation in the amount of memory that needs to be allocated. Neither of
>> those reasons apply in this case: 9 bytes is quite tiny, and it's also
>> fixed.
>>
> There was spurious trailing bullshit at the end of the output lines. 
> When I put in the malloc() that solved the problem.

That's to be expected, because your code fails to null-terminate the
buffer before passing it to strcmp(), strcpy(), and printf(), all of
which require that the strings they work on be null-terminated. Even
worse, you didn't allocate enough memory to store the terminating null
character.

I'm not sure why using malloc() would make a difference, but I've got an
idea: on many implementations, malloc() fills the allocated memory with
zeros, just like calloc() is required to do. That's because the system
utilities that make memory available always zero it out, usually as a
security measure. Also, if you request an allocation of 9 bytes, it's
quite likely that malloc() may have rounded that allocation up to 10,
12, or 16 bytes, and zeroed out the entire thing. In that case, your
strings get null terminated even though you code didn't do anything to
null terminate them. You should not rely upon this, because malloc() is
not required to zero out the memory, and you should also not count on
malloc() setting aside enough room for the terminating null, since you
failed to tell it to do so.

There's a simple solution to this problem: allocate enough space for the
entire string, including the terminating null character, using either

    char vec1[10];

or

    char *vec1 = malloc(10);

Then, after filling in the string

    memcpy(vec1, ivt1+i*9, 9);

null terminate it using the following statement:

    vec1[9] = '\0';

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

how to do this? bilsch01 <king621@comcast.net> - 2015-11-14 12:09 -0800
  Re: how to do this? Rosario19 <Ros@invalid.invalid> - 2015-11-14 21:49 +0100
  Re: how to do this? Ian Collins <ian-news@hotmail.com> - 2015-11-15 10:02 +1300
    Re: how to do this? Ian Collins <ian-news@hotmail.com> - 2015-11-15 10:17 +1300
    Re: how to do this? bilsch01 <king621@comcast.net> - 2015-11-14 15:16 -0800
      Re: how to do this? James Kuyper <jameskuyper@verizon.net> - 2015-11-14 18:24 -0500
        Re: how to do this? bilsch01 <king621@comcast.net> - 2015-11-14 15:30 -0800
          Re: how to do this? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2015-11-14 19:01 -0500
          Re: how to do this? James Kuyper <jameskuyper@verizon.net> - 2015-11-14 19:07 -0500
  Re: how to do this? Ian Collins <ian-news@hotmail.com> - 2015-11-15 11:26 +1300
    Re: how to do this? bilsch01 <king621@comcast.net> - 2015-11-14 15:07 -0800
      Re: how to do this? James Kuyper <jameskuyper@verizon.net> - 2015-11-14 18:17 -0500
      Re: how to do this? bilsch01 <king621@comcast.net> - 2015-11-14 15:25 -0800
        Re: how to do this? Ian Collins <ian-news@hotmail.com> - 2015-11-15 13:10 +1300
          Re: how to do this? James Kuyper <jameskuyper@verizon.net> - 2015-11-14 19:21 -0500
            Re: how to do this? Ian Collins <ian-news@hotmail.com> - 2015-11-15 13:24 +1300
        Re: how to do this? James Kuyper <jameskuyper@verizon.net> - 2015-11-14 19:12 -0500
          Re: how to do this? Ian Collins <ian-news@hotmail.com> - 2015-11-15 13:18 +1300
            Re: how to do this? James Kuyper <jameskuyper@verizon.net> - 2015-11-14 19:23 -0500
  Re: how to do this? bilsch01 <king621@comcast.net> - 2015-11-14 15:18 -0800

csiph-web