-7
int main ()
{
     char* tab=new char[14] ;
     cout << " lenght with sizeof: "<<sizeof(tab)<<endl;
     cout << " length with strlen: "<<strlen(tab)<<endl;
     
     system(" pause");
     return 0;
}

I got the output:

length with sizeof: 4

length with strlen: 30

I expect the result of sizeof but not what return strlen!

For those who will hurry to publish that it's a duplicate question. I want to say that it's not the opportunity at all. Because I know about compile time and run-time and many other things concerning strlen and sizeof however I cannot find explanation to this result.

Thank you for help in advance.

Community
  • 1
  • 1
xtensa1408
  • 584
  • 12
  • 32

1 Answers1

4

Since you're allocating a char array but do not initialize it, strlen() will count from the beginning of the tab pointer to the first NUL character. So the result depends on the contents of your program's heap.

Tore Olsen
  • 2,153
  • 2
  • 22
  • 35