c - what's the difference between the two code block? -
in program , use basename function partition. first , write if sentence this:
if (!strncmp(buf, basename("/dev/mmcblk0p3"), strlen(basename("/dev/mmcblk0p3"))) { ret = 1; } else { ret = 0; }
buf point string "mmcblk0p3" , ret = 0, using gdb, found basename("/dev/mmcblk0p3") returned weird string, when changed program this:
char *p = null; p = basename("/dev/mmcblk0p3"); if (!strncmp(buf, p, strlen(p)) { ret = 1; } else { ret = 0; }
the ret 1, program runs normal. difference? basename cannot used ? compiling environment armel7v/gcc.
one consequence of using non-reentrant function basename returns string subsequent invocations invalidate prior results. need make copy of return value before making second call, or stop using old value after second call has returned. because basename has 1 internal buffer store result, assuming not modify parameter ( looks case program, because not crash on string literal; still, it's undefined behavior).
modify program follows make portable:
char *p = null; char pathname[] = "/dev/mmcblk0p3"; p = basename(pathname); if (!strncmp(buf, p, strlen(p)) { ret = 1; } else { ret = 0; }
Comments
Post a Comment