Where is the segmentation fault? [closed]
-
04-12-2019 - |
Question
I know not everyone has used the PortAudio API, but maybe you can find it in here anyway.
#include <iostream>
#include <math.h>
#include "portaudio.h"
int main() {
PaDeviceIndex outputDevice;
PaDeviceIndex deviceCount;
outputDevice = Pa_GetDefaultOutputDevice();
deviceCount = Pa_GetDeviceCount();
const PaDeviceInfo * outputDeviceInfo;
outputDeviceInfo = Pa_GetDeviceInfo(outputDevice);
std::cout << "Name of default output device: " << outputDeviceInfo->name << std::endl;
std::cout << "Struct version: " << outputDeviceInfo->structVersion << std::endl;
return 0;
}
*EDIT: * sorry guys, I didn't know there even was a debugger. I wasn't trying to be rude or inconsiderate. It said it's caused by line 12 and 13, the ones that begin with std::cout. I don't see what the problem is.
Solution
Null pointer dereference. Pa_GetDeviceInfo
is returning a null pointer.
Program received signal SIGSEGV, Segmentation fault.
0x080487f4 in main () at pa.cpp:12
12 std::cout << "Name of default output device: " << outputDeviceInfo->name << std::endl;
(gdb) print outputDeviceInfo
$1 = (const PaDeviceInfo *) 0x0
OTHER TIPS
From the documentation:
const PaDeviceInfo* Pa_GetDeviceInfo ( PaDeviceIndex device )
Retrieve a pointer to a
PaDeviceInfo
structure containing information about the specified device.Returns: A pointer to an immutable
PaDeviceInfo
structure. If the device parameter is out of range the function returnsNULL
.
You forgot to check for NULL
before dereferencing the pointer. Oops.