SemiAccurate Forums  

 
Go Back   SemiAccurate Forums > Main Category > Off topic

Off topic Posts not related to technology

Reply
 
Thread Tools Display Modes
  #1  
Old 03-27-2012, 03:55 PM
Spartak's Avatar
Spartak Spartak is offline
8-bit overflow
 
Join Date: Mar 2011
Posts: 442
Spartak is on a distinguished road
Default need a bit help in C++

In the second semester we moved to C++, so this the spiritual successor to
previous thread, considering I got 11/15 points without any knowledge about programming before my studies, was a success.

I have one simple question and a more demanding one

The first is what does the arrow operator "->" do?

The second one is about dynamic memory allocation. Could you explain in the simplest possible manner how "new" works?
__________________
IMHO
Core i7-4770k with Thermalright True Spirit 140, ASRock Z87 Extreme 4 Mobo, 2x4 GB TeamGroup DDR3-2133, 2xSapphire Radeon HD 6950 2GB CF, 2xSamsung 840 Pro 256 GB in RAID 0, WD Caviar Black 1TB SATA 3 HDD, Super Flower 800W 80+ gold PSU, LG BH10LS30 BD-RE, Ultron Cardreader, Windows 7 Ultimate x64, LG W2261 FullHD monitor
Reply With Quote
  #2  
Old 03-28-2012, 04:17 AM
rambaldi's Avatar
rambaldi rambaldi is offline
2^11
 
Join Date: Jan 2010
Location: Auckland, New Zealand
Posts: 2,435
rambaldi will become famous soon enoughrambaldi will become famous soon enough
Default

new allocates a block of memory on the heap.

