2017-06-24 10 views
2

上にあるとき、それはゼロに行くことになりここで(C)で、関連するコードがあります:ランダム配列の要素を交換すると、配列がスタック

uint8_t sort_array[SORT_ARRAY_LEN]; 
for (int i = 0; i < SORT_ARRAY_LEN; i++) { 
    sort_array[i] = rand() % 256; 
} 

// Every frame 
int el1_i = rand() % SORT_ARRAY_LEN; 
int el2_i = rand() % SORT_ARRAY_LEN; 
uint8_t temp = sort_array[el1_i]; 
sort_array[el1_i] = sort_array[el2_i]; 
sort_array[el2_i] = temp; 

私は十分な長さのためにこれを実行すると、要素がように見えるだろうがすべての要素がゼロになるまで時間の経過とともに消えます。これがなぜ起こるか私には理解できないが、ランダムなものをコメントアウトのしばらく後、私は

uint8_t sort_array[SORT_ARRAY_LEN]; 

uint8_t *sort_array = malloc(sizeof(u8) * SORT_ARRAY_LEN); 

への修正問題を変更することを発見しました。なぜ地球上で起こっているのですか?ここ

完全なコードである:

#include <SDL2/SDL.h> 
#include <stdint.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 
#include <unistd.h> 

#define SCREEN_WIDTH 640 
#define SCREEN_HEIGHT 480 

#define SORT_ARRAY_LEN 160 

#define MAX_TICK_SAMPLES 100 

void error_quit(char * message) { 
    fprintf(stderr, "%s SDL_Error:\n\t%s\n", message, SDL_GetError()); 
    exit(1); 
} 

void draw_array(SDL_Surface * screen_surface, 
       uint8_t * sort_array, int sa_len) 
{ 
    int bar_width = SCREEN_WIDTH/sa_len; 
    for (int i = 0; i < sa_len; i++) { 
     int bar_height = (SCREEN_HEIGHT * sort_array[i])/256; 
     SDL_Surface * bar_surface = SDL_CreateRGBSurface(
      0, bar_width, bar_height, 32, 0, 0, 0, 0); 
     SDL_FillRect(bar_surface, 0, 
      SDL_MapRGB(bar_surface->format, 255, 0, 0)); 
     SDL_Rect bar_rect; 
     bar_rect.x = i * bar_width; 
     bar_rect.w = bar_width; 
     bar_rect.y = SCREEN_HEIGHT - bar_height; 
     bar_rect.h = bar_height; 
     SDL_BlitSurface(bar_surface, 0, screen_surface, &bar_rect); 
     SDL_FreeSurface(bar_surface); 
    } 
} 

int main() { 
    /* INITIALIZATION */ 
    SDL_Window * window; 
    SDL_Surface * screen_surface; 
    if (SDL_Init(SDL_INIT_VIDEO) != 0) { 
     error_quit("Trouble initializing."); 
    } 
    window = SDL_CreateWindow(
     "Sorting Visualizer", 
     SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 
     SCREEN_WIDTH, SCREEN_HEIGHT, 
     SDL_WINDOW_SHOWN 
    ); 
    if (window == 0) { 
     error_quit("Trouble creating the window."); 
    } 
    screen_surface = SDL_GetWindowSurface(window); 
    if (screen_surface == 0) { 
     error_quit("Trouble getting window surface."); 
    } 

    srand(time(0)); 

    uint8_t sort_array[SORT_ARRAY_LEN]; 
    //uint8_t * sort_array = malloc(sizeof(uint8_t) * SORT_ARRAY_LEN); 
    for (int i = 0; i < SORT_ARRAY_LEN; i++) { 
     sort_array[i] = rand() % 256; 
    } 

    /* FRAMERATE SETUP */ 
    int tick_index = 0; 
    int tick_sum = 0; 
    int tick_list[MAX_TICK_SAMPLES]; 
    for (int i = 0; i < MAX_TICK_SAMPLES; i++) { 
     tick_list[i] = 0; 
    } 

    printf("[ "); 
    for (int i = 0; i < SORT_ARRAY_LEN; i++) { 
     printf("%d, ", sort_array[i]); 
    } 
    printf("]\n"); 

    /* MAIN LOOP */ 
    uint8_t running = 0xFF; 
    SDL_Event event; 
    while (running) { 
     int frame_start_time = SDL_GetTicks(); 
     /* PROCESS EVENTS */ 
     while (SDL_PollEvent(&event) != 0) { 
      if (event.type == SDL_QUIT) { 
       running = 0x00; 
      } 
     } 
     /* SORTING */ 
     int el1_i = rand() % SORT_ARRAY_LEN; 
     int el2_i = rand() % SORT_ARRAY_LEN; 
     uint8_t temp = sort_array[el1_i]; 
     sort_array[el1_i] = sort_array[el2_i]; 
     sort_array[el2_i] = temp; 
     /* DRAWING */ 
     SDL_FillRect(
      screen_surface, 0, 
      SDL_MapRGB(screen_surface->format, 255, 255, 255) 
     ); 
     draw_array(screen_surface, sort_array, SORT_ARRAY_LEN); 
     SDL_UpdateWindowSurface(window); 
     /* FRAMERATE */ 
     int frame_time = SDL_GetTicks() - frame_start_time; 
     tick_sum -= tick_list[tick_index]; 
     tick_sum += frame_time; 
     tick_list[tick_index] = frame_time; 
     if (tick_index++ == MAX_TICK_SAMPLES) { 
      tick_index = 0; 
     } 
     if (SDL_GetTicks() % 50 == 0) { 
      //printf("%f\n", (double)tick_sum/MAX_TICK_SAMPLES); 
     } 
    } 

    /* CLEANUP */ 
    SDL_DestroyWindow(window); 
    SDL_Quit(); 

    printf("[ "); 
    for (int i = 0; i < SORT_ARRAY_LEN; i++) { 
     printf("%d, ", sort_array[i]); 
    } 
    printf("]\n"); 

    return 0; 
} 
+0

'sort_array'が宣言された関数は復帰しますか? – zwol

+0

これはすべて現在のmain()にあります。 – pixlark

+0

問題を示す例の完全なコードを投稿してください。 – chqrlie

答えて

1

main機能における自動ストレージで定義の配列。ヒープに移動しても効果はありません。

if (tick_index++ == MAX_TICK_SAMPLES) { 
    tick_index = 0; 
} 

このテストでは、1でオフになって、それはする必要があります:あなたは、カウントを観察することは...ここで

それは他の場所でゼロ値を持つ配列を上書きするコードのバグの副作用も:

if (++tick_index == MAX_TICK_SAMPLES) { 
    tick_index = 0; 
} 

このエラーは、おそらく最終的には、アレイ内の別の場所にランダムに移動されます他の配列の最初のエントリに0を格納する、すべてのMAX_TICK_SAMPLES+1ランoverfowバッファを引き起こします。

これは素晴らしいバグです!配列の内容は時間の経過とともに腐敗しています...

+0

まあ、私は何を言うべきか分からない。スタック割り当てをヒープ割り当てに置き換えると、それは動作します。私はそうしても何も変えていない。 – pixlark

+0

@pixlark:私はそれを打ちました。回答が更新されました。 – chqrlie

+0

聖なる牛!本当にありがとう、それは信じられないほどのバグです。 – pixlark

関連する問題