strlen vs sizeof

  1. strlen method is used to find the length of an array whereas sizeof method is used to find the actual size of data, eg:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    strcpy(str,"this is a test");
    lengthStr = strlen(str);
    // lengthStr is 14 dont include \0
    sizeStr = sizeof(str);
    // sizeStr is 15 iclude \0
    char myStr[20] = "this is a test";
    lengthMyStr = strlen(myStr);
    // lengthMyStr is 14
    sizeMyStr = sizeof(myStr);
    // sizeMyStr is 20

  2. strlen count the numbers of character in a string while sizeof return the size of an operand.
  3. strlen looks for the null value of variable but sizeof doesn't care about the variable value.

char [] vs char*

  • char is a primitive data type in c++, can hold a single character while char[] is an array of characters, whereas char* is a pointer to one or more char objects and can be used as a string.
  • eg,
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
    char charArray[] = "Hello";
    char *charPointor = charArray;

    cout << charArray << " " << charPointor << endl;
    cout << *charArray << " " << *charPointor << endl;
    return 0;
    }
    the output is: Hello Hello and H H. The char* ** in C++ is a pointer used to point to the first character of the character array.** ## constructor and shadow copying An questionable program with an error in copying constructor.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>MyString
#include <string.h>
using namespace std;

class MyString
{
private:
char *buffer;
//declare a pointer point character string

public:
MyString(const char *initString)
{
buffer = NULL;
if (initString != NULL)
{
buffer = new char[strlen(initString) + 1];
//allocate address for buffer
strcpy(buffer, initString);
//copy initString to buffer
}
}

~MyString() // Destructor
{
cout << "Invoking destructor, clearing up" << endl;
delete[] buffer;
}

int GetLength()
{
return strlen(buffer);
}

const char *GetString()
{
return buffer;
}
};

void UseMyString(MyString str)
{
cout << "String buffer in MyString is " << str.GetLength();
cout << " characters long" << endl;
cout << "buffer contains: " << str.GetString() << endl;

return;
}

int main()
{
MyString sayHello("Hello from String Class");
UseMyString(sayHello);//error here

return 0;
}

using the object sayHello of class MyString which delegated to function UssMyString(), invoked in UseMyString(sayHello);. Delegating work to this function result in object sayHello in main() to be copied into parameter str. This operation take str as a parameter by value and not by reference(&). Hence the pointer value in sayHello.buffer has simply been copied to str, that is, sayHello.buffer points the same memory location as str.buffer. Now the two objects of class MySyring both point the same memory location. When main() ends, sayHello goes out of scope and is destroyed. This time, however, delete[] buffer; repeats a call to delete on a memory address that is no longer valid.

note that: in void UseMyString(MyString str) ,str is similar to MyString in MyString(const char *initString)