strlen() and segfault?

Z

Zach

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
 
I

Ian Collins

Zach said:
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.
 
C

Christopher Benson-Manica

Zach said:
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).
 
A

attn.steven.kuo

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;

}
 
Z

Zach

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
 
Z

Zach

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,039
Messages
2,570,376
Members
47,027
Latest member
PilarRitch

Latest Threads

Top