Obviously I have some wrong assumptions, because this prints "strsize = 8" no matter what.
Good call. buff is a pointer to char, so sizeof(buff) will always tell you what the size of a pointer to char is, not the length of the string it points to (if it points to a string at all, which isn't a safe assumption). What you want is the strlen() function, not sizeof.
Also note that strcat() expects the destination to be a valid string, which means you code is broken. This will fix it:
char* buff = malloc(sizeof(command)+42);
buff[0] = '\0'; /* Make buff a valid string */
strcat(buff, "notify-send \"notif finished\" \"Command: '");
Finally, it's good practice to check malloc() for failure. Otherwise you'll attempt to dereference a null pointer.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
as far as I can tell, strcat depends on the string pointed to by its first parameter to be large enough to hold the two strings.
Correct, it's your job to ensure that the destination has enough room.but if I change the code to buff = malloc(sizeof(command)+2); (or pretty much any size at all, smaller than 42) the 'string' is expanded to be 42 characters long?
Expanded? No. C doesn't stop you from writing beyond the boundaries of an array. In Java, for example, you'll get an exception immediately, but C will happily write to memory you don't own and silently corrupt things. If you're lucky, you'll corrupt something that causes a fatal error, but in this case it seems as if you weren't lucky.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
I understand your confusion, but please don't be tricked into thinking that the code isn't broken when you provide insufficient space for strcat().
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Shouldn't the strlen only return 2 (or 4 with 'ls'), shouln't the string in the system(); call be cut of, and shouln't I get a segmentation fault?
Yes, yes, and yes. Or no, no, and no. Or any combination thereof. Undefined behavior meansany damn thing can happen, which includes working perfectly.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
If "malloc() doesn't guarantee anything about the data it returns a pointer to" what do you think that means about your buffer?
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
That it could contain anything?
The reference I'm using says strcat will append string2 to the end of string1, so we add a string terminator to the beginning of buff, to make sure strcat will write from buff[0], even if other elements contain a terminator. So yes, buff[0] = '\0' is necessary.
Guess I could figured that out : )
And you did, too! :)
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944