c - Cannot understand why variable i is set to zero? -


i cannot understand why i set 0 right after array initialized zero.

the program working fine because have reinitialized value of k i. not find out why i becomes 0. , why memset() clearing array, or setting array 0?

#include <stdio.h> #include <stdlib.h> #include <windows.h> int main() {     long long int = 123456789;     long long int j = 987654321;     long long int cnt = 0;     int array[9] ;     int xyz, k, x, rem, se;     xyz = 0;   //   printf("i = %llf", i);     (i; (i < j) && (cnt < 100000); i++)     {         k = i;         x = 0;         (se = 0; se <= 9; se++)         {             array[se] = 0;         }  /*************************************************/          = k;   // here becomes zero. why?  /************************************************/          //memset(array, 0, 9);             while(k != 0)         {             rem = k % 10;             for(se = 0; se <= 9; se++)             {                 if(rem == array[se])                 {                     xyz = 1;                     break;                 }              }             if(rem == array[se])                 {                     xyz = 1;                     break;                 }             array[x++] = rem;             k = k / 10;         }         if (xyz != 1)         {             cnt++;         //    printf("cnt = %d  ", cnt);         //    printf("the value = %lld\n", i);         //    sleep(10);         }         xyz = 0;         // printf("the value = %lld\n", i);         // printf("cnt = %d  \n", cnt);         fflush(stdin);     }     printf("the value = %lld \n", i-1);     return 0; } 

you have buffer overflow; since buffer on stack, might regarded form of stack overflow.

int array[9];    // elements array[0] .. array[8] ...  (se = 0; se <= 9; se++) {     array[se] = 0; } 

it must k being overwritten 0; i assigned 0 because that's value in k. invoke 'undefined behaviour' when write outside boundaries of array, did here. undefined behaviour means can happen , ok. sometimes, seem work; sometimes, there'll unexpected side effects. avoid 'undefined behaviour' @ costs.

the idiomatic for loop is:

for (se = 0; se < 9; se++)     array[se] = 0; 

note < instead of <=.

there disagree. see comments.


you ask (commented out) call memset():

//memset(array, 0, 9); 

the third parameter memset() size in bytes of area of memory set. setting 9 bytes out of total of (probably) 36 in array, unlikely wanted.

here, array defined in same function safe , sensible write:

memset(array, 0, sizeof(array)); 

if array parameter passed function, not work correctly; need different size. example:

void somefunc(int array[], int num) {     ...     memset(array, 0, num * sizeof(array[0]));     ... } 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -