0

[EDITED]I wrote a program in C and it should print a vertical wave by printing and modifying the vector "riga_disegno" continously.

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

int main()
{
    int riga_disegno[10], i = 0, j = 0, k = 0, length = 0;

    for(i = 0; i < 10; i++) /* Inizializza il vettore */
    {
        riga_disegno[i] = 177;
    }

    printf("Quanti periodi dell'onda vuoi stampare?");
    scanf("%d", &length);

    for(k = 0; k < length; k++)
    {

        for(j = 0; j < 10; j++) /* Assegna ad un componente vettore il disegno dell'onda */
        {
            riga_disegno[j] = 254;

            for(i=0; i < 10; i++)
            {
                putchar(riga_disegno[i]); /* stampa il carattere [i] del vettore*/

            }
            printf("\n");

            riga_disegno[j] = 177; /* Riporta il componente al carattere di base */
        Sleep(100);
        }
        for(j = 9; j >= 0; j--) /* Assegna ad un componente vettore il disegno dell'onda */
        {
            riga_disegno[j] = 254;

            for(i=0; i < 10; i++)
            {
                putchar(riga_disegno[i]);
            }
            printf("\n");

            riga_disegno[j] = 177; /* Riporta il componente al carattere di base */
        Sleep(100);
        }
    }

        return 0;
}

I entered the function Sleep because the execution of the program was too fast, but now, when I compile it prints strange characters near the "wave". Thank you. Sorry for my bad english, I'm italian.

Granpasso
  • 15
  • 1
  • 6
  • 2
    When `j` is zero, `riga_disegno[j-1] = 177;` leads to Undefined Behavior. The same happens when `j` is 10 here:`riga_disegno[j+1] = 177;`. Also,`riga_disegno[j] = 254;` is wrong when `j` is -1 – Spikatrix Apr 12 '15 at 10:10
  • You are right, but why when I delete Sleep functions everything works fine? – Granpasso Apr 12 '15 at 10:27
  • 1
    Undefined Behavior means that **anything** can happen. You mustn't rely on this behavior. The program *might* work as expected, *might* do something else etc etc – Spikatrix Apr 12 '15 at 10:30
  • Why use `printf("%c", riga_disegno[i]);` when `putchar(riga_disegno[i]);` is both simpler and faster? – chqrlie Apr 12 '15 at 10:45
  • I didn't know the putchar fuction, I'm new in C programming (programming in general). Thank you chqrlie! – Granpasso Apr 12 '15 at 10:52

1 Answers1

3

Your code invokes undefined behaviour when you modify riga_disegno beyond its boundaries. You access and modify riga_disegno[-1] and riga_disegno[10]. As explained, undefined behaviour means anything can happen, including expected behaviour.

In your particular case, you wonder why the behaviour changes when you modify your code by adding function calls Sleep(). A possible explanation is that the compiler generates different code when you modify the source, especially when you invoke external functions. The side effects of modifying memory beyond array boundaries may be different because the array riga_disegno itself or the other local variables i and j may be allocated differently. Without the calls, the side effect might not affect the program's output, leading you to erroneously believe that everything works fine. With the calls, you can see some side effects of this undefined behaviour.

This is a good illustration for why one should always avoid undefined behaviour. Even if the program seems to function properly, the slightest modification can have catastrophic consequences.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • I fixed the part of the code that took the program to undefined behavior, but the fuction Sleep is still not working fine. It modifies the values of the vector, I don't know why. – Granpasso Apr 12 '15 at 11:10
  • What OS are you running on, one of your own? On Windows, linux, it is beyond reasonable doubt that Sleep() works correctly and cannot modify vectors or anything remotely like that. Sleep() is not borken, your code is, still. – Martin James Apr 12 '15 at 11:21
  • @Granpasso , Try including `windows.h` if you are on windows or `unistd.h` if you are running UNIX. See the answers [here](http://stackoverflow.com/questions/14818084/what-is-the-proper-include-for-the-function-sleep-in-c) for knowing why – Spikatrix Apr 12 '15 at 11:24
  • Solved. Thank you guys. I added windows.h library and everything works fine. But why do I have to add windows.h? – Granpasso Apr 12 '15 at 11:45
  • I'm guessing calling conventions. – Martin James Apr 12 '15 at 11:50