Search code examples
cwindowsspawn

Simple code spawning a process in C crashes. Why?


The following program crashes. What am I doing wrong?

#include <stdio.h>
#include <process.h>

int main() {
    puts("Hello!");
    return spawnlp(0, "notepad.exe", "notepad.exe", "test.txt");
}

Solution

  • You're missing the terminating NULL to be passed as part of argument list.

    Without that terminator, the end of the argument list will not be known, system will continue reading which causes undefined behavior due to access of invalid memory locations.