-> is used to reference a component within a class (I think, haven't touched the plus plus in a while)
__________________
E-Peen: AMD Phenom II x4 965 (stock) | MSI 790FX-GD70 | ATI HD 5970 (stock) | Kingston HyperX DDR3 1333 CL7 2x2GB | Seagate 7200.12 4x1TB | Ikonic RaX10 Liquid
Reply With Quote
  #3  
Old 03-28-2012, 07:47 AM
alvter alvter is offline
2^11
 
Join Date: Jun 2009
Posts: 2,071
alvter will become famous soon enoughalvter will become famous soon enough
Default

Quote:
Originally Posted by Spartak View Post
In the second semester we moved to C++, so this the spiritual successor to
previous thread, considering I got 11/15 points without any knowledge about programming before my studies, was a success.

I have one simple question and a more demanding one

The first is what does the arrow operator "->" do?

The second one is about dynamic memory allocation. Could you explain in the simplest possible manner how "new" works?
It's been a long while...
"->" is a pointer... You use it to simplify calls to objects or items in objects...

I'm very much out of practice with this... and Java has made me lazy when using pointers. I can't help you out much unless i start punching out code again.
__________________
-V
My views and opinions do not represent those of my employer
Reply With Quote
  #4  
Old 03-29-2012, 04:26 AM
Lightning's Avatar
Lightning Lightning is offline
640k who needs more?
 
Join Date: Oct 2009
Location: Canberra, Australia
Posts: 774
Lightning is on a distinguished road
Default

"->" is a pointer operator. If we didn't have that, we'd have to dereference with * every time we wanted to do something to what the pointer points to. That's inefficient, so instead C++ lets you carry out an operation at the address the pointer points to with "->".

Hope that helps. New allocates memory on the heap but it's clever in the sense that it does it based on the calling object and I think it can also be overridden so that you can have specific New calls from objects. It's safer than malloc from C.
__________________
Long live aceshardware!
Reply With Quote
  #5  
Old 03-31-2012, 07:01 AM
laurent laurent is offline
2^10
 
Join Date: Feb 2011
Location: France
Posts: 1,211
laurent will become famous soon enough
Default

Quote:
Originally Posted by Spartak View Post
The first is what does the arrow operator "->" do?
a->b is the same as (*a).b. It's an operator and hence can be overloaded.

Quote:
The second one is about dynamic memory allocation. Could you explain in the simplest possible manner how "new" works?
new does memory allocation (with clearing) and calls the constructor that matches the parameters of the new call.
__________________
Speaking for myself.
Reply With Quote
  #6  
Old 04-02-2012, 05:03 PM
Shadow Concept Shadow Concept is offline
8-bit overflow
 
Join Date: Jul 2009
Location: 'Strail-ya
Posts: 390
Shadow Concept is on a distinguished road
Default

Due to the fact that I'm surfing this at work, I don't have enough time to find one myself, but if someone could link him a C++ LinkedList tutorial, I've found it's the best way to introduce people to the logic of pointers. Then he can make his own progression onto circular doubly linked lists etc...
__________________
CPU: Intel i7 3930K + Swiftech Apogee XT, GFX: HIS 6970@950mhz + EK Waterblock, Mobo: ASUS RAMPAGE IV EXTREME RAM: 8x8gb G.Skill Ripjaws Z 2133mhz, Sound: SB ZxR + Audio Technica ATH-AD700, Case: Corsair Obsidian 900D, PSU: Enermax Revolutions85+ 1250w, Main Drive: 2x256gb OCZ Vertex 4 RAID 0, Storage: 2x4tb + 2x3tb RAID 1, OS: Windows 8.1/Ubuntu 13.10/Gentoo triple boot
Reply With Quote
  #7  
Old 04-03-2012, 08:17 PM
Bitey Bitey is offline
itanic
 
Join Date: Apr 2011
Posts: 201
Bitey is on a distinguished road
Default

http://www.cplusplus.com/reference/s...r%20new%5B%5D/

a good c++ reference site
Reply With Quote
  #8  
Old 04-23-2012, 09:02 PM
Spartak's Avatar
Spartak Spartak is offline
8-bit overflow
 
Join Date: Mar 2011
Posts: 442
Spartak is on a distinguished road
Default

Ok Now I can offer you something that will bring your brain to full load.
It's the last (optional) assignment from the first lab in this semester.


given is a chessboard with some cells filled. It is stored as a private matrix of the class Chessboard. the main task is to write a member function that counts all the empty squares. the counter is private variable sqr_count
It has to work for any size of chessboard (set by #define SQR_SIZE) and arbitrary cells filled.
This is as best as I could do. I've almost broken the F10 button, but couldn't make it to produce correct result.
Code:
void Chessboard::check_all_squares(void)
{
	for(int i=0; i<SQR_SIZE; i++)//moving  horizontally
		for(int j=0; j<SQR_SIZE;j++)//moving vertically
		{
			char check=0;
			for(int d=0;(d<SQR_SIZE-i-1)&&(d<SQR_SIZE-j-1); d++)//distance of the cells for checking
			{
				for(int r=0;r<=d; r++)
				{
					if ((field[d+i][r+j]==1)||(field[r+i][d+j]==1))
					{
						check=1;
						break;

					}
				}


				if(check==1)
					break;
				else
					quadr_count++;

			}


		}
}
__________________
IMHO
Core i7-4770k with Thermalright True Spirit 140, ASRock Z87 Extreme 4 Mobo, 2x4 GB TeamGroup DDR3-2133, 2xSapphire Radeon HD 6950 2GB CF, 2xSamsung 840 Pro 256 GB in RAID 0, WD Caviar Black 1TB SATA 3 HDD, Super Flower 800W 80+ gold PSU, LG BH10LS30 BD-RE, Ultron Cardreader, Windows 7 Ultimate x64, LG W2261 FullHD monitor
Reply With Quote
  #9  
Old 04-24-2012, 01:57 AM
laurent laurent is offline
2^10
 
Join Date: Feb 2011
Location: France
Posts: 1,211
laurent will become famous soon enough
Default

If you only have to count pieces what are the inner two loops "distance" doing? You only need two loops over x and y to count pieces.
__________________
Speaking for myself.
Reply With Quote
  #10  
Old 04-24-2012, 08:19 AM
Rottis's Avatar
Rottis Rottis is offline
8-bit overflow
 
Join Date: Jul 2009
Posts: 491
Rottis is on a distinguished road
Default

If that zero means empty square.

Code:
#define SQR_SIZE 123

class Chessboard
{
public:
    Chessboard(){}
	
    void check_all_squares();

private
    int sqr_count;
    int field[SQR_SIZE][SQR_SIZE];
};

void Chessboard::check_all_squares()
{
    sqr_count = 0;
    for (int y = 0; y < SQR_SIZE; ++y)
    {
        for (int x = 0; x < SQR_SIZE; ++x)
        {
            if (field[y][x] == 0)
            {
                ++sqr_count;
            }
        }
    }
}
Maybe I didn't get the question, but the braintwister was the code you pasted.
Had to answer...
__________________
Mmm, bacon balls.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Forum Jump


All times are GMT -5. The time now is 05:51 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SemiAccurate is a division of Stone Arch Networking Services, Inc. Copyright 2009 Stone Arch Networking Services, Inc, all rights reserved.