strlen() and segfault?

Discussion in 'C Programming' started by Zach, Feb 14, 2007.

  1. Zach

    Zach Guest

    Wrote simple program to setup a char array of pointers and print each
    one. It runs with the expected output however it then causes a
    segmentation fault. Could someone take a lot at the code? My debugging
    is below as well. Should I have malloc'd memory for each of the
    strings? The examples I've studies so far from K&R and K&A didn't
    indicate that was needed. I haven't learned malloc yet anyways :)

    Zach

    #include <stdio.h>
    #include <stdlib.h>

    #define NUMSTRINGS 3

    int main (void)
    {

    char *strings[NUMSTRINGS] = {
    "Testing 1",
    "Testing 2",
    "Testing 3"
    };

    int i;

    for (i=0; i <= NUMSTRINGS; i++) {
    printf("%s", strings);
    printf("\n");
    }

    return(EXIT_SUCCESS);
    }

    zu22@netrek:~/src/testing$ gcc -g char-array2.c
    zu22@netrek:~/src/testing$ gdb ./a.out
    GNU gdb 6.4.90-debian
    Copyright (C) 2006 Free Software Foundation, Inc.
    GDB is free software, covered by the GNU General Public License, and
    you are
    welcome to change it and/or distribute copies of it under certain
    conditions.
    Type "show copying" to see the conditions.
    There is absolutely no warranty for GDB. Type "show warranty" for
    details.
    This GDB was configured as "i486-linux-gnu"...Using host libthread_db
    library "/lib/libthread_db.so.1".

    (gdb) run
    Starting program: /home/zu22/src/testing/a.out
    Testing 1
    Testing 2
    Testing 3

    Program received signal SIGSEGV, Segmentation fault.
    0x400959db in strlen () from /lib/libc.so.6
    (gdb) bt
    #0 0x400959db in strlen () from /lib/libc.so.6
    #1 0x4006b327 in vfprintf () from /lib/libc.so.6
    #2 0x40070cd3 in printf () from /lib/libc.so.6
    #3 0x080483da in main () at char-array2.c:19
    (gdb) quit
    The program is running. Exit anyway? (y or n) y
     
    Zach, Feb 14, 2007
    #1
    1. Advertising

  2. Zach

    Ian Collins Guest

    Zach wrote:
    > Wrote simple program to setup a char array of pointers and print each
    > one. It runs with the expected output however it then causes a
    > segmentation fault. Could someone take a lot at the code? My debugging
    > is below as well. Should I have malloc'd memory for each of the
    > strings? The examples I've studies so far from K&R and K&A didn't
    > indicate that was needed. I haven't learned malloc yet anyways :)
    >
    > Zach
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > #define NUMSTRINGS 3
    >
    > int main (void)
    > {
    >
    > char *strings[NUMSTRINGS] = {
    > "Testing 1",
    > "Testing 2",
    > "Testing 3"
    > };
    >
    > int i;
    >
    > for (i=0; i <= NUMSTRINGS; i++) {


    Should be < NUMSTRINGS. The array is indexed from 0 to NUMSTRINGS-1.

    --
    Ian Collins.
     
    Ian Collins, Feb 14, 2007
    #2
    1. Advertising

  3. Zach <> wrote:

    > Wrote simple program to setup a char array of pointers and print each
    > one.


    Plus an extra one that doesn't exist...

    > char *strings[NUMSTRINGS] = {
    > "Testing 1",
    > "Testing 2",
    > "Testing 3"
    > };


    > for (i=0; i <= NUMSTRINGS; i++) {
    > printf("%s", strings);
    > printf("\n");
    > }


    What happens when i == NUMSTRINGS? strings is indexed from 0 to
    NUMSTRINGS-1, so you meant

    for (i=0; i < NUMSTRINGS; i++) {

    You either learned something or are kicking yourself; hopefully the
    former (from someone who's done his share of the latter).

    --
    C. Benson Manica | I *should* know what I'm talking about - if I
    cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Feb 14, 2007
    #3
  4. Zach

    Guest

    On Feb 13, 4:59 pm, "Zach" <> wrote:
    > Wrote simple program to setup a char array of pointers and print each
    > one. It runs with the expected output however it then causes a
    > segmentation fault. Could someone take a lot at the code? My debugging
    > is below as well. Should I have malloc'd memory for each of the
    > strings? The examples I've studies so far from K&R and K&A didn't
    > indicate that was needed. I haven't learned malloc yet anyways :)
    >
    > Zach
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > #define NUMSTRINGS 3
    >
    > int main (void)
    > {
    >
    > char *strings[NUMSTRINGS] = {
    > "Testing 1",
    > "Testing 2",
    > "Testing 3"
    > };
    >
    > int i;
    >
    > for (i=0; i <= NUMSTRINGS; i++) {
    > printf("%s", strings);
    > printf("\n");
    > }
    >
    > return(EXIT_SUCCESS);
    >
    > }
    >


    (snipped)



    As others have told you: access is
    outside the bounds of array string
    when i == NUMSTRINGS.


    One might consider using an initializer
    list to determine the size of the array.
    In this way one can add to or take
    away from the initializer list without
    having to make other updates to the code.
    E.g.,

    int main (void)
    {
    char *string [] = {
    "Testing 1",
    "Testing 2",
    "Testing 3",
    };

    size_t i;

    for (i = 0; i < sizeof string / sizeof string[0]; ++i)
    printf("%s\n", string);

    return EXIT_SUCCESS;

    }

    --
    Hope this helps,
    Steven
     
    , Feb 14, 2007
    #4
  5. Zach

    Zach Guest

    Thanks for the responses. I'll make sure I remember array indexes in C
    run from
    0 to (LENGTH - 1) and not 1 to LENGTH :)

    Zach
     
    Zach, Feb 14, 2007
    #5
  6. Zach

    Zach Guest

    On Feb 13, 11:44 pm, Christopher Benson-Manica
    <> wrote:
    >
    > You either learned something or are kicking yourself; hopefully the
    > former (from someone who's done his share of the latter).


    Yes I definitely learned my lesson. :) Thanks to all who responded.

    Zach
     
    Zach, Feb 14, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    82
    Views:
    1,858
    Douglas A. Gwyn
    Sep 29, 2004
  2. No Such Luck

    Problem with character string loop and strlen()

    No Such Luck, Jul 7, 2005, in forum: C Programming
    Replies:
    9
    Views:
    434
    Joe Wright
    Jul 10, 2005
  3. segfault strlen

    , Sep 28, 2006, in forum: C++
    Replies:
    10
    Views:
    626
    Ron Natalie
    Sep 29, 2006
  4. segfault strlen

    , Sep 28, 2006, in forum: C++
    Replies:
    2
    Views:
    418
    peter koch
    Sep 28, 2006
  5. Andrey Vul
    Replies:
    8
    Views:
    714
    Richard Bos
    Jul 30, 2010
Loading...

Share This Page