1

C++

1.

#include<iostream>
#include<cstring>

int main()
{
    char s1[20]="bonapart",s2[]="boy",*k;

    std::cout<<"hey";

    k=strstr(s1,s2);
    std::cout<<k;
    
    std::cout<<"bye";
}

OUTPUT

Hey

so why program is crashing here

2.

#include<iostream>

int main()
{

    std::cout<<"hey";
    
    std::cout<<nullptr;
    
    std::cout<<"hey";
}

OUTPUT

[Error] ambiguous overload for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'std::nullptr_t')

why this error ?

C

1.

#include<stdio.h>
#include<string.h>

int main()
{
    char s1[20]="bonapart",s2[]="boy",*k;
    
    printf("Hey");
    k=strstr(s1,s2);
    printf("%s",k);
    
    printf("bye");
}

OUTPUT

Hey(null)bye

works fine here

2.

#include<stdio.h>

int main()
{
    printf("Hey");
    
    printf("%s",NULL);
    
    printf("bye");
}

OUTPUT

Hey(null)bye

works fine.

So why in C++ code is crashing at runtime due to null string though C works fine by just putting null for null string.

Abhishek Mane
  • 619
  • 7
  • 20
  • When printing a string, the pointer passed will be dereferenced. A null pointer can't be dereferenced, attempting to do so will lead to *undefined behavior*. – Some programmer dude Mar 28 '21 at 06:10
  • 1
    Also, There are two overloads for the `<<` "output" operator that can take a pointer: 1) The C-style null-terminated string output operator; And 2) The generic pointer output operator. When you use `nullptr` which one should be used? – Some programmer dude Mar 28 '21 at 06:11
  • 5
    "So why in C++ code is crashing at runtime due to null string though C works fine by just putting null for null string." Because both of them have undefined behaviour. – Antti Haapala -- Слава Україні Mar 28 '21 at 06:15
  • `std::cout << static_cast(nullptr);` – Eljay Mar 28 '21 at 13:14
  • @Someprogrammerdude thanks first of all, in both c and c++ null and nullptr can't be defrenced and in both languages they will invoked undefined behaviour ? and also printing on output screen "(null)" in C language is part of undefined behaviour ? – Abhishek Mane Mar 29 '21 at 05:29
  • @AnttiHaapala and also printing on output screen "(null)" in C language is part of undefined behaviour ? – Abhishek Mane Mar 29 '21 at 05:32
  • @Someprogrammerdude I can't understand your 2nd comment properly will you please put some extra light on that – Abhishek Mane Mar 29 '21 at 05:33
  • 2
    [Output streams](https://en.cppreference.com/w/cpp/io/basic_ostream) have multiple overloads for the "output" operator `<<`. [Some which is used for strings](https://en.cppreference.com/w/cpp/io/basic_ostream/operator_ltlt2), and [some which are used for other things](https://en.cppreference.com/w/cpp/io/basic_ostream/operator_ltlt). One of the string overloads take a pointer as argument, and so does one of the others. `nullptr` can be used for *both* types of pointers. But which one should be used? That's the *ambiguity* error you get, the compiler can't pick one or the other. – Some programmer dude Mar 29 '21 at 05:46
  • 2
    It would not be ambiguous if you used a more modern compiler which supports the C++17 standard, as it has specified an overload for `<<` that explicitly takes a `nullptr_t` argument, which is the type of `nullptr`. – Some programmer dude Mar 29 '21 at 05:46
  • @Someprogrammerdude I got it now, Thanks – Abhishek Mane Mar 29 '21 at 07:38
  • @Someprogrammerdude "When printing a string, the pointer passed will be dereferenced. A null pointer can't be dereferenced, attempting to do so will lead to undefined behavior" this valid for both C and C++ or for just C++? – Abhishek Mane Mar 29 '21 at 07:39
  • 1
    Yes. Null pointer can't be dereferenced in neither C nor C++. The behavior you're seeing in the C program is because of an *implementation specific extension*. IIRC it's not mandated by the C specification, and passing a null pointer might as well crash. – Some programmer dude Mar 29 '21 at 08:00
  • 1
    and it does often crash as explained in the duplicate. – Antti Haapala -- Слава Україні Mar 29 '21 at 09:39
  • thanks @Someprogrammerdude – Abhishek Mane Mar 29 '21 at 09:45
  • @Someprogrammerdude char a=3, *p ; p=nullptr ; std::cout<<"hey"<

    – Abhishek Mane Mar 29 '21 at 11:35
  • 1
    @AbhishekMane That code calls one of the string overloads of the `<<` operator for `p`. This overload will dereference the pointer to print the supposedly null-terminated string. That will not work since `p` is a null pointer. – Some programmer dude Mar 29 '21 at 12:58
  • @Someprogrammerdude I got it thanks – Abhishek Mane Mar 30 '21 at 05:10
  • @Someprogrammerdude printf("%f",2); printf("%f",0); printf("%f",NULL); all these 3 statements have output is 0.000000 (in context of C ) why this ? – Abhishek Mane Mar 30 '21 at 05:13
  • @AbhishekMane In most systems a null pointer is equivalent to the integer value zero. In C the macro `NULL` is often defined as `((void *) 0 )`. – Some programmer dude Mar 30 '21 at 05:17
  • Also note that to print a pointer (which `NULL` really is) then you need the `%p` format specifier. Mismatching format specifier and argument type (like using `%d` for a pointer) really leads to *undefined behavior*. – Some programmer dude Mar 30 '21 at 05:19
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/230525/discussion-between-abhishek-mane-and-some-programmer-dude). – Abhishek Mane Mar 30 '21 at 05:23
  • @Someprogrammerdude sorry I don't know that you do not do chat just stack Overflow notify me that avoid extended discussion in comments so. let it be – Abhishek Mane Mar 30 '21 at 05:46
  • @Someprogrammerdude thanks I got it, printf("%f",0) -> 0.000000 this is case of undefined behaviour right ? and also I checked printf("%p",NULL) it works fine and output is 0000..... total 16 digits with zeroes is there any reason for these 16 digits regarding NULL just curious ? – Abhishek Mane Mar 30 '21 at 05:46
  • Yes it's UB (Undefined Behavior) because of the mismatchinbg format and argument. And 16 digits because you're on a 64-bit system with 64-bit pointers (16 hexadecimal digits). – Some programmer dude Mar 30 '21 at 06:00
  • @Someprogrammerdude I got it , Thanks for all help I really mean it. – Abhishek Mane Mar 30 '21 at 06:19
  • No problem, that's why we're here! :) – Some programmer dude Mar 30 '21 at 07:26
  • @Someprogrammerdude but in C++ int *a=nullptr; cout< – Abhishek Mane Mar 31 '21 at 07:28
  • `int a` or `int* a`? There's a big difference. Also, output though a C++ stream with `<<` is going to be different from `printf`. – Some programmer dude Mar 31 '21 at 07:54
  • @Someprogrammerdude sorry I made writing mistake int *a=nullptr; only – Abhishek Mane Mar 31 '21 at 08:48
  • @Someprogrammerdude https://stackoverflow.com/q/66998504/11862989 can you please answer this question – Abhishek Mane Apr 08 '21 at 08:17
  • @AnttiHaapala https://stackoverflow.com/q/67772282/11862989 can you answer this question please – Abhishek Mane Jun 02 '21 at 12:26

0 Answers0