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


Groups > comp.lang.c > #109900

Re: What am I doing wrong here?

Subject Re: What am I doing wrong here?
Newsgroups comp.lang.c
References (2 earlier) <of6vp1$m8g$2@dont-email.me> <87o9uwnbcl.fsf@gmail.com> <of7774$f78$2@dont-email.me> <of7999$g8p$1@gioia.aioe.org> <of7g2h$dj9$3@dont-email.me>
From bartc <bc@freeuk.com>
Message-ID <gTHRA.74828$wa2.60906@fx36.am4> (permalink)
Organization virginmedia.com
Date 2017-05-13 19:03 +0100

Show all headers | View raw


On 13/05/2017 18:38, DFS wrote:

> 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),

Automatically? How does that work?

You examples use the size of name[0], which is a pointer, so will be 4 
or 8 bytes, then you seem to add an arbitrary number to that. It has 
little to do with the size of the largest string in the table, except it 
might just be big enough by chance.

  and there's no 'waste' like there would
> be if I used randName[100]?

You can choose any number you like that you know will be comfortably 
large enough. The space is local storage in a function, and will be 
recovered once you're back outside the function. Note that your machine 
will be probably have some multiple of 1000,000,000 bytes available.

However, I showed in my other post that you don't need to use sprintf at 
all so you don't even need to allocate 2 bytes.

> 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?


Probably, as you're effectively using random numbers for the size of the 
buffer.

>
>
> Also:
> given char *names[] = {"AA","B","CCCCXACCCC","D","E"};
>
> Why does sizeof(names[0]) return 8 instead of 9?

8 is the number of bytes of a pointer on your machine, as name[0] has a 
pointer type (char*).

But why would you expect 9? names[0] points to a string occupying 3 
bytes. The longest string (pointed to by names[2]) requires 11 bytes I 
think.

-- 
bartc

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