Conversation
yegorjke
left a comment
There was a problem hiding this comment.
remove unnecessary couts and endls. make yours "print" function working. fix other comments
| auto adglob{34.5}; | ||
|
|
||
| // The function doesn't work because global variables sent with copy of value | ||
| // template <typename T> inline void print_vars(string name, T var, T * pvar, T & rvar) |
There was a problem hiding this comment.
I took a look at this function and it seems me overhead. I don't understand why you need to have all these passed params (I followed that these are a value, pointer, and reference of the same variable, right?), but if consider it as a real function of your library, do you really always have them in present on the stack? If not, you will have to pass them because the function demands (even so, print_vars("var", var, &var, var)).
There was a problem hiding this comment.
If you don't want to pass params as values then consider passing them as [const] reference.
There was a problem hiding this comment.
If I were you I would like to have the function which takes 1 required argument var, it is a variable being printed, and the optional one name or description. The function itself will perform printing the value, the address in memory, and the sizeof (and type of variable (see typeid) but it is optional). Some kind of this:
template<typename T>
void print_variable_info(const T& var, const std::string& name = "") {
std::string prefix = (name.empty()) ? "" : ("[" + name + "] ");
std::cout << prefix << "Value: " << var << "\n";
std::cout << prefix << "Address: " << &var << "\n"; // Q: why it won't print address of var with type "char"? what is needed to do?
std::cout << prefix << "Type: " << typeid(var).name() << "\n";
std::cout << prefix << "Sizeof: " << sizeof(var) << std::endl;
}It doesn't matter what you will pass to this function. If it confuses you how it will work with a pointer, then I would say that a pointer is a variable as well, but the value of a pointer is address (and the pointer has its own address as well as a common variable)
There was a problem hiding this comment.
Done. The source code is changed to use template function.
There was a problem hiding this comment.
std::cout << prefix << "Address: " << &var << "\n"; doesn't work because the compiler interpret &char variable as c-style string and tries to print whole string instead of address of pointer. To print address of char pointer we need to use static_cast<const void *>.
|
|
||
| cout << "---------------------------------------------------------------------" << endl; | ||
|
|
||
| cout << "Value of iglob: " << iglob << endl; |
There was a problem hiding this comment.
remake it with the proposed function above
| //ERROR: error: invalid conversion from ‘const char*’ to ‘int’ [-fpermissive] | ||
|
|
||
| return 0; | ||
| } No newline at end of file |
There was a problem hiding this comment.
add to habits leaving an extra newline at the EOF.
| cout << "c - const" << endl; | ||
| cout << "s - static" << endl; | ||
| cout << "glob - global" << endl; | ||
| cout << "loc - local" << endl << endl; |
There was a problem hiding this comment.
a lot of "endl"s force the IO buffer to flush. it costs the time of your [future] programs. it is useful to flush the buffer when you are sure that it must be done. consider of "\n" in some places.
There was a problem hiding this comment.
Got it. Thanks. I will use the advice in the future.
| unsigned char * puchar{&ucloc}; | ||
| cout << "*puchar <- ucloc " << endl << "*puchar=" << int (*puchar) << endl; | ||
| *puchar=*puchar +1; | ||
| cout << "increment *puchar " << endl << "*puchar=" << int (*puchar) << endl; |
There was a problem hiding this comment.
Added print of ucloc after pointer value increment.
| float floc{12.34}; | ||
| double dloc{12.3456}; | ||
| char cloc{'l'}; | ||
| unsigned char ucloc{'k'}; |
There was a problem hiding this comment.
what if ucloc is assigned with a small integer (for example, 12)? what will be printed?
There was a problem hiding this comment.
Nothing will be printed until k>32. Because from 0 to 31 are not printable signs, 32 is space and from 33 are printable signs.
|
Made corrections in accordance with the comments |
506e7a0 to
6b7a7aa
Compare
6b7a7aa to
c7efbf5
Compare
Finish the first task.