Search the web
Sign In
New User? Sign Up
c4swimmers · India's First C/C++ Programming Portal
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Want your group to be featured on the Yahoo! Groups website? Add a group photo to Flickr.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
Strange Behavior   Message List  
Reply | Forward Message #8744 of 9110 |
Hi All,

Could you please help me out in analysing this issue:

I really find strange behavior with the following code. If the statement b is
present as below, the for loop is executing infinitely. But when statement b is
replaced with statement a as
int c[31], s[31];. Then the for loop is exited after n iterations. Below is my
gcc version where i executed the code. Please help me out in this regard.

int c [30], s[30]; // statement b

#include <stdio.h>
void change (int a[], int, int);
int main ()
{
int d[3];
d[0] = 1, d[1] = 10, d[2] = 25;
int n = 30, k = 3;
change (d, k, n);
return 1;
}

void change (int d[], int k, int n)
{
int c [30], s[30]; // statement b
c[0] = 0;
int p;
int min, i, coin = 0;

for (p=1; p<=n; p++)
{
c[p] = 0;
s[p] = 0;
printf ("\n test : %d \n", p);
}
}

gcc (GCC) 4.1.2 (Gentoo 4.1.2 p1.0.2)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

--
Thanks & Regards,
Mallik





[Non-text portions of this message have been removed]




Sun Nov 9, 2008 12:41 am

hi_malli2k3
Offline Offline
Send Email Send Email

Forward
Message #8744 of 9110 |
Expand Messages Author Sort by Date

Hi All, Could you please help me out in analysing this issue: I really find strange behavior with the following code. If the statement b is present as below,...
Mallik
hi_malli2k3
Offline Send Email
Nov 9, 2008
4:23 pm

That is because you are overwriting on memory location of variable p. If you print address of p and c, you'll find that memory for variable p is allocated...
Shantanu
shantanu_bed...
Offline Send Email
Nov 13, 2008
1:57 pm

Try doin it like this: #define SIZE 30 int c [SIZE], s[SIZE]; // statement b #include <stdio.h> void change (int a[], int, int); int main () { int d[3]; d[0] =...
ravi sundriyal
ravisundriyal@...
Send Email
Nov 13, 2008
1:57 pm

... statement b is present as below, the for loop is executing infinitely. But when statement b is replaced with statement a as ... Below is my gcc version...
suraj_joshi22
Offline Send Email
Nov 17, 2008
7:55 am

Hi, It goes to infinite loop because you are using the for loop wrong. when you have done s[30], you have allocated s[0] to s[29]. Now if you try to write...
srinivas g
srini_g2003
Offline Send Email
Nov 17, 2008
12:16 pm

Yes, Even i observed such cases where it didnot get into infinite loop and sometimes i'm getting a segmentation fault. I think that its an atypical behavior....
Mallik
hi_malli2k3
Offline Send Email
Nov 18, 2008
9:15 am
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help