469,568 Members | 1,495 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,568 developers. It's quick & easy.

bus error with printf line included, error without printf line?

ben
why is it, in the below code, when there's a printf statement (the one
commented with /* ****** */) the final for loop prints out fine, but
without the commented with stars printf statement included in the code
there's a bus error on the fourth element in the final for loop?

final for loop print out when the /* ****** */ printf line is included
in the code:

ab
ba
bc
cb
ca
ac
final for loop print without that printf line included in the code:

ab
ba
bc
Bus error
any ideas what's wrong? and why would the inclusion of a printf line
make a difference to whether it works or not? seems strange to me.

thanks very much, ben.

#include <stdio.h>
#include <stdlib.h> /* malloc */
#include <string.h> /* strcmp */

int
main(void)
{
char *data[] = { "ab", "ba", "ab", "bc", "cb", "ba", "ab", "ba",
"cb", "bc", "ca", "ac", NULL };

char **udp; /* for list of pointers to unique strings */
unsigned number = 0; /* number of strings */
unsigned unique = 0; /* number of unique strings */
unsigned dpi, udpi; /* indexes */
unsigned char flag;

for( dpi = 0; data[dpi] != 0; dpi++ ) /* get number of strings */
number++;

udp = (char**)malloc(number); /* for list of pointers to unique
strings */

for( dpi = 0; dpi < number; dpi++ ) { /* loop per string in input
data */
flag = 0;
for( udpi = 0; udpi < unique && flag != 1; udpi++ ) { /* loop per
unique string collected so far */
printf("%s %s\n", data[dpi], udp[udpi]); /* ****** */
if( strcmp(data[dpi], udp[udpi]) == 0 )
flag = 1;
}
if( flag == 0 ) /* if unique store it in unique list */
udp[unique++] = data[dpi];
}

putchar('\n');

for( dpi = 0; dpi < unique; dpi++ ) /* print the list of unique
strings */
printf("%s\n", udp[dpi]);

return 0;
}
Nov 14 '05 #1
4 2699
"ben" <my****@dodgeit.com> wrote in message
news:60**************************@posting.google.c om...
why is it, in the below code, when there's a printf statement (the one
commented with /* ****** */) the final for loop prints out fine, but
without the commented with stars printf statement included in the code
there's a bus error on the fourth element in the final for loop?

any ideas what's wrong? and why would the inclusion of a printf line
make a difference to whether it works or not? seems strange to me.
It is strange, but the printf (or absence) is not the cause of the problem.
#include <stdio.h>
#include <stdlib.h> /* malloc */
#include <string.h> /* strcmp */

int
main(void)
{
char *data[] = { "ab", "ba", "ab", "bc", "cb", "ba", "ab", "ba",
"cb", "bc", "ca", "ac", NULL };

char **udp; /* for list of pointers to unique strings */
unsigned number = 0; /* number of strings */
unsigned unique = 0; /* number of unique strings */
unsigned dpi, udpi; /* indexes */
unsigned char flag;

for( dpi = 0; data[dpi] != 0; dpi++ ) /* get number of strings */
number++;

udp = (char**)malloc(number); /* for list of pointers to unique
strings */
Apart from not checking the return value for failure, you're not performing the allocation
properly. Try...

udp = malloc(number * sizeof *udp);

[The cast is redundant in C.]

for( dpi = 0; dpi < number; dpi++ ) { /* loop per string in input
data */
flag = 0;
for( udpi = 0; udpi < unique && flag != 1; udpi++ ) { /* loop per
unique string collected so far */
printf("%s %s\n", data[dpi], udp[udpi]); /* ****** */
if( strcmp(data[dpi], udp[udpi]) == 0 )
flag = 1;
}
if( flag == 0 ) /* if unique store it in unique list */
udp[unique++] = data[dpi];
This is likely writing to memory you don't own.
}

putchar('\n');

for( dpi = 0; dpi < unique; dpi++ ) /* print the list of unique
strings */
printf("%s\n", udp[dpi]);

return 0;
}


--
Peter
Nov 14 '05 #2
In 'comp.lang.c', my****@dodgeit.com (ben) wrote:
udp = (char**)malloc(number); /* for list of pointers to unique
strings */


This is the problem. You don't allocate enough memory. See the fixed code
hereby:

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

int main (void)
{
char const *data[] =
{"ab", "ba", "ab", "bc", "cb", "ba", "ab", "ba",
"cb", "bc", "ca", "ac", NULL};

/* number of strings */
unsigned number = 0;

/* get number of strings */
{
unsigned dpi;
for (dpi = 0; data[dpi] != NULL; dpi++)
{
number++;
}
}

{
/* number of unique strings */
unsigned unique = 0;

/* for list of pointers to unique strings */
char const **udp = malloc (number * sizeof *udp);

if (udp != NULL)
{
unsigned dpi;

/* loop per string in input data */
for (dpi = 0; dpi < number; dpi++)
{
int already = 0;
unsigned udpi;

/* loop per unique string collected so far */
for (udpi = 0; udpi < unique && !already; udpi++)
{
printf ("%s %s\n", data[dpi], udp[udpi]);

already = strcmp (data[dpi], udp[udpi]) == 0;
}

/* if unique store it in unique list */
if (!already)
{
udp[unique++] = data[dpi];
}
}
}
putchar ('\n');

{
unsigned dpi;

/* print the list of unique strings */
for (dpi = 0; dpi < unique; dpi++)
{
printf ("%s\n", udp[dpi]);
}
}

free (udp), udp = NULL;
}
return 0;
}

Feel free to ask for details.

--
-ed- get my email here: http://marreduspam.com/ad672570
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 14 '05 #3
ben
(sorry the subject of this doesn't make sense - should have been
"*no* bus error with printf line included, error without printf line?")

yup, great, thanks -- you're both correct of course. i knew the printf
statement wasn't the cause, but obviously wasn't sure what was.

thanks again :)

ben.
Nov 14 '05 #4
ben wrote:

any ideas what's wrong?
char **udp; /* for list of pointers to unique strings */ udp = (char**)malloc(number); /* for list of pointers to unique
strings */


You have no reason to thing that a char * is one char long. Rewrite this:
udp = malloc(number * sizeof *udp);
and check the return value.
Nov 14 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Peter Frost | last post: by
13 posts views Thread by a.zeevi | last post: by
33 posts views Thread by Martin Jørgensen | last post: by
19 posts views Thread by RedDevilDan | last post: by
21 posts views Thread by one2001boy | last post: by
13 posts views Thread by Albert | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.