Skip to content

Conversation

@SilentGitUser
Copy link
Owner

Finish the first task.

  • First part of task is made for integer and double types
  • All compiler and runtime errors are commented

Copy link

@yegorjke yegorjke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)).

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't want to pass params as values then consider passing them as [const] reference.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. The source code is changed to use template function.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remake it with the proposed function above

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... and all "cout"s are below

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

//ERROR: error: invalid conversion from ‘const char*’ to ‘int’ [-fpermissive]

return 0;
} No newline at end of file
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add to habits leaving an extra newline at the EOF.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

cout << "c - const" << endl;
cout << "s - static" << endl;
cout << "glob - global" << endl;
cout << "loc - local" << endl << endl;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, but what is happened to ucloc?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added print of ucloc after pointer value increment.

float floc{12.34};
double dloc{12.3456};
char cloc{'l'};
unsigned char ucloc{'k'};
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if ucloc is assigned with a small integer (for example, 12)? what will be printed?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@SilentGitUser SilentGitUser marked this pull request as draft January 11, 2022 21:36
@SilentGitUser
Copy link
Owner Author

Made corrections in accordance with the comments

@SilentGitUser SilentGitUser force-pushed the 1_constants_variables_and_types branch from 506e7a0 to 6b7a7aa Compare January 13, 2022 07:48
@SilentGitUser SilentGitUser marked this pull request as ready for review January 13, 2022 07:49
@SilentGitUser SilentGitUser force-pushed the 1_constants_variables_and_types branch from 6b7a7aa to c7efbf5 Compare January 13, 2022 09:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants