473,513 Members | 2,677 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3029
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
6361
by: Patrick Dunnigan | last post by:
Hi, I am attempting a bulk copy from a c program into SQL Server 2000 using DBLib in freeTDS 0.63 RC11 (gcc 3.4.3, RH 9). I am getting an error message that I cannot find any documentation on. The server is sending back the following: "Received invalid row length 2 from bcp client. Minimum row size is 4." I know the row is longer 2...
6
4715
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much appreciated. Thanks in advance
13
2455
by: a.zeevi | last post by:
free() multiple allocation error in C ==================================== Hi! I have written a program in C on PC with Windows 2000 in a Visual C environment. I have an error in freeing multiple allocation as follows: 1. I allocated an array of pointer. 2. I Read line by line from a text file. 3. I allocated memory for the read line.
33
2827
by: Martin Jørgensen | last post by:
Hi, In continuation of the thread I made "perhaps a stack problem? Long calculations - strange error?", I think I now got a "stable" error, meaning that the error always seem to come here now (tried: visual studio 2005 + linux/macintosh gcc)... That's a pretty good thing. I think the error still appears using both gcc and visual studio...
19
9834
by: RedDevilDan | last post by:
I am working on a Memory Footprint Reduction project. I came across an idea to disable all printf statements so that less memory is required. In addition, when there is no single printf statement, the printf library will not be linked, so it further reduces the executable size. Is there an easy way to disable printf in a large project? In my...
21
6835
by: one2001boy | last post by:
PostMessage() function returns ERROR_NOT_ENOUGH_QUOTA after running in a loop for 700 times, but the disk space and memory are still big enough. any suggestion to resolve this problem? thanks.
18
2836
by: sam_cit | last post by:
Hi Everyone, int main() { printf("not included stdio.h"); } Yes, i haven't included stdio.h and my compiler would generate a warning and would assume that it would return a int, my question is how does the linker manage to link the function invocation to the proper
13
3788
by: Albert | last post by:
Hi I'm using the lcc compiler for win32. I tried compiling a program but there's an error stating: "cpp: Can't open input file clrscr()" I don't get it - I've included <tcconio.h>. (strange why they couldn't have just left it as <conio.h>?): #include <tcconio.h> // code
10
6933
by: happyse27 | last post by:
Hi All, I got this apache errors(see section A1 and A2 below) when I used a html(see section b below) to activate acctman.pl(see section c below). Section D below is part of the configuration of section c. Not sure where went wrong as the web page displayed internal server error. Also, what is the error 543? and error 2114....
0
7559
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7123
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7542
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5701
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5100
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4756
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3248
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3237
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
470
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.