C++ sizeof C-style string / char array - optimization

后端 未结 2 1639
失恋的感觉
失恋的感觉 2021-01-29 06:56

I\'m a student at university. I work mostly with Java, C++ is very new to me, so I probably make many silly mistakes and I have upcoming exams to cope with. Don\'t be too harsh

2条回答
  •  不思量自难忘°
    2021-01-29 07:48

    sizeof gives you a number of bytes which c/c++ need to keep the object in memory. In you r case (though you have not shown it) it looks like name, address, and phone are pointers to char:

    struct Person {
      char *name, *address, *phone;
    }
    

    a pointer is a variable which keeps an address of another object. So, depending on the underlying system it could occupy 32 bits (4 bytes) or 64 bite (8 bytes) (or some other number). In this case the sizeof struct person will be for 64-bit system -- 24. (3 pointers per 8 bytes each). This corresponds to your results.

    The sizeof provides you with a shallow size calculation. Your strings are pointed by the those pointers and their lengths are not included. So, potentially you need to create a member function which will calculate those for you, i.e.

    struct Person {
       char *name, *address, *phone;
       int getSize() {
          return strlen(name) + strlen(address) + strlen(phone);
       }
    };
    

    And as mentioned in the comments before, every char *string in c/c++ must have a termination character ('\0') which tells the program where the string ends. So, if you allocate space for a string, you should provide space for it as well (+ 1 to the length). And you have to make sure that this character is written as '\0'. if you use library functions to copy strings, they will take car of it, otherwise you need to do it manually.

    void setName(const char *n) {
      name = new char[strlen(n) + 1]; // includes needed '0', if exists in 'n'
      strcpy(name, n); // copies the string and adds `\0` to the end
     }
    

    If you use the loop to copy chars instead of strcpy you would need to add it manually:

        name[strlen(n)] = 0;
    

提交回复
热议问题