473,698 Members | 2,524 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Little problem with a delirant program

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
char *_(char *str1, char *str2)
{
char *res;
res = malloc((strlen( str1)+strlen(st r2))*sizeof(cha r));
strcpy(res, str1);
strcpy(res+strl en(str1), str2);
return res;
}

int main(void)
{
char a[] = "\nHey man look at me rockin' now\nI'm on the ";
char b[] = "Danny and Lisa";
char c[] = "\nThey take me away from";
char *d = _(_(a,"radio"), _(a,"video"));
char *e = _(d,_(" with ",b));
char *g = _("\nThe strangest places\nSweet ",b);
char *h = _(c,g);
char *i = _(_(h,c),_(h,c) );
char *j = _(e,i);
char *k = _(_(d,j),_(j,e) );
char *l = _(_(k,h),_(d,e) );
puts(++l); /* ++l gets rid of the spurious newline character at the
beginning */
return 0;
}

Why does this program add a spurious '1' after each occurrence of the last
line of the chorus? (i.e. "I'm on the video with Danny and Lisa1").
(I said it was delirant... But I fear that for copyright problems I won't be
able to sumbit a modification of this to the next IOCCC...)
T.I.A.

--
#include <stdio.h>
main()
{
printf("\n\n--\nArmy1987");
}
Mar 18 '07 #1
14 1378
Army1987 wrote:
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
char *_(char *str1, char *str2)
Don't do that. Identifiers beginning with `_' are
reserved. There are a few contexts in which they are not
reserved, but this isn't one of them.
{
char *res;
res = malloc((strlen( str1)+strlen(st r2))*sizeof(cha r));
strcpy(res, str1);
strcpy(res+strl en(str1), str2);
This is probably where your program goes off the rails;
perhaps one line earlier if str2 has zero length. I thought
this matter would be covered in the FAQ -- but perhaps it's
just a Frequently Committed Error rather than a Frequently
Asked Question, since the very asking of the question implies
awareness of the issue.

Anyhow, the immediate error is that a string whose length
is N characters requires N+1 bytes of storage.
return res;
}

int main(void)
{
char a[] = "\nHey man look at me rockin' now\nI'm on the ";
char b[] = "Danny and Lisa";
char c[] = "\nThey take me away from";
char *d = _(_(a,"radio"), _(a,"video"));
char *e = _(d,_(" with ",b));
char *g = _("\nThe strangest places\nSweet ",b);
char *h = _(c,g);
char *i = _(_(h,c),_(h,c) );
char *j = _(e,i);
char *k = _(_(d,j),_(j,e) );
char *l = _(_(k,h),_(d,e) );
puts(++l); /* ++l gets rid of the spurious newline character at the
beginning */
return 0;
}

Why does this program add a spurious '1' after each occurrence of the last
line of the chorus? (i.e. "I'm on the video with Danny and Lisa1").
(I said it was delirant... But I fear that for copyright problems I won't be
able to sumbit a modification of this to the next IOCCC...)
It wouldn't win. The IOCCC seeks obfuscations far more
original than just using an unusual name, especially an unusual
name that isn't yours to use.

--
Eric Sosman
es*****@acm-dot-org.invalid
Mar 18 '07 #2

"Army1987" <pl********@for .itwrote in message n
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
char *_(char *str1, char *str2)
{
char *res;
res = malloc((strlen( str1)+strlen(st r2))*sizeof(cha r));
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^
No room for terminating nul.
strcpy(res, str1);
strcpy(res+strl en(str1), str2);
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^
Undefined behaviour here.
return res;
}

int main(void)
{
char a[] = "\nHey man look at me rockin' now\nI'm on the ";
char b[] = "Danny and Lisa";
char c[] = "\nThey take me away from";
char *d = _(_(a,"radio"), _(a,"video"));
char *e = _(d,_(" with ",b));
char *g = _("\nThe strangest places\nSweet ",b);
char *h = _(c,g);
char *i = _(_(h,c),_(h,c) );
char *j = _(e,i);
char *k = _(_(d,j),_(j,e) );
char *l = _(_(k,h),_(d,e) );
puts(++l); /* ++l gets rid of the spurious newline character at the
beginning */
return 0;
}

Why does this program add a spurious '1' after each occurrence of the last
line of the chorus? (i.e. "I'm on the video with Danny and Lisa1").
(I said it was delirant... But I fear that for copyright problems I won't
be able to sumbit a modification of this to the next IOCCC...)
T.I.A.

