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

B

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;
}
 
P

Peter Nilsson

ben said:
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;
}
 
E

Emmanuel Delahaye

In said:
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.
 
B

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.
 
M

Martin Ambuhl

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.
 

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
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top