473,416 Members | 1,486 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

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=malloc(sizeof(char)*100000);
if (getenv("QUERY_STRING")==NULL)
strcpy(querystring,"QUERY_STRING\0");
else
strcpy(querystring,(char *) getenv("QUERY_STRING"));
//l=strlen((char *)getenv("QUERY_STRING"));
//printf("%d \n",l);
//strcpy(querystring,(char *) getenv("QUERY_STRING"));
i=0;word=0;
printf("Stampa di QUERY_STRING\n");
//printf("QUERY_STRING=%s\n",querystring);
//while (querystring[i]!='\0')
//{
//printf("Parola %d",word++);
//while (querystring[i]!='&')
//{
//printf("%c",querystring[i]);
//i++;
//}
//i++;
//}
}

Francesco

Nov 15 '05 #1
2 2283
Lampa Dario <la**@dario.it> 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=malloc(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(querystring,"QUERY_STRING\0");
else
strcpy(querystring,(char *) getenv("QUERY_STRING"));
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(querystring,(char *) getenv("QUERY_STRING"));
i=0;word=0;
printf("Stampa di QUERY_STRING\n");
//printf("QUERY_STRING=%s\n",querystring);
//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
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...
5
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...
12
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...
1
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...
20
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,...
0
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...
42
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...
5
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') { // ......
37
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
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...
0
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...
0
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...

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.