--
#include <stdio.h>
main()
{
printf("\n\n--\nArmy1987");
}
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Mar 18 '07 #3
I've found the problem. For the memory allocated by the function _(str1,
str2) to contain the concatenation of str1 and str2, its size should be
strlen(str1) + strlen(str2) + 1, as one byte is needed to contain the
terminator '\0' character.

So I replaced the assignment in the function body with:
res = malloc((strlen( str1)+strlen(st r2)+1)*sizeof(c har));

Probably, in the former version of the program the '\0' byte after one of
these strings was somehow overwritten as it was not "allotted" for that
string.

BTW, I meant "delirious" . I should have checked my English besides my C...
Mar 18 '07 #4
It wouldn't win. The IOCCC seeks obfuscations far more
original than just using an unusual name, especially an unusual
name that isn't yours to use.
I used that just because it was the shortest identifier which couldn't clash
with a letter (as I didn't know beforehand how many letters I had to use).
Of course I could just use a capital letter, but I didn't know about
reserved identifiers, and I thougth _ was cooler... ;-)
Of course, if I were really going to submit it to IOCCC, I'd brutally deface
it first... (I was just joking, I started learning C just about two months
ago... And I've seen some programs by the past winners of IOCCC and I really
think some of them really need to get a life...)
Mar 18 '07 #5
Army1987 wrote:
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
char *_(char *str1, char *str2)
^^^
very, very naughty. Don't do this. It is evil.
{
char *res;
res = malloc((strlen( str1)+strlen(st r2))*sizeof(cha r));
Not only is sizeof(char) superfluous here (it's 1 by definition),
you don't allocate enough space:
res = malloc(strlen(s tr1) + strlen(str2) + 1);
And, of course, you should check the return value from malloc.

strcpy(res, str1);
strcpy(res+strl en(str1), str2);
return res;
}

int main(void)
{
char a[] = "\nHey man look at me rockin' now\nI'm on the ";
char b[] = "Danny and Lisa";
char c[] = "\nThey take me away from";
char *d = _(_(a,"radio"), _(a,"video"));
char *e = _(d,_(" with ",b));
char *g = _("\nThe strangest places\nSweet ",b);
char *h = _(c,g);
char *i = _(_(h,c),_(h,c) );
char *j = _(e,i);
char *k = _(_(d,j),_(j,e) );
char *l = _(_(k,h),_(d,e) );
puts(++l); /* ++l gets rid of the spurious newline character at the
beginning */
return 0;
Are you _sure_ you want to return before freeing all those arrays
that your (incorrectly) allocated?
}

Why does this program add a spurious '1' after each occurrence of the last
line of the chorus?
Since your allocation is broken, you're unlucky that your program didn't
crash instead.
(i.e. "I'm on the video with Danny and Lisa1").
(I said it was delirant... But I fear that for copyright problems I won't be
^^^^^^^^
What language is this a word in? And when did you say it? What
does it mean?
able to sumbit a modification of this to the next IOCCC...)
Please don't submit broken or illegal code to IOCCC. The woeld doesn't
need any more of either.
T.I.A.
Bite me.
Mar 18 '07 #6
Are you _sure_ you want to return before freeing all those arrays that
your (incorrectly) allocated?
Isn't my OS supposed to free them all immediately after main() returns?
(I refer to the corrected version, with
res = malloc(strlen(s tr1)+strlen(str 2)+1);
and maybe even
if (res==NULL) exit(EXIT_FAILU RE);
now.)

>(I said it was delirant... But I fear that for copyright problems I won't
be
^^^^^^^^
What language is this a word in? And when did you say it? What
does it mean?
I meant "delirious" , sorry. I said it in the object line.
Mar 18 '07 #7
On Mar 19, 7:38 am, Eric Sosman <esos...@acm-dot-org.invalidwrot e:
Army1987 wrote:
char *_(char *str1, char *str2)

Don't do that. Identifiers beginning with `_' are
reserved. There are a few contexts in which they are not
reserved, but this isn't one of them.
I thought such identifiers were only reserved if they started
with _ followed by an uppercase letter (or another _).

Mar 18 '07 #8
"Army1987" <pl********@for .itwrote in message
> Are you _sure_ you want to return before freeing all those arrays that
your (incorrectly) allocated?
Isn't my OS supposed to free them all immediately after main() returns?
(I refer to the corrected version, with
res = malloc(strlen(s tr1)+strlen(str 2)+1);
and maybe even
if (res==NULL) exit(EXIT_FAILU RE);
now.)
If an OS doesn't free memory on exit either
You are running a useless operating system that should be traded in
immediately
or
You are on some small system that will only run a limited subset of
programs, and isn't meant to be a general-purpose computer.

However it is a good habit to free memory after you are done with it. In big
programs it is essential.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Mar 18 '07 #9
Old Wolf wrote:
>
On Mar 19, 7:38 am, Eric Sosman <esos...@acm-dot-org.invalidwrot e:
Army1987 wrote:
char *_(char *str1, char *str2)
Don't do that. Identifiers beginning with `_' are
reserved. There are a few contexts in which they are not
reserved, but this isn't one of them.

I thought such identifiers were only reserved if they started
with _ followed by an uppercase letter (or another _).
The rules are complicated enough so that I just simply
avoid writing identifiers that start with underscores.

N869
7.1.3 Reserved identifiers
[#1]
-- All identifiers that begin with an underscore are
always reserved for use as identifiers with file scope
in both the ordinary and tag name spaces.

--
pete
Mar 18 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3427
by: Ron Stephens | last post by:
I posted to my web site a fun little program called merlin.py today. Please keep in mind that I am a hobbyist and this is just a little hack, if you look at the code you will see that it is still possible to write spaghetti code, even with Python. I apologize, and I do intend to clean up the code, but it may take awhile. For now it works, with some bugs. It is a composite of a few scripts. The first, based on a script Max M uploaded to...
8
1947
by: Usman | last post by:
Huy everyone , Well I am not a big C++ programmer , I am just a little young kid on it tryint to learn . Actually I was given an assignment last week by my teacher which I solved completely but was unable to go through one question.
38
3330
by: Martin Marcher | last post by:
Hi, I've read several questions and often the answer was 'C knows nothing about .' So if C knows that little as some people say, what are the benefits, I mean do other languages know more or is it a benefit that C knows nearly nothing (what I can think about is that C is the largest common divisor defined on most available platforms)?
7
1530
by: selekta | last post by:
hi, i've recently started to deal a little bit with tcp/ip-socket-programming (linux). Now i've written a little test-program (as displayed further down). When i'm trying to compile it the gcc-compiler return "parse error in l.23/.24 before token '='. " the line before those two ( host_addr.sin_family = AF_INET;) is acepted without any problems. I'd be happy if anybody could tell me where's my fault.
0
1191
by: Kenneth Lantrip | last post by:
After some cleaning of some of my personal directories and files, I stumbled upon this little program I wrote some time ago. A scammer was trying to introduce me into his little pyramid scam. So to prove it was a bad idea, I decided to write this little program to find some confidence in the dicision to not get mixed up in his plot to rule the world. Anyway, Here is the program for your amusement. I only had to change a couple of...
54
2979
by: ash | last post by:
i am writing this program (for exercise1-9 in k&r-2nd edition) which removes extra spaces in string example- "test string" will be "test string" #include<stdio.h> #include<string.h> #include<conio.h> main() { char str,st;
102
5098
by: BoogieWithStu22 | last post by:
I am running into a problem with a web page I have created when viewing it in IE6 on some machines. The page has a database lookup. The user enters an account name or number and clicks a lookup button. This hits an AS400 database looks for matches and returns a dataset that is used to populate a datagrid. The user then selects one of the entries from the list, this entry is used to populate a couple of textboxes on the page and a couple...
23
7039
by: Niranjan | last post by:
I have this program : void main() { int i=1; if((*(char*)&i)==1) printf("The machine is little endian."); else printf("The machine is big endian."); }
8
9987
by: marknvicf | last post by:
I have read a bit about the endian differences, got a couple of jar/libs to help (Android and com.mindprod), but still am not sure how to fix this program that I have (written in JDK 1.4 by someone else who is not here anymore). I have some complicated structures in this program that I don't know how to fix. I have fixed DataInputStream and DataOutputStream but using com.mindprod and now have LEDataInput and Output Streams (Little Endian). The...
0
8609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9030
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8899
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8871
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6525
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5861
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
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...
0
4621
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2333
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.