Pourquoi mon programme présente-t-il une erreur de segmentation ?

la programmation


J’utilise SDL 2.23.6 et gcc 12.3. Lorsque je compile et crée un lien, je ne reçois que quelques avertissements.

Citation:

avertissement : passer l’argument 3 de ‘SDL_OpenAudioDevice’ crée un pointeur à partir d’un entier sans conversion

par exemple.

Ce que j’ai essayé :

#include <stdio.h>
#include <stdlib.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_audio.h>

#define SAMPLE_RATE 44100
#define CHANNELS 1
#define BUFFER_SIZE 512

int main(int argc, char *argv[]) {
  // Initialize SDL.
  if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) {
    printf("SDL could not be initialized: %s\n", SDL_GetError());
    return 1;
  }

  // Create a window and renderer.
  SDL_Window *window = SDL_CreateWindow("Audio Spectrum", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
  SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);

  // Create an audio device.
  SDL_AudioDeviceID device = SDL_OpenAudioDevice(NULL, 0, AUDIO_S16SYS, SAMPLE_RATE, BUFFER_SIZE);
  if (device == 0) {
    printf("Failed to open audio device: %s\n", SDL_GetError());
    return 1;
  }

  // Allocate memory for the audio buffer.
  short *buffer = malloc(BUFFER_SIZE * sizeof(short));

  // Create a texture to render the audio spectrum.
  SDL_Texture *texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB24, SDL_TEXTUREACCESS_TARGET, 640, 480);

  // Main loop.
  while (1) {
    // Get the audio samples from the device.
    SDL_AudioStreamGet(device, buffer, BUFFER_SIZE);

    // Calculate the audio spectrum.
    int i, j;
    for (i = 0; i < BUFFER_SIZE; i++) {
      int sample = buffer[i];
      for (j = 0; j < SAMPLE_RATE/256; j++) {
        if (sample >= j * 256 / SAMPLE_RATE) {
          // Set the pixel at (i, j) to red.
          SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
          SDL_RenderDrawPoint(renderer, i, j);
        }
      }
    }

    // Render the audio spectrum.
    SDL_RenderClear(renderer);
    SDL_RenderCopy(renderer, texture, NULL, NULL);
    SDL_RenderPresent(renderer);

    // Sleep for a millisecond.
    SDL_Delay(10);
  }

  // Free resources.
  SDL_DestroyTexture(texture);
  SDL_Free(buffer);
  SDL_CloseAudioDevice(device);
  SDL_DestroyRenderer(renderer);
  SDL_DestroyWindow(window);
  SDL_Quit();

  return 0;
}

Solution 1

À tout le moins, vous devez identifier sur QUELLE ligne les erreurs de segmentation de votre programme.
Parcourez-le avec un débogueur et si vous arrivez à quelle ligne il fait cela, vous saurez probablement pourquoi.

Vous devez également comprendre pourquoi vous recevez ces avertissements et prendre les mesures appropriées pour vous assurer qu’il n’y a pas d’avertissement.

コメント

タイトルとURLをコピーしました