473,320 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

strlen() and segfault?

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

Feb 14 '07 #1
5 7510
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.
Feb 14 '07 #2
On Feb 13, 4:59 pm, "Zach" <net...@gmail.comwrote:
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

Feb 14 '07 #3
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

Feb 14 '07 #4
Zach <ne****@gmail.comwrote:
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.
Feb 14 '07 #5
On Feb 13, 11:44 pm, Christopher Benson-Manica
<a...@otaku.freeshell.orgwrote:
>
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

Feb 14 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Nathaniel Echols | last post by:
I've written a function in C to perform protein sequence alignment. This works fine in a standalone C program. I've added the necessary packaging to use it in Python; it returns three strings and...
6
by: Stefan Behnel | last post by:
Hi! In Python 2.4b3, the deque is causing a segfault on two different machines I tested on. With deque, my program runs fine for a while (at least some tens of seconds up to minutes) and then...
0
by: dale | last post by:
Python newbie disclaimer on I am running an app with Tkinter screen in one thread and command-line input in another thread using raw_input(). First question - is this legal, should it run...
10
by: name | last post by:
When I started testing the algorithms for my wrap program, I threw together this snippet of code, which works quite well. Except that it (predictably) segfaults at the end when it tries to go...
3
by: kj | last post by:
I am trying to diagnose a bug in my code, but I can't understand what's going on. I've narrowed things down to this: I have a function, say foo, whose signature looks something like: int foo(...
10
by: danielesalatti | last post by:
Hello!! I'm studying c++ and I'm trying to get a little piece of code working, but I'm getting a segfault with strlen here: void tabhash::set (url *U) { uint hash = U->hashCode(); char* url =...
44
by: sam_cit | last post by:
Hi Everyone, I tried the following program unit in Microsoft Visual c++ 6.0 and the program caused unexpected behavior, #include <stdio.h> #include <string.h> int main() {
53
by: ¬a\\/b | last post by:
strlen is wrong because can not report if there is some error e.g. char *a; and "a" point to an array of size=size_t max that has no 0 in it
14
by: Donn Ingle | last post by:
Yo, An app of mine relies on PIL. When PIL hits a certain problem font (for unknown reasons as of now) it tends to segfault and no amount of try/except will keep my wxPython app alive. My first...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.