Why will this simple program wont work?

Why will this simple program wont work?

Post by amir » Sat, 22 May 1999 04:00:00



I wrote some simple program to function as a SSI CGI for random queries.

I can compile it, But it won's work(Undex Linux, I succed runing it
under DOS and Windows):

#include<iostream.h>
#include<stdlib.h>
#include<string.h>
char *setquery(const char *query);
void main(void)
{
 char *queries[2];
 queries[0]=setquery("I wrote here other queries...");
 queries[1]=setquery("I puttted two here just that the random will take
efect.");
 cout<<"Content/type: text/html\n\n";
 cout<<queries[rand() % 1];
 delete[] queries;

Quote:}

char *setquery(const char *query)
{
 char *res=new char [strlen(query)+1];
 strcpy(res,query);
 return res;

Quote:}

Thanx to who ever help me.
 
 
 

Why will this simple program wont work?

Post by Andre Kost » Sat, 22 May 1999 04:00:00



>I wrote some simple program to function as a SSI CGI for random queries.

>I can compile it, But it won's work(Undex Linux, I succed runing it
>under DOS and Windows):

You haven't defined what 'work' is... but I'll comment on the program
itself...

Quote:>#include<iostream.h>
>#include<stdlib.h>
>#include<string.h>
>char *setquery(const char *query);
>void main(void)
>{
> char *queries[2];
> queries[0]=setquery("I wrote here other queries...");
> queries[1]=setquery("I puttted two here just that the random will take
>efect.");
> cout<<"Content/type: text/html\n\n";
> cout<<queries[rand() % 1];
> delete[] queries;

Boom.  'queries' was not allocated with new[].  You should
iterate over each element of queries, and call delete[] on each one.
(The elements were dynamically created, the array itself wasn't...)
Quote:>}
>char *setquery(const char *query)
>{
> char *res=new char [strlen(query)+1];
> strcpy(res,query);
> return res;
>}

>Thanx to who ever help me.


 
 
 

Why will this simple program wont work?

Post by Jonathan Brow » Sat, 22 May 1999 04:00:00


The line :
delete[] queries;

causes core dump, change it to:
delete queries[0];
delete queries[1];

Also, you are not seeding rand,  call srand(time(NULL)) to seed it.

 
 
 

Why will this simple program wont work?

Post by Kalle Olavi Niemital » Sun, 23 May 1999 04:00:00



>  cout<<queries[rand() % 1];

integer % 1 is always 0.
 
 
 

Why will this simple program wont work?

Post by Bob Nelso » Mon, 24 May 1999 04:00:00



> I wrote some simple program to function as a SSI CGI for random queries.
> I can compile it, But it won's work(Undex Linux, I succed runing it
> under DOS and Windows):
> #include<iostream.h>
> #include<stdlib.h>
> #include<string.h>

If you're going to do C++, use the standard header naming convention:

#include<iostream>
#include<cstdlib>
#include<cstring>

Quote:> void main(void)

main returns an int, not void. In C++, this can be written as
``int main()''.

Other responses have addressed other aspects of your code.

--
========================================================================

      http://www.oldradio.com/archives/nelson/open-computing.html
``Those who don't understand UNIX are condemned to reinvent it, poorly.''

 
 
 

Why will this simple program wont work?

Post by Kaz Kylhe » Wed, 26 May 1999 04:00:00




Quote:>The line :
>delete[] queries;

>causes core dump, change it to:
>delete queries[0];
>delete queries[1];

>Also, you are not seeding rand,  call srand(time(NULL)) to seed it.

That would render the program somewhat less portable. The time_t typedef is
some kind of arithmetic type, not necessarily an integer (according to ANSI C,
an ``arithmetic type capable of representing time''). On some platform where it
happens to be a floating type, and where time() happens to return a value that
won't fit into an unsigned int, a floating point exception may result.  On
Linux you're OK because time_t is an integer which can be safely converted to
unsigned int.
 
 
 

1. Simple X program wont build

Hi X-perts. I am an X novice and have a simple question. I am trying
to build a very simple program on my Linux box. Its the very first
example in Eric Johnsons X Window Applications Programming.

        Display *theDisplay;

        theDisplay = XOpenDisplay(NULL);

The compiler complains as follows:

Undefined symbol _XOpenDisplay referenced from text segment.
Yes I have included the header files and the proto for this is in there.

Also,
        /usr/lib/crt0.o: Undefined synbol _main referenced from text segment

What is crt0.o and what does it have to do with _main??

Thanks

Dave Bashaw

2. kdev_t crapectomy

3. Simple client/server socket program works on one Linux machine, it doesnot work on other machine

4. All you need is to invest $5 for a FORTUNE!!! ACT NOW!!!

5. Why wont my lilo install work?

6. fvwm-ewmh-0.3 - Extended WM Hints for FVWM

7. Why wont telnet work with ppp ?

8. Problem with Connection to Linux host

9. why wont sym links work w/ftp?

10. why wont this command work in a script?

11. Why wont this syslog.conf work?

12. why wont my soundcard work?

13. Help! setup program wont work