This question already has answers here:
Strlen returns unreasonable number
(4 answers)
Closed 8 years ago.
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.
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.
Related
This question already has answers here:
What is the purpose of allocating a specific amount of memory for arrays in C++?
(5 answers)
Closed 5 years ago.
I examine that C array maybe have some extra bytes at tail.
There are my code
int a = 5;
int test[] = {1,2,3,4};
int b = 5;
test[-1] = 11;
test[4] = 11;
cout << b << endl; // 11
cout << a << endl; // 5
You can see the running result there
the value of b is changed through changing test[-1]'s value. But when I change test[4]'s value, the value of a doesn't change;
I use gdb to check their addresses, found that
In g++ 6.4.0, the address of a substract address of test[4] is 8 bytes
In clang++ 3.8.1, the address of a substract address of test[4] is 4 bytes
So, I am curious that why the array has some bytes at tail?
Thanks #Peter A.Schneider to explaining the question.
It is surely a UB , But it is just a experimental code. This isn't a discuss for practical code.
generally,variables at the runtime stack are close together. b is close to test, but why 'a' is not close to 'test+3'. That's the key of the problem.
test[-1] = 11;
test[4] = 11;
This is undefined behavior.(Meaning anything could have happened). In your case you changed the value of b because they are adjacent in the memory where they are allocated. But you shouldn't rely on it. Because this may blow up your program or results in erroneous code behavior most of the time.
The UB you have is because `Accessing an array index out of bound in undefined behavior."
This question already has answers here:
Accessing an array out of bounds gives no error, why?
(18 answers)
Closed 6 years ago.
#include <iostream>
using namespace std;
int main(){
int a[3], no;
cout << "Index Value\n";
for(int i = 0; i < 100; i++){
cin >> no;
a[i] = no;
cout << i << "\t" << a[i] << endl;
}
return 0;
}
Here I initialized a[ 3 ]. In for loop, I'm feeding input 100 times to a[ ], exceeding the indices of [ 3 ].
Why don't it give segmentation error right after when i equals 4.
Input
1 2 3 4 5 6 7
Output
Index Value
0 1
1 2
2 3
4 0
5 5
6 6
7 7
Output is wrong when Index equals 4. Printed 0 . Expected 4
Unfortunately for the debugging programmer, C and C++ programs don't usually segfault when you write past the end of an array. Instead it will usually silently write over whatever the pointer arithmetic as up to -- if the OS allows it. This often overwrites other variables or even program code, causing confusing and unpredictable errors.
I have used the word "usually" here because according to the standards this is "undefined behaviour" -- that is, the compiler and runtime can do anything they like.
When developing and testing, it can be very useful to use a library such as electricfence, which puts extra checks into memory operations and would make your program fail in the way you expect.
This question already has answers here:
Pointer array and sizeof confusion
(5 answers)
Closed 8 years ago.
I wrote a code a fragment of which is shown below. I don't understand why it does not print 800 for the pointer variable p.
double *p = new double [100];
double q[10];
printf("Sizeof(p) = %d\n", sizeof(p)); // prints 4
printf("Sizeof(q) = %d\n", sizeof(q)); // prints 80
I understand why it prints 80 for q (8 bytes/double * 10) but why not 800 for p? An associated question would be, how does the compiler know how much space to deallocate when it encounters the delete for p?
delete [] p;
Because the actual pointer address is stored in 4 bytes. If you wanted the size of what p points to, you would say:
sizeof(*p);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I've tried this, but I having no luck, I'm am looking to get user input 5 letters and then print them out.
string input = "";
const int max = 5;
char string[max] = { };
cout << "Please enter 5 letters: " << endl;
cin.getline(string, max, '\n');
cout << "Your letters :" << string[max];
I think I figured out what's not working:
First, you are printing out string[max] at the end. Since string is a char[] of size max, it actually has no data at index max--its indices are 0 to max-1. You are actually printing out a random character from whatever happens to be in memory immediately after the characters of your string variable.
So instead of << string[max] in the last line, it should be << string.
Second, after making that change, it will still seem to print only 4 characters, instead of the 5 that were entered. This is because strings in the form of char[]s have a null terminator. So since you are telling cin.getline to only fill up 5 characters in string, it fills the first 4 with actual characters from input, and then the last character is '\0'.
So if the input is "hello", then string will contain the following values: { 'h', 'e', 'l', 'l', '\0' }. And then when you print it, there are, of course, really only 4 characters in the array.
And two notes: string input is not used anywhere in your program, so it should be taken out of the question. And also, you really should call your char string[max] variable something else, to reduce confusion.
I hope this helps!
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
C++: “std::endl” vs “\n”
I'm wondering if there is any significant difference between these two ways to print newline :
cout << endl; //approach1
cout << "\n"; //approach2
Is there any practical difference?
Yes, they're different.
"\n" is just a string of length 1 that gets appended to stdout.
std::endl, instead, is an object that will cause to append the newline character ("\n") AND to flush stdout buffer. For this reason it will take more processing.