0

Why is this code returning -1?

memcmp() compares block of memory and takes 3 parameters in constructor but what happens when I miss the third parameter?

int main()
{
    char ptr[] = "hello";
    char ptr1[] = "hello";

    int a = memcmp(ptr,ptr1);
    printf("%d",a);

    return 0;
}

Also the following program abruptly terminates without the third parameter:

int main()
{
    char *ptr = "hello";
    char *ptr1 = "hello";

    int a = memcmp(ptr,ptr1);
    printf("%d",a);

    return 0; 
}
ssbl
  • 43
  • 2
  • 6

2 Answers2

4

For starters, memcmp() takes three arguments: the pointers to the memory segments to be compared and the size. Although the code may compile in C (I don't think it should) it certainly doesn't compile using C++. If the code compiled, the third argument is a pretty random value and it is unlikely that the memory after these strings is the same.

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
  • Not "in C" - it is the case in C89 only. Later versions of the language prohibit this. For example, C99 p6.5.1 and note 79 say: "An identifier is a primary expression, provided it has been declared as designating an object (in which case it is an lvalue) or a function (in which case it is a function designator). [79] Thus, an undeclared identifier is a violation of the syntax." –  Dec 01 '13 at 06:38
2

It will compile neither in C nor in C++. In C, first one does compile only when you do not include <stdlib.h> and it simply invokes undefined behavior because passing arguments to a function less than that of its parameter invokes UB.

Here is the output:

enter image description here

haccks
  • 104,019
  • 25
  • 176
  • 264
  • That shouldn't even compile, unless the function has default parameters. – ctor Nov 30 '13 at 23:56
  • 1
    ctor: In C++, not in C. – SigTerm Nov 30 '13 at 23:56
  • @ctor; My answer is C specific. – haccks Nov 30 '13 at 23:57
  • @SigTerm It shan't compile in C either. –  Nov 30 '13 at 23:59
  • @haccks That doesn't matter, it shouldn't compile neither in C nor in C++. –  Nov 30 '13 at 23:59
  • I don't see why any C compiler would compile this if it's most definitely going to result in UB.. – ctor Dec 01 '13 at 00:00
  • @H2CO3; first one is compiling and giving output 1. – haccks Dec 01 '13 at 00:00
  • @H2CO3: Prove that. Quote from C standard should do. In C program you can call many functions without previously #including stdio/etc. – SigTerm Dec 01 '13 at 00:00
  • @SigTerm Haters gonna hate. –  Dec 01 '13 at 00:01
  • @haccks I'd be interested in your compiler settings. It should at least emit a warning... *at least.* (If it really compiled, then your compiler is either ancient or non-conforming.) –  Dec 01 '13 at 00:01
  • @H2CO3: For a programmer, brain is required component. Google "C implicit declaration". Also try to compile `int main(int argc, char** argv){printf ("asdf");}`. With **C** compiler. – SigTerm Dec 01 '13 at 00:03
  • @H2CO3; Yes, it is emitting the warning for few arguments but it is compiling! – haccks Dec 01 '13 at 00:03
  • @H2CO3: Here's a link for you: http://stackoverflow.com/questions/9182763/implicit-function-declarations-in-c – SigTerm Dec 01 '13 at 00:04
  • @SigTerm No need for that. –  Dec 01 '13 at 00:08
  • @haccks So again, what did you use to compile this? –  Dec 01 '13 at 00:09
  • @H2CO3; GCC 4.8.1 in C99 mode with `-Wextra`, `pedantic` and `Wall` flag only :). Try to compile with this settings and do not include ``. – haccks Dec 01 '13 at 00:12
  • @haccks That's funny; the same isn't working for me neither with `clang` nor with `gcc`, whether ([link](http://ideone.com/yR6Sxv)) or not ([link](http://ideone.com/Bi19bW)) I include that header file. –  Dec 01 '13 at 00:18
  • @H2CO3; Trust me, its compiling on my compiler. I am including my output snap. – haccks Dec 01 '13 at 00:19
  • 2
    @H2CO3: Compiles with -Wextra, -pedantic, and -Wall on Mingw64-gcc 4.8.1. How about you stop wasting time saying it shouldn't compile? It compiles on certain compilers, because of implicit declaration thing. Passing wrong number of arguments triggers UB which explains behavior OP experienced. – SigTerm Dec 01 '13 at 00:23
  • @H2CO3; I added the screen shot. – haccks Dec 01 '13 at 00:24
  • @SigTerm; Agreed. I compiled with same flags. – haccks Dec 01 '13 at 00:24
  • @SigTerm "How about you stop wasting time saying it shouldn't compile?" - and how about *you* stop wasting time being stubborn about something in which you are absolutely wrong? You are confusing versions of the language with each other. –  Dec 01 '13 at 00:26
  • @H2CO3; I am amazed that why it is not compiling for you :) – haccks Dec 01 '13 at 00:29