7vujy0f0hy
7vujy0f0hy

Reputation: 9119

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");
}

Upvotes: 1

Views: 62

Answers (1)

Sourav Ghosh
Sourav Ghosh

Reputation: 134346

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.

Upvotes: 5

Related Questions