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;
} 4 3042
"ben" <my****@dodgeit .com> wrote in message
news:60******** *************** ***@posting.goo gle.com... 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
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/
(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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 bytes (see below). Once this happened I created a
|
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
|
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.
|
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 2005. Everything is standard C (ANSI C ?? I don't
know the difference) - but since so many functions...
|
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
project, we have thousands of C & C++ files. They don't have a common
included common header. ...
| |
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.
|
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
|
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
|
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. Where to find the list of errors in websites as it is not the standard apache error. I could not find...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| | |