Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > strlen() and segfault?

Reply
Thread Tools

strlen() and segfault?

 
 
Zach
Guest
Posts: n/a
 
      02-14-2007
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[i]);
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

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      02-14-2007
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.
 
Reply With Quote
 
 
 
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      02-14-2007
Zach <(E-Mail Removed)> 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[i]);
> 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.
 
Reply With Quote
 
attn.steven.kuo@gmail.com
Guest
Posts: n/a
 
      02-14-2007
On Feb 13, 4:59 pm, "Zach" <(E-Mail Removed)> 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[i]);
> 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[i]);

return EXIT_SUCCESS;

}

--
Hope this helps,
Steven

 
Reply With Quote
 
Zach
Guest
Posts: n/a
 
      02-14-2007
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

 
Reply With Quote
 
Zach
Guest
Posts: n/a
 
      02-14-2007
On Feb 13, 11:44 pm, Christopher Benson-Manica
<(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
if and and vs if and,and titi VHDL 4 03-11-2007 05:23 AM



Advertisments