473,748 Members | 8,530 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Where is the stupid error?

Hi, where is this stupid error in this program? When I execute it, i
receive a segmentation fault error.

#include <stdio.h>
int main(int argc, char *argv[], char *env[]) { int i=0; int l=0;
int word=0;
char *querystring;

querystring=mal loc(sizeof(char )*100000);
if (getenv("QUERY_ STRING")==NULL)
strcpy(querystr ing,"QUERY_STRI NG\0");
else
strcpy(querystr ing,(char *) getenv("QUERY_S TRING"));
//l=strlen((char *)getenv("QUERY _STRING"));
//printf("%d \n",l);
//strcpy(querystr ing,(char *) getenv("QUERY_S TRING"));
i=0;word=0;
printf("Stampa di QUERY_STRING\n" );
//printf("QUERY_S TRING=%s\n",que rystring);
//while (querystring[i]!='\0')
//{
//printf("Parola %d",word++);
//while (querystring[i]!='&')
//{
//printf("%c",que rystring[i]);
//i++;
//}
//i++;
//}
}

Francesco

Nov 15 '05 #1
2 2307
Lampa Dario <la**@dario.i t> wrote:
Hi, where is this stupid error in this program?
There are several stupid errors in this program.
When I execute it, i receive a segmentation fault error.
Then you're lucky. It could've gone on to scribble over random memory.
#include <stdio.h>
int main(int argc, char *argv[], char *env[]) { int i=0; int l=0;
The first stupid error is the use of a non-Standard declaration for
main() - what's worse, you don't even use the non-Standardness.

The second stupid error is a very bad indentation style, or rather, no
indentation style at all. Do you want to be able to still read your code
next month? Then lay it out legibly.
int word=0;
char *querystring;

querystring=mal loc(sizeof(char )*100000);
The third stupid error is not providing a declaration for malloc() (and
later, similar for getenv()). <stdlib.h> exists for a reason - use it.

The fourth stupid error is either using a compiler which doesn't check
for such errors, or asking a perfectly good compiler not to check.

The fifth stupid error is not checking that malloc() succeeded.

Both the last error, and the third aided by the fourth, could have
caused the segfault you saw. They could just as easily have caused the
program to appear to work on your computer, but fail ignominously on
your boss's or teacher's system.
if (getenv("QUERY_ STRING")==NULL)
strcpy(querystr ing,"QUERY_STRI NG\0");
else
strcpy(querystr ing,(char *) getenv("QUERY_S TRING"));
The sixth stupid error is casting away a warning. You did not get that
warning for nothing. There is no need to cast a char * to a char * in
order to pass it to a function expecting a char *, and no useable
compiler will warn about it. Since you did (apparently - else why the
cast?) get a warning, clearly something else is wrong. It is related to
one of the errors above - guess which.
The same error occurs again later.
//l=strlen((char *)getenv("QUERY _STRING"));
//printf("%d \n",l);
//strcpy(querystr ing,(char *) getenv("QUERY_S TRING"));
i=0;word=0;
printf("Stampa di QUERY_STRING\n" );
//printf("QUERY_S TRING=%s\n",que rystring);
//while (querystring[i]!='\0')
//{
//printf("Parola %d",word++);
//while (querystring[i]!='&')


The seventh stupid error is not checking whether you run over the end of
a string. If this program gets used the way I think it will, this _will_
happen here.

That's seven - that should be enough cardinal sins for one post.

Richard
Nov 15 '05 #2
Lampa Dario wrote:
Hi, where is this stupid error in this program? When I execute it, i
receive a segmentation fault error.


The usual wild point the last 4000 times analogous questions were posted
to this newsgroup.

A well-behaved person will before posting to any newsgroup,
a) Check the FAQ -- you obviously haven't
b) follow the newsgroup for a while -- you obviously haven't.

You should learn to indent your code, lose superfluous casts, learn that
sizeof(char) == 1 always, and use a portable form of main().
And don't comment out code with C++ style '//' comments: not only are
they not legal in C89 and cause problems with broken lines on posted
code, but they are less readable than the conventional idiom of
#if 0
commented out code
#endif
Nov 15 '05 #3

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

Similar topics

3
1315
by: João Santa Bárbara | last post by:
Hi all. i have a funny error in the IDE but only in one form. :( i have declare a Method like this "Private Sub MyTransGForms(ByVal sender As Object, ByVal e As EventArgs)" and i have an handler to it. and in the same form i have this calling
5
3135
by: raz | last post by:
Greetings all. I apologize for what is almost certainly a stupid question, but I can't figure this out, and have no more time for head bashing... The short version: what is the appropriate include file to define the CString type in an MFC based app? The longer version:
12
2265
by: Martin_Hurst | last post by:
It would appear that MySQL is making great strides into the commercial and even the enterprise arena. I am not seeing the same news coverage being said about Postgresql. I believe Postgresql has been a much better rdbms than MySQL for a long time, so then why has not Postgresql grabbed the commercial and enterprise arena before MySQL did? -------------------------------------------------------------------------------------------...
1
1492
by: Stephen Quinney | last post by:
I really cannot tell if this is a bug or I am just doing something stupid. I create a table called wibble: CREATE TABLE wibble (a integer, b integer); I insert some data: INSERT INTO wibble VALUES (1,1);
20
2609
by: Rich Grise | last post by:
I've been lurking for awhile, and I notice that there are some real tight-assed prigs around here. Bitch, bitch, bitch. So what if it's not "Standard C?" If it looks like C, smells like C, quacks like C, and compiles like C, it's probably close enough.
0
1798
by: CHRIS | last post by:
Isn't it always the case that as soon as you post, you solve the problem? Apparently, adding an extra "space" to a blank line in my web.config file solved this problem. I would be angry that it was something stupid that like that ended up fixing it, but Im just so glad that I can actually get some work done now.
42
1665
by: aaron.kempf | last post by:
since Access 2007 is a 'major change' then i assume that MS is going to make a new newsgroup for it. I mean; when VB.net 2002 came out; they decided to rename the 'main vb newsgroup' as 'Microsoft.Public.Dotnet.Languages.VB' and now.. when people go to microsoft.public.vb they see 'vb as a dead language' so I nominate this newsgroup name for Access 2007
5
1758
by: Puppet_Sock | last post by:
So, I'm madly coding away, and my fingers stutter, and I produce this. (mfirstToken is a std::string object.) if(m_firstToken.c_str() == 'M' || m_firstToken.c_str().c_str() == 'T') { // ... other code here } And of course my compiler barfs over it. But what it tells me is
37
2613
by: Phlip | last post by:
1230987za wrote: Kanze is a classically-trained "unit tester". In some circles "unit" is a QA concept - specifically, if a test fails, you only need to inspect one unit. So "units" are "things which are clearly delimited and accessible to inspection". That should map onto C++ classes - specifically due to overlapping requirements. C++ classes _should_ be "things which are clearly delimited and accessible to inspection". Yet...
0
8989
marktang
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...
0
8828
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
9537
Oralloy
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...
1
9319
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
8241
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6795
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
4599
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
4869
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2213
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.