Alright so I am getting a weird problem with the following code

int toDB[2];
int fromDB[2];
char * param1;
char * param2;

err = pipe(toDB);
if(err==-1)
{
    printf("Error on pipe creation: %d\n", errno);
    exit(1);
}
sprintf(param1, "%d", toDB[0]);

err = pipe(fromDB);
if(err==-1)
{
    printf("Error on pipe creation: %d\n", errno);
    exit(1);
}
sprintf(param2, "%d", fromDB[1]);

The last line of code sprintf(param2, "%d", fromDB[1]); is the line causing the problem, if it is commented out the code runs, if it is not commented out I get a core dump, can anyone see what i am doing wrong?

NOTE: This is not the full code.

Recommended Answers

All 5 Replies

You can not write to either of param1 or param2 without pointing them at valid memory. When you create a raw pointer (i.e. char *) it most likely does not point to some place you should (or can) write to.

Try something like:

char param1[SIZE] = {0}, param2[SIZE] = {0};

/* and then */
snprintf (param1, SIZE, "%d", toDB[0]);

but the first sprintf works, the one that writes toDB[0] to param1

but the first sprintf works, the one that writes toDB[0] to param1

The first sprintf causes undefined behavior just like the second does. Some times undefined behavior means that you get a segfault and some times it seems to work fine. That doesn't change the fact that it's undefined behavior.

Thank you for the helpp

On the last line you are printing fromDB[1]. It should be fromDB[0] I would think.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.