在 Linux 中使用声卡。理解第一个例子(为什么不是 UB?)
Working with sound card in Linux. Understanding first example (Why not UB?)
我正在阅读 ALSA
tutorial 并且遇到了一些误解。在 2. Basic PCM audio
部分有一个代码示例:
snd_pcm_t *pcm_handle; //uninitialized
snd_pcm_stream_t stream = SND_PCM_STREAM_PLAYBACK;
snd_pcm_hw_params_t *hwparams;
char *pcm_name;
pcm_name = strdup("plughw:0,0");
snd_pcm_hw_params_alloca(&hwparams); //macro, must be ok
if (snd_pcm_open(&pcm_handle, pcm_name, stream, 0) < 0) { // <--- !!HERE!!
fprintf(stderr, "Error opening PCM device %s\n", pcm_name);
return(-1);
}
我不明白为什么不是UB。我们将单元化的 &pcm_handle
传递给应该是 UB 的 snd_pcm_open(&pcm_handle, pcm_name, stream, 0)
。为什么可以?
你不是 "using an uninitialized pointer"。您正在使用它的地址来为它存储一个值。指针是一条红鲱鱼。这与 int x; scanf("%d", &x);
没有区别
我正在阅读 ALSA
tutorial 并且遇到了一些误解。在 2. Basic PCM audio
部分有一个代码示例:
snd_pcm_t *pcm_handle; //uninitialized
snd_pcm_stream_t stream = SND_PCM_STREAM_PLAYBACK;
snd_pcm_hw_params_t *hwparams;
char *pcm_name;
pcm_name = strdup("plughw:0,0");
snd_pcm_hw_params_alloca(&hwparams); //macro, must be ok
if (snd_pcm_open(&pcm_handle, pcm_name, stream, 0) < 0) { // <--- !!HERE!!
fprintf(stderr, "Error opening PCM device %s\n", pcm_name);
return(-1);
}
我不明白为什么不是UB。我们将单元化的 &pcm_handle
传递给应该是 UB 的 snd_pcm_open(&pcm_handle, pcm_name, stream, 0)
。为什么可以?
你不是 "using an uninitialized pointer"。您正在使用它的地址来为它存储一个值。指针是一条红鲱鱼。这与 int x; scanf("%d", &x);