I've been coding+posting here for awhile, and I'd say I'm fairly adept at the proper application of each of the following examples, but if somebody could please go over the nuances/differences between the following examples it would be much appreciated.
In the first example the code has undefined behaviour because variable has an arbitrary value.
In the second example variable pointer is an array that initialized by string literal "hello".
In the third example an array of 5 elements are being allocated in the heap and the address of the first element is assigned to variable pointer.
Also note that the storage space provided by your third example is too small to hold the string "hello"; you need an extra element for the null terminator.
With the second example, space for the null is automatically accounted for as you did not specify the array length.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
usingnamespace std;
int main() {
char arr[]="hello";
cout<<"arr="<<arr<< endl;
int len=strlen(arr);
cout<<"len="<<len<<endl;
char* ptr=newchar[len+1]; // plus one for null term
strcpy(ptr, arr);
cout <<"ptr="<<ptr<<endl;
delete [] ptr; // free memory now we're done with it
return 0;
}
I see. But how come the 1st example is behaving indefinitely? Is not declaring an array with indefinite size (I.e., with empty brackets) the same as pointing to an element and then adding data directly after it?
In other words, shouldn't the first two pieces of code behave identically?
Oh, okay. In the event that I have a class like such
1 2 3 4
class cfoo{
public:
char* m_pointer;
};
assuming that I have a constructor that dynamically allocates memory to hold the size of a string entered with getline, and assuming that proper copy constructors+destructors are implemented, would that imply that the size of an instantiation of the cfoo class would vary depending on the length of the string entered?
In other words, would cfoo str1("hello");
be larger than cfoo str2("hi");
just a word about your first example:
a pointer is no more than 4bytes variable(on 32bit systems) whatever its type is, and its value is used as an address, it is NOT an array.
using a pointer that hasn't been initialized is considered dangerous, (although when i tried it, windows did kill the program with no side effects)
however, you shouldn't use a bad pointer.
a simulation of what might happen:
for now, we know that pointer is pointing to unknown address.
when you call strcpy(), it will try to replace data pointed to by pointer with the data contained in the source string. pointer could be pointing to some essential operating system code, so when strcpy() tries to replace these data it can cause some bad side effects.
bad pointers are one of the hardest bugs found in a program, pay attention when dealing with pointers.
the proper fix to this problem is demonstrated in your example 3, the new operator allocates the needed memory and makes pointer point to it.
don't forget to delete the allocated memory when you no more need it in your program.