In need of refeshing skills

This is a discussion on In need of refeshing skills within the C++ Programming forums, part of the General Programming Boards category; Unfortunately, the last time I used pointers was about 2 years ago in my intro to C++ class. I am ...

  1. #1
    Registered User carrja99's Avatar
    Join Date
    Oct 2002
    Posts
    56

    In need of refeshing skills

    Unfortunately, the last time I used pointers was about 2 years ago in my intro to C++ class. I am now finding out in my new class, we are going to be using pointers in more and more frequency. Could someone give me a good website or perhaps some code examples and/or simple assignments to learning/practicing pointer usage?

    (Right now, I'll be frantically looking through my old C++ book to look at some pointer examples!)

    EDIT: Ok, I was wrong. Re-learning pointers was not that hard at all. Now I just need to work on efficiently simulating a stack with pointers. I have discovered that a statement such as "p1 = new int;" also stores the new variable at a new memory location located 10 bytes (I think!?) after the previous memory location. Is the previous location still holding the data? And if so, how do I access it? I tried just "delete p1;" but the new value of p1 was just random numbers at the same memory location... any ideas?
    Last edited by carrja99; 01-22-2003 at 09:21 PM.

  2. #2
    Registered User carrja99's Avatar
    Join Date
    Oct 2002
    Posts
    56
    OK... I may talking to myself here as I see no response, but I am very active and am coming across some odd problems. I came up with the notion of reducing the address as follows:

    Code:
    #include <iostream.h>
    
    typedef int* IntPtr;
    
    void sample_function(IntPtr& pointer);
    
    main()
    	{
    	IntPtr p1, p2;
    	
    	sample_function(p1);
    
    	cout << "The current value of *p1 is " << *p1 << " and it is at address " << p1 << endl;
    	p1 = p1 - 1;
    	cout << "*p1 is now equal to " << *p1 << " and is at memory loc " << p1 <<endl;
    	
    	
    
    	return 0;
    	}
    void sample_function(IntPtr& pointer)
    	{
    	for (int i = 0; i < 25; i++)
    		{
    		pointer = new int;
    		*pointer += i;
    		cout << *pointer << " is now stored in address " 			<< pointer <<endl;
    		}
    	}
    It produced very odd results. The following is the output I obtained from it (for space, I am starting at 10);

    Code:
    10 is now stored in address 0x8049d98
    11 is now stored in address 0x8049da8
    12 is now stored in address 0x8049db8
    13 is now stored in address 0x8049dc8
    14 is now stored in address 0x8049dd8
    15 is now stored in address 0x8049de8
    16 is now stored in address 0x8049df8
    17 is now stored in address 0x8049e08
    18 is now stored in address 0x8049e18
    19 is now stored in address 0x8049e28
    20 is now stored in address 0x8049e38
    21 is now stored in address 0x8049e48
    22 is now stored in address 0x8049e58
    23 is now stored in address 0x8049e68
    24 is now stored in address 0x8049e78
    The current value of *p1 is 24 and it is at address 0x8049e78
    *p1 is now equal to 17 and is at memory loc 0x8049e74
    Why is the integer 17 being stored at memory location 0x8049e74 when it has been indicated that 17 is located at memory location 0x8049e08?
    I am Error. When all else fails, use fire.

    My Current Screenshot

  3. #3
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Code:
    void sample_function(IntPtr& pointer)
    	{
    	for (int i = 0; i < 25; i++)
    		{
    		pointer = new int;
    		*pointer += i;
    		cout << *pointer << " is now stored in address " 			<< pointer <<endl;
    		}
    	}
    Well first off, this code creates a huge memory leak. Each call to new must be followed up with a call to delete. In your case, you lose the address of the newly created memory, as you reassign it to new memory each iteration. You have no chance of ever releasing that memory.

    I'm suprised you got the results you did.. there's no telling what value *pointer holds after you do an assignment such as pointer = new int;

    Code:
    p1 = p1 - 1;
    What are you trying to do here? What's probably happening in memory is:
    [byte][byte]p1->[byte][byte][byte][byte][byte]
    [byte]p1->[byte][byte][byte][byte][byte][byte]
    There's no telling what Mr. Red byte holds, you don't own him. He's probably part of the memory which you've leaked throughout the program. Mr. Blue byte is sent on his merry way, along with any value he holds.
    Last edited by Eibro; 01-22-2003 at 09:47 PM.

  4. #4
    Registered User carrja99's Avatar
    Join Date
    Oct 2002
    Posts
    56
    Hmph... I was assuming, since each new int had a new memory address, that I each previous memory address was retaining the same value it previously had.

    Basicallu, I assumed that 0x8049e38 still contains 20 after p1 points to 0x8049e78 and contains 24. Is this still there, or is it pretty much "not reachable"??

    Basically, I'm trying to figure a way of perhaps creating stacks or linked lists using pointers only and not arrays.

    Right now I am just looking for something elementary to build on. My book, unfortunately, has the idea that it can print a 3 page program introducing a new concept when I perfer just testing a core example to learn on my own.

    For example, while the following is nice...
    Code:
    #include <iostream.h>
    
    struct node
    	{
    	int key;
    	struct node *next;
    	};
    
    main()
    	{
    	int i, N, M;
    	
    	struct node *t, *x;
    	cin >> N >> M;
    	t = new node;
    	t -> key = 1;
    	x = t;
    	
    	for (int i = 2; i <= N; i++)
    		{
    		t -> next = new node;
    		t = t -> next;
    		t -> key = i;
    		}
    	t -> next = x;
    	while (t != t -> next)
    		{
    		for (int i = 1; i < M; i++) t = t -> next;
    		cout << t->next->key << ' ';
    		x = t ->next;
    		t -> next = x -> next;
    		delete x;
    		}
    	cout << t ->key << endl;
    	
    	return 0;
    	}
    I really have no idea how it works!! wtf is -> ?? It says it is a way of following through a pointer, but that's a bit too vague for me.
    I am Error. When all else fails, use fire.

    My Current Screenshot

  5. #5
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    '->' is simply shorthand for dereferencing the pointer, and using a '.' to access members.

    For example, these two statements are equivalent:

    t->key
    (*t).key
    Last edited by Eibro; 01-22-2003 at 10:19 PM.

  6. #6
    Registered User carrja99's Avatar
    Join Date
    Oct 2002
    Posts
    56
    ah.. nice

    anyway, the following modification to sample_function:
    Code:
    void sample_function(IntPtr& pointer)
    	{
    	pointer = new int[5];
    	for (int i = 0; i < 5; i++)
    		{
    		
    		*pointer += i;
    		cout << *pointer << " is now stored in address " 			<< pointer <<endl;
    		pointer++;
    		}
    	getchar();
    	for (int i = 0; i < 5; i++)
    		{
    		
    		cout << *pointer << " is now stored in address " 			<< pointer <<endl;
    		pointer--;
    		}
    	}
    produced the following output:
    Code:
    0 is now stored in address 0x8049d58
    1 is now stored in address 0x8049d5c
    2 is now stored in address 0x8049d60
    3 is now stored in address 0x8049d64
    4 is now stored in address 0x8049d68
    
    17 is now stored in address 0x8049d6c
    4 is now stored in address 0x8049d68
    3 is now stored in address 0x8049d64
    2 is now stored in address 0x8049d60
    1 is now stored in address 0x8049d5c
    seems like I am on the right track, except for that darn 17! Should probably be a simple matter of decrementing the pointer once beforehand, but doing such things has always felt a little... inefficient...

    EDIT: heh. ignore that. Just a simple if statement solved it...
    Last edited by carrja99; 01-22-2003 at 10:12 PM.
    I am Error. When all else fails, use fire.

    My Current Screenshot

  7. #7
    Registered User carrja99's Avatar
    Join Date
    Oct 2002
    Posts
    56
    Okay... I have finally created a simple simulation of a linked list that feels as though it truely is a linked list.

    I have also included a search method that steps through the nodes looking for the variables created in fill_list. Anyone looking, please tell me if this could be simplified further or made more abstract/efficient.

    Code:
    #include <iostream.h>
    
    int list_size = 0; //initializing a default value for saftey
    typedef int* IntPtr;
    
    void fill_list(IntPtr& pointer);
    void find_num(IntPtr& pointer,int v1);
    main()
    	{
    	IntPtr p1, p2;
    	int var1;
    	
    	cout << "Enter the size of the list:";
    	cin >> list_size;
    	fill_list(p1);
    	do
    		{
    		cout << "Now, enter a number to search the list for"
    			<< "(enter 0 to exit):";
    		cin >> var1;
    		find_num(p1, var1);
    		}while (var1 != 0);
            
    	
    
    	return 0;
    	}
    void fill_list(IntPtr& pointer)
    	{
    	pointer = new int[list_size];
    	for (int i = 0; i < list_size; i++)
    		{
    		
    		*pointer += i*i+1;
    		cout << *pointer << " is now stored in address " 			<< pointer <<endl;
    		if ( i < list_size -1)
    			pointer++;
    		}
    	}
    void find_num(IntPtr& pointer, int v1)
    	{
    	bool found = false;
    	for (int i = 0; i < list_size; i++)
    		{
    		if (*pointer == v1)
    			found = true;
    		pointer--;
    		}
    	for (int i = 0; i < list_size; i++) pointer++; 
    	if (found == true)
    		cout << v1 << " was found at memory location "<<pointer<<endl;
    	else 
    		cout << v1 << " was not found!"<<endl;
    	}
    I am Error. When all else fails, use fire.

    My Current Screenshot

  8. #8
    Registered User carrja99's Avatar
    Join Date
    Oct 2002
    Posts
    56
    Well, while mesing around with different operators and such, I finally figured out how to create a crude linked list and came up with the following code:

    Code:
    #include <iostream.h>
    
    struct node
    	{ int key; struct node *next;};
    
    
    main()
    	{
    	int v;
    	struct node *t, *x;
    	
    	cout << "Press enter to fill a list with 5 random items:";
    	getchar();
    
    	t = new node; 
    	t->key = v;
    	x = t;
    	for(int i = 0; i < 5; i++) //fills the list up with 5 more items
    		{
    		t->next = new node;
    		t = t->next; t->key = i*v;
    		}
    
    	
    
    	
    	t->next = x->next;
    	for (int i = 0; i <25; i++)     //prints contents of list, 25 times 
    		{			//to illustrate ciricular(why?) 
    		cout << t->key <<endl;	//property
    		t = t->next;
    		}
    	
    	return 0;
    	}
    As I noted in the 2nd loop, I now have a linked list with a circular property. Why? Also, for some odd reason the list will seem to start at the last element, rather than the 1st, when it seems that x->next should be pointing to the first link? Could anyone give me a few pointers(no pun intended) on what the logical problem with my code is?
    I am Error. When all else fails, use fire.

    My Current Screenshot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MURK - a small preview
    By Mario F. in forum Game Programming
    Replies: 27
    Last Post: 12-18-2006, 08:22 AM
  2. Communication skills
    By itsme86 in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 09-14-2006, 02:19 PM
  3. Job skills
    By IfYouSaySo in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 05-25-2006, 02:53 AM
  4. JOB: C/C++ Developer with problem solving skills
    By VoltRecruiter in forum Projects and Job Recruitment
    Replies: 1
    Last Post: 01-26-2006, 12:25 AM
  5. Skills of device driver writer for GPU vs Game Developer
    By Silvercord in forum Game Programming
    Replies: 9
    Last Post: 02-15-2003, 06:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21