Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > bus error with printf line included, error without printf line?

Reply
Thread Tools

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

 
 
ben
Guest
Posts: n/a
 
      06-26-2004
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;
}
 
Reply With Quote
 
 
 
 
Peter Nilsson
Guest
Posts: n/a
 
      06-26-2004
"ben" <> wrote in message
news: 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


 
Reply With Quote
 
 
 
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      06-26-2004
In 'comp.lang.c', (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/
 
Reply With Quote
 
ben
Guest
Posts: n/a
 
      06-26-2004
(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.
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      06-26-2004
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.
 
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
printf affects following printf/s azza C Programming 0 10-17-2010 09:43 AM
Address Bus and External Data Bus Confusion LoXodonte A+ Certification 1 04-18-2006 09:09 PM
(void) printf vs printf whatluo C Programming 29 09-08-2005 05:42 PM
IRB, Mac OS X, command-line require via "-r" and Bus Errors James Adam Ruby 15 05-18-2005 03:10 PM
Consultant has a bus line in Sacramento Network Guru MCSE 13 08-09-2004 04:54 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57