MLSC
MLSC

Reputation: 5972

why is "char" a bad programming practice in struct types?

I see in c++ programming language some recommendations like "don't use char in struct type",

struct Student{
    int stdnum, FieldCode, age;
    double average, marks, res[NumOfCourses];
    char Fname[20], Lname[20], cmp[20];
};

And is better to use:

struct Student{
    int stdnum, FieldCode, age;
    double average, marks, res[NumOfCourses];
    string Fname, Lname, cmp;
};

Any other recommendations would be welcome on the matter

Thank you in advance

Upvotes: 2

Views: 677

Answers (4)

Tony Delroy
Tony Delroy

Reputation: 106236

For your student class, you have first name, last name and "cmp" - I've no idea what that's supposed to be. Clearly a student could have a first and/or last name longer than 20 characters, so by hardcoding an array of 20 elements you've already created a system that:

  • has to bother to check all inputs to make sure no attempt is made to store more than 19 characters (leaving space for a NUL), and
  • can't reliably print out any formal documents (e.g. graduation certificates) that require students' exact names.

If you don't carefully check all your input handling when modifying arrays, your program could crash, corrupt data and/or malfunction.

With std::string, you can generally just let people type into whatever fields your User Interface has, or pick up data from files, databases or the network, and store whatever you're given, print whatever you're given, add extra characters to it without worrying about crossing that threshold etc.. In extreme situations where you can't trust your input sources (e.g. accepting student data over untrusted network connections) you may still want to do some length and content checks, but you'd very rarely find it necessary to let those proliferate throughout all your code the way array-bounds checking often needs to.

There are some performance implications:

  • fixed length arrays have to be sized to your "worst supported case" size, so may waste considerable space for average content
  • strings have some extra data members, and if the textual content is larger than any internal Short String Optimisation buffer, then they may use further dynamic memory allocation (i.e. new[]), and the allocation routines might allocate more memory than you actually asked for, or be unable to effectively reuse delete[]d memory due to fragmentation
  • if a std::string implementation happens to share a buffer between std::string objects that are copied (just until one is or might be modified), then std::strings could reduce your memory usage when there are multiple copies of the student struct - but this probably only happens transiently and is only likely to help with very long strings

It's hard to conclude much from just reading about all these factors - if you care about potential performance impact in your application, you should always benchmark with your actual data and usage.

Separately, std::string is intuitive to use, supporting operators like +, +=, ==, !=, < etc., so you're more likely to be able to write correct, concise code that's easily understood and maintained.

Upvotes: 2

unwind
unwind

Reputation: 400029

Because string handling using C-level raw character arrays is hard to get right, prone to fail, and when it fail it can fail rather horribly.

With a higher-level string datatype, your code becomes easier to write, more likely to be correct. It's also often easier to get shorter code, since the string datatype does a lot of work for you that you otherwise have to do yourself.

The original question was tagged with the tag, but of course string is a construct so this answer doesn't apply to C. In C, you can choose to use some string library (glib's GString is nice) to gain similar benefits but of course you'll never have overloaded operators like in C++.

Upvotes: 8

Carl Colijn
Carl Colijn

Reputation: 1617

@unwind's answer is generally perfectly appropriate, unless it's really a sequence of 20 char's you're after. In that case a std::string might be overkill (performance-wise as well), but a std::array might still be better.

Upvotes: 3

Sunil Bojanapally
Sunil Bojanapally

Reputation: 12658

I dont think it makes difference in C as well.

Check this precious stackoverflow answer explained clearly states strings are more secure than char *.

Upvotes: 1

Related Questions