texts

The Partridge Family were neither partridges nor a family. Discuss.
albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: texts

Post by albinopapa » October 19th, 2017, 5:57 pm

Here is your program using std::string and some C++ features.

Code: Select all

void print( std::string S )
{
	for( const auto &c : S )
	{
		_putch( c );
	}
}

std::string scan()
{
	std::string s;
	while( true )
	{
		s.push_back( _getch() );
		if( s.back() == 13 )
			return s;
	}
}

int main()
{
	print( "Enter a number : " );
	print( "\nYou typed : " + scan() );
	
	// You can just put the ; at the end here and it will loop until you press a key.  Looks cleaner.
	while( !_kbhit() );

	return 0;
}
Really cleans things up.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Empirean
Posts: 26
Joined: September 24th, 2017, 3:23 am

Re: texts

Post by Empirean » October 19th, 2017, 10:57 pm

Thank you for showing me how to use strings albinopapa. I still dont fully grasp pointers. I know its gonna be relevant in the future since linked lists runs using pointers. Any tips?

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: texts

Post by albinopapa » October 20th, 2017, 4:25 am

Keep practicing.

Make a testbed project that you can practice all the things you want to experiment with. My biggest advice is not to worry about linked lists, always prefer std::vector lol. Seriously, unless you absolutely need a linked list for something, always use std::vector. It's an array that can grow and shrink. Lots of tests have been run and vectors are almost always faster than a linked list.

Here's my take on pointers.
Allocation:
You have stack and heap allocation. Stack allocation would be use for arrays, which can decay into a pointer. You don't have to worry about deallocation because the language pops the array from the stack when it goes out of scope. Heap allocation is manual memory management in C++. You can use malloc or new to allocate memory and free and delete to deallocate memory, but this leads to manually having to keep track of pointers. In the C++11 standard, they came out with std::unique_ptr and std::shared_ptr which manage the lifetime of your allocations by using new/delete under the hood. When a unique_ptr goes out of scope, the memory is released automatically like the stack allocated pointers.

Usage:
You can use raw pointers, but most of the time you will be initializing them as unique_ptr or shared_ptr. The only time one would need to use a raw pointer is when passing a pointer to a function, either to your own or to a library function that doesn't take a 'smart pointer' (unique_ptr and shared_ptr are considered smart pointers ).

In C++, you mostly want to deal with references, which is a special type of pointer. References cannot be reassigned like pointers can so once they are initialized, that it. This makes them safer than raw pointers. If you are wanting an array of things, use std::array or std::vector. If you want to get the size of an array using pointers you can subtract the last pointer to element in the array from the first pointer to element, but if you use a container like std::array or std::vector, you can just call the size() function.

Data structures:
Some data structures probably need to be setup using pointers, but something like a Heap ( not memory heap ) can be implemented using some math and a vector or an array. For others, check the STL ( standard template library ) for pre-made data structures, std::list is a linked list if you really need a list.

If you are working with someone else's code, you are probably going to need to use pointers. If you can, try to encapsulate the creation and destruction in a class, so you don't have to worry about trying to remember every place you allocated one. For instance, the DirectX API uses pointers a lot to interact with the library. There is a class called ComPtr which handles the destruction of those pointers when their reference count <= 0.

TL;DR:
prefer std::vector over lists if possible
prefer smart pointers over raw pointers to show ownership and handle memory management
prefer references or const references over raw pointers
only use raw pointers when a function needs it
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Empirean
Posts: 26
Joined: September 24th, 2017, 3:23 am

Re: texts

Post by Empirean » October 20th, 2017, 11:22 am

I appreciate the time you spent explaining things albinopapa. Thank you so much for a very comprehensive response.

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: texts

Post by albinopapa » October 20th, 2017, 9:10 pm

You're welcome, hope it is useful. I know a lot of it may be ahead of where you are in learning C++.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Post Reply