1

I'm trying to create a program in C which, after every even number, will add a "0". But I have a problem. If I insert for example only even numbers (5 or more numbers) the program crashes.

Below is the program I have right now.

I would like some indications or a code sample to point out what I did wrong and how I can fix it.

void main()
{
    int *a, i, n, m;
    printf("dimensiune=");
    scanf_s("%d", &n);
    a = (int*)malloc(n*sizeof(int));
    for (i = 0; i < n; i++)
    {
        printf("a[%d]=", i + 1);
        scanf_s("%d", &a[i]);
    }
    for (i = 0; i < n; i++)
    {
        if (a[i] % 2 == 0)
        {
            n++;                                        
            a = (int*)realloc(a, n*sizeof(int));        
            for (m = n - 1; m > i;m--)                  
            {       
                a[m + 1] = a[m];                        
            }
            a[i + 1] = 0;                               
            i++;                                        
        }
    }
    printf("\n currently you have %d numbers in this string\n", n);

    printf("your string \n");

    for (i = 0; i < n; i++)
    {
        printf("a[%d]=%d\n", i + 1, a[i]);
    }
}
LBes
  • 3,366
  • 1
  • 32
  • 66
Gabriel
  • 75
  • 2
  • 9
  • 2
    `void main()` should better be `int main(void)` or something. – Sourav Ghosh Dec 15 '15 at 13:23
  • 2
    [Please see this discussion on why not to cast the return value of `malloc()` and family in `C`.](http://stackoverflow.com/q/605845/2173917). – Sourav Ghosh Dec 15 '15 at 13:23
  • This solved , thanks ,can you explain why , whats the difference between void main() and int main(void)? – Gabriel Dec 15 '15 at 13:26
  • Be careful when using `realloc` and assigning directly back to the pointer you're reallocating, if `realloc` fails it will return `NULL` and you will loose your original pointer. – Some programmer dude Dec 15 '15 at 13:28
  • `void main()` is implementation-defined manner of defining an entry point. `int main(void)` is one of the standard. – MikeCAT Dec 15 '15 at 13:29
  • I don't think my comment can _solve_ your issue, that's just a standard conformance part. What you're experiencing is [undefined behavior](https://en.wikipedia.org/wiki/Undefined_behavior) in your code. Try to debug more. – Sourav Ghosh Dec 15 '15 at 13:31
  • I think it is more simple to do `malloc( n * 2 * sizeof(int) )`. Then you can avoid `realloc` because you have space for 0 after each value. – i486 Dec 15 '15 at 13:33

3 Answers3

1

Change:

for (m = n - 1; m > i;m--)                  
    {       
    a[m + 1] = a[m];                        
    }

to:

for (m = n - 1; m > i;m--)                  
    {       
    a[m] = a[m-1];                        
    }

I've just tested it, it's working for me, should work for you.

Shady Programmer
  • 794
  • 4
  • 9
  • 22
0

I see a problem with this loop:

    for (m = n - 1; m > i;m--)                  
    {       
        a[m + 1] = a[m];                        
    }

When you start the loop, n is the number of element in the loop. During the first iteration, m is the index of the last element of the loop. So, m+1 is after the last element, creating a buffer overflow.

Richard St-Cyr
  • 970
  • 1
  • 8
  • 14
0

Thanks for all comments I solved the bug replacing void main() with int main(void) + the solution provided by Shady Programmer.

Gabriel
  • 75
  • 2
  • 9
  • No you didn't , I've just pumped that code into my compiler and it crashes even with `int main(int argc , char** argv)` look at my answer for the proper fix. – Shady Programmer Dec 15 '15 at 14:19