Why this doesent work on bigger numbers..

M

Milutin_Popovski

Don't be suprised if on UNIX
it retourns Segmentation fault...
But it works for smalest numbers...
Why this doesent work for bigger...
(it makes double linked list
whith recoursion)...


Thanks in advance, Robert...;)
(e-mail address removed)-com.hr


#include<stdio.h>
#include<stdlib.h>
#include<time.h>

struct double_list{
int val;
struct double_list *prev;
struct double_list *next;
};


int main(int argc, char *argv[]){
int n;
struct double_list *first, *t;
if(argc!=2){
printf("\nUsage r number");
return 1;
}
if(chk(argv[1])==0){
printf("\nUsage r number");
return 1;
}
n=atoi(argv[1]);
first=(struct double_list *)malloc(sizeof(struct double_list));
first->val=rand()%100;
// t=first;
r(first, n);
t=p(first, n);
printf("\n-----------------------------------------------------
\n");
rp(t,n);
return 0;
}

int r(struct double_list *temp, int n){
struct double_list *old;
if(n==0) return 0;
old=temp;
// printf("%d ", temp->val);
temp->next=(struct double_list *)malloc(sizeof(struct
double_list));
temp=temp->next;
temp->val=rand()%100;
temp->next=NULL;
temp->prev=old;
r(temp, n-1);
}

struct double_list *p(struct double_list *temp, int n){
if(temp->next == NULL) return temp;
printf("%d ", temp->val);
temp=temp->next;
return(p(temp,n-1));
}


int rp(struct double_list *t, int n){
if(n==0) return 0;
t=t->prev;
printf("%d ", t->val);
// t=t->prev;
rp(t, n-1);
}




int chk(char *c){
if(!isdigit(*c) && *c!='\0') return 0;
if(*c=='\0') return 1;
*c++;
chk(c);
}
 
N

Nick Keighley

Don't be suprised if on UNIX
it retourns Segmentation fault...
But it works for smalest numbers...

what does "it works" mean?
what is a small number? [6.02e-35?, 0, 9?)
Why this doesent work for bigger...

what does "doesn't work" mean?
what is a bigger number? (6.02e23, 0, 9?)
(it makes double linked list
whith recoursion)...

maybe it over-recursed. I must admit I'd try running it but it doesn't
compile and I think its your job to sort that out.


D:\bin\net\pop.c(23) : warning C4013: 'chk' undefined; assuming extern
returning int
D:\bin\net\pop.c(31) : warning C4013: 'r' undefined; assuming extern
returning int
D:\bin\net\pop.c(32) : warning C4013: 'p' undefined; assuming extern
returning int
D:\bin\net\pop.c(32) : warning C4047: '=' : 'struct double_list *'
differs in levels of indirection from 'int '
D:\bin\net\pop.c(34) : warning C4013: 'rp' undefined; assuming extern
returning int
D:\bin\net\pop.c(57) : error C2371: 'p' : redefinition; different
basic types
D:\bin\net\pop.c(61) : warning C4047: 'return' : 'struct double_list
*' differs in levels of indirection from 'int '
D:\bin\net\pop.c(79) : warning C4013: 'isdigit' undefined; assuming
extern returning int
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

it's odd not to put a space between #include and <
in general you seem very mean with white space
struct double_list{
   int val;
   struct double_list *prev;
   struct double_list *next;

};

why the blank line. You seem desparate to save vertical space with the
opening curly then waste it just before the closing one


   first=(struct double_list *)malloc(sizeof(struct double_list));

don't cast the return value of malloc()

   first->val=rand()%100;

just as an example I'd lay that out as

first->val = rand () % 100;

int r(struct double_list *temp, int n){

it's better to use descriptive names for functions. What does r() do?
It's not obvious from its name

<snip>
 
N

Nick Keighley

On 15 July, 12:21, Milutin_Popovski <[email protected]>
Don't be suprised if on UNIX
it retourns Segmentation fault...
But it works for smalest numbers...

what does "it works" mean?
what is a small number? [6.02e-35?, 0, 9?)
Why this doesent work for bigger...

what does "doesn't work" mean?
what is a bigger number? (6.02e23, 0, 9?)
(it makes double linked list
whith recoursion)...

maybe it over-recursed. I must admit I'd try running it but it doesn't
compile and I think its your job to sort that out.

D:\bin\net\pop.c(23) : warning C4013: 'chk' undefined; assuming extern
returning int
D:\bin\net\pop.c(31) : warning C4013: 'r' undefined; assuming extern
returning int
D:\bin\net\pop.c(32) : warning C4013: 'p' undefined; assuming extern
returning int
D:\bin\net\pop.c(32) : warning C4047: '=' : 'struct double_list *'
differs in levels of indirection from 'int '
D:\bin\net\pop.c(34) : warning C4013: 'rp' undefined; assuming extern
returning int
D:\bin\net\pop.c(57) : error C2371: 'p' : redefinition; different
basic types
D:\bin\net\pop.c(61) : warning C4047: 'return' : 'struct double_list
*' differs in levels of indirection from 'int '
D:\bin\net\pop.c(79) : warning C4013: 'isdigit' undefined; assuming
extern returning int

ok I relented. I moved chk(), p(), r() and rp() to the top of the
program and added
#include <ctype.h>
so it compiled. I ran it with various input values up to 50,000 and it
produced a bunch of numbers and didn't crash, Is 50,000 a big number?
Its larger than the guranteed minimum size of an int so that seems
pretty big for a number you're storing in an int.

I ran it on Windows. Have you checked how much stack space you have?
Have you tried a debugger?

<snip>
 
N

Nick Keighley

what does "it works" mean?
what is a small number? [6.02e-35?, 0, 9?)
Why this doesent work for bigger...
what does "doesn't work" mean?
what is a bigger number? (6.02e23, 0, 9?)
maybe it over-recursed. I must admit I'd try running it but it doesn't
compile and I think its your job to sort that out.
D:\bin\net\pop.c(23) : warning C4013: 'chk' undefined; assuming extern
returning int
D:\bin\net\pop.c(31) : warning C4013: 'r' undefined; assuming extern
returning int
D:\bin\net\pop.c(32) : warning C4013: 'p' undefined; assuming extern
returning int
D:\bin\net\pop.c(32) : warning C4047: '=' : 'struct double_list *'
differs in levels of indirection from 'int '
D:\bin\net\pop.c(34) : warning C4013: 'rp' undefined; assuming extern
returning int
D:\bin\net\pop.c(57) : error C2371: 'p' : redefinition; different
basic types
D:\bin\net\pop.c(61) : warning C4047: 'return' : 'struct double_list
*' differs in levels of indirection from 'int '
D:\bin\net\pop.c(79) : warning C4013: 'isdigit' undefined; assuming
extern returning int

ok I relented. I moved chk(), p(), r() and rp() to the top of the
program and added
#include <ctype.h>
so it compiled. I ran it with various input values up to 50,000 and it
produced a bunch of numbers and didn't crash, Is 50,000 a big number?
Its larger than the guranteed minimum size of an int so that seems

that should have read something like "...larger than the guranteed
maximum value...". Basically the C standard only gurantees int can
hold up to 16 bits. The fact that most modern platforms make int 32-
bits (or more) is not relevent.
pretty big for a number you're storing in an int.

I ran it on Windows. Have you checked how much stack space you have?

how much heap, you're doing an awful lot of mallocs()
 
B

Ben Bacarisse

Nick Keighley said:
On 15 July, 13:00, Nick Keighley <[email protected]>
wrote:

how much heap, you're doing an awful lot of mallocs()

Only N+1 mallocs where N is the command-line argument. On my machine a
corrected version of the program runs out of stack before it runs out of
heap.
 
N

Nick Keighley

Don't be suprised if on UNIX
it retourns Segmentation fault...
But it works for smalest numbers...
Why this doesent work for bigger...
(it makes double linked list
whith recoursion)...

Thanks in advance, Robert...;)
(e-mail address removed)-com.hr

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

struct double_list{
   int val;
   struct double_list *prev;
   struct double_list *next;

};

int main(int argc, char *argv[]){
   int n;
   struct double_list *first, *t;
   if(argc!=2){
      printf("\nUsage r number");
      return 1;
   }
   if(chk(argv[1])==0){
      printf("\nUsage r number");
      return 1;
   }
   n=atoi(argv[1]);
   first=(struct double_list *)malloc(sizeof(struct double_list));
   first->val=rand()%100;

and the other fields? What is n is zero?
 
B

Ben Bacarisse

Milutin_Popovski said:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>

You should include <ctype.h> for isdigit. The type of isdigit means you
will get away without it, but that's no way to program!

I suggest you turn up the warning level on your compiler (or pay more
attention to what it says).
struct double_list{
int val;
struct double_list *prev;
struct double_list *next;
};


int main(int argc, char *argv[]){
int n;
struct double_list *first, *t;
if(argc!=2){
printf("\nUsage r number");
return 1;
}
if(chk(argv[1])==0){
printf("\nUsage r number");
return 1;
}
n=atoi(argv[1]);

You can use strtol (or one of its friends) to do both the conversion and
the checking. I had to move main to the end of the file so that
declarations of r, rp and chk could be seen. Your compiler should have
told you about that.
first=(struct double_list *)malloc(sizeof(struct double_list));

first = malloc(sizeof *first);

is much simpler and (to me) clearer.
first->val=rand()%100;

What have you got against spaces! Use more of them!
// t=first;
r(first, n);
t=p(first, n);
printf("\n-----------------------------------------------------
\n");
rp(t,n);
return 0;
}

int r(struct double_list *temp, int n){
struct double_list *old;
if(n==0) return 0;
old=temp;
// printf("%d ", temp->val);
temp->next=(struct double_list *)malloc(sizeof(struct
double_list));
temp=temp->next;
temp->val=rand()%100;
temp->next=NULL;
temp->prev=old;
r(temp, n-1);

You need 'return r(temp, n - 1);' here. You don't use the return value
in your code so this won't be the reason for the fault.
}

struct double_list *p(struct double_list *temp, int n){
if(temp->next == NULL) return temp;
printf("%d ", temp->val);
temp=temp->next;
return(p(temp,n-1));
}


int rp(struct double_list *t, int n){
if(n==0) return 0;
t=t->prev;
printf("%d ", t->val);
// t=t->prev;
rp(t, n-1);

Again, no 'return'.
}




int chk(char *c){
if(!isdigit(*c) && *c!='\0') return 0;
if(*c=='\0') return 1;
*c++;
chk(c);

Again, no return. This one matters. If I had to write this recursively
(and it is a little odd to do that in C) I'd write:

int is_digits(char *s)
{
if (*s == 0) return 1;
else return isdigit(*s) && is_digits(s + 1);
}

which I think is a lot clearer. Predicate function (ones that test for
some condition) should be named after what the confirm. chk tells me
very little except that it is probably checking for something.
 
P

Paul N

why the blank line. You seem desparate to save vertical space with the
opening curly then waste it just before the closing one

This may be due to his newsreader. I post using Google groups, and for
some reason it seems to always insert a blank line immediately prior
to a line containing just a }. Conversely it seems to remove some of
the blank lines I put in. I think it is something to do with the way
it handles quoting.
 
N

Nick Keighley

This may be due to his newsreader. I post using Google groups, and for
some reason it seems to always insert a blank line immediately prior
to a line containing just a }. Conversely it seems to remove some of
the blank lines I put in. I think it is something to do with the way
it handles quoting.

void test (void)
{
some_stuff();
}

void test2 (int i){
more_stuff();
}
 
N

Nick Keighley

void test (void)
{
     some_stuff();

}

void test2 (int i){
    more_stuff();



}- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -

ah, indeed you are correct
 
N

Nick

Nick Keighley said:
ah, indeed you are correct

I think you're making this up. That's not a genuine GG post. It
doesn't have anything like enough spurious "- Show/Hide quoted text -"
blocks to be real.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top