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


Groups > comp.lang.c > #109899

Re: What am I doing wrong here?

From DFS <nospam@dfs.com>
Newsgroups comp.lang.c
Subject Re: What am I doing wrong here?
Date 2017-05-13 13:38 -0400
Organization A noiseless patient Spider
Message-ID <of7g2h$dj9$3@dont-email.me> (permalink)
References (1 earlier) <ennoubFn8ntU1@mid.uni-berlin.de> <of6vp1$m8g$2@dont-email.me> <87o9uwnbcl.fsf@gmail.com> <of7774$f78$2@dont-email.me> <of7999$g8p$1@gioia.aioe.org>

Show all headers | View raw


On 5/13/2017 11:38 AM, Manfred wrote:
> On 05/13/2017 05:06 PM, DFS wrote:
>> On 5/13/2017 9:19 AM, Gareth Owen wrote:
>>> DFS <nospam@dfs.com> writes:
> <snip>
>>
>> Thanks Bart.
>>
>> What I intended to do from the start was not just print, but to put 
>> random char data into char variable(s), and random int data into int 
>> variable(s).  Then I could use the variables to build SQL strings or 
>> for other uses.
>>
>> This seems to work:
>> --------------------------------------------------------------------
>> #include <stdio.h>
>> #include <time.h>
>>
>> int main(int argc, char *argv[])
>> {
>>    int i;
>>    char randName[2];
>>    int  randRate=0;
>>    char randJob[11];
> 
> The size of these buffers needs to be thoroughly considered:
> randName: only if all names are guaranteed to be 1-letter names, and 
> stay that way, then a 2-char buffer is fine.
> randJob: in this example all jobs are max 10-letters long, so an 11-char 
> buffer works fine, but they are variable length names already now, what 
> if max length may change in future revisions? bart suggested a 100-char 
> buffer.


Yeah, but for this example I'm OK.

How about this?

----------------------------------------------------------
char *names[] = {"AA","B","CCCCXCCCC","D","E"};
char randName[sizeof(names[0])+1];
or
char randName[sizeof(names[0])+2];
or
char randName[sizeof(names[0])+5];
----------------------------------------------------------

Then the buffers are automatically sized to the largest value in the 
array (+1 or +2 or +5 whatever), and there's no 'waste' like there would 
be if I used randName[100]?

Output looks OK.

$ getrandoms 10
D, 50, architect
AA, 20, architect
B, 20, DBA
E, 20, analyst
B, 40, programmer
D, 30, analyst
AA, 30, architect
D, 50, analyst
CCCCXCCCC, 20, analyst
D, 40, contractor

Is there anything wrong with that approach to sizing?



I noticed something:
----------------------------------------------------------
given: char *names[] = {"AA","B","CCCCXCCCC","D","E"};

option
1. char randName[sizeof(names[0])+2];
2. char randName[sizeof(names[0])+1];
3. char randName[sizeof(names[0])];
4. char randName[sizeof(names[0])-1];
5. char randName[sizeof(names[0])-2];
----------------------------------------------------------

All options compile cleanly.

Option 1 always run fine.
Option 2 always run fine.
Option 3 always runs but sometimes outputs gibberish in the 3rd var 
(randJob)
Option 4 runs once or twice then crashes.
Option 5 crashes first run.

Are options 3,4,5 resulting in what's known as a buffer overflow?




Also:
given char *names[] = {"AA","B","CCCCXACCCC","D","E"};

Why does sizeof(names[0]) return 8 instead of 9?


Thanks

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


Thread

What am I doing wrong here? DFS <nospam@dfs.com> - 2017-05-13 00:39 -0400
  Re: What am I doing wrong here? DFS <nospam@dfs.com> - 2017-05-13 01:03 -0400
  Re: What am I doing wrong here? jt@toerring.de (Jens Thoms Toerring) - 2017-05-13 06:55 +0000
    Re: What am I doing wrong here? DFS <nospam@dfs.com> - 2017-05-13 08:59 -0400
      Re: What am I doing wrong here? bartc <bc@freeuk.com> - 2017-05-13 14:15 +0100
      Re: What am I doing wrong here? Gareth Owen <gwowen@gmail.com> - 2017-05-13 14:19 +0100
        Re: What am I doing wrong here? DFS <nospam@dfs.com> - 2017-05-13 11:06 -0400
          Re: What am I doing wrong here? Manfred <noname@invalid.add> - 2017-05-13 17:38 +0200
            Re: What am I doing wrong here? DFS <nospam@dfs.com> - 2017-05-13 13:38 -0400
              Re: What am I doing wrong here? bartc <bc@freeuk.com> - 2017-05-13 19:03 +0100
          Re: What am I doing wrong here? bartc <bc@freeuk.com> - 2017-05-13 16:43 +0100
      Re: What am I doing wrong here? Barry Schwarz <schwarzb@dqel.com> - 2017-05-13 07:44 -0700
        Re: What am I doing wrong here? DFS <nospam@dfs.com> - 2017-05-13 11:25 -0400
          Re: What am I doing wrong here? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-05-13 19:23 +0100
    Re: What am I doing wrong here? Keith Thompson <kst-u@mib.org> - 2017-05-13 13:58 -0700
      Re: What am I doing wrong here? jt@toerring.de (Jens Thoms Toerring) - 2017-05-13 23:43 +0000
        Re: What am I doing wrong here? Tim Rentsch <txr@alumni.caltech.edu> - 2017-05-14 13:43 -0700
  Re: What am I doing wrong here? DFS <nospam@dfs.com> - 2017-05-15 20:18 -0400

csiph-web