different pointer changes in same code

This is a discussion on different pointer changes in same code within the C++ Programming forums, part of the General Programming Boards category; Hi, My problem is like the below code. In "pushes" function "ptr++" is increasing 16 bytes and in "pops" "ptr--" ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    35

    different pointer changes in same code

    Hi,

    My problem is like the below code. In "pushes" function "ptr++" is increasing 16 bytes

    and in "pops" "ptr--" code decreasing only 1 byte as normal. I didnt understand the 16.

    Why 16 and 1?

    (BC++ 5.5)

    Code:
    #include<iostream.h>
    #include<stdlib.h>
    #include<string.h>
    #include <stdio.h>
    
    class stack{
    public:
    	stack();
    	~stack();
    	void pushes(char kr);
    	void pops();
    private:
    	char* ptr;
    	int tos;
    };
    
    stack::stack()
    {
    
    	tos = 0;
    	ptr = (char*) malloc(sizeof(char));
    	if (!ptr)
    	{
    		cout << "alloc err\n";
    		exit(1);
    	}
    }
    
    stack::~stack()
    {
    	free(ptr);
    }
    
    void stack::pushes(char kr)
    {
    	ptr = (char*) malloc(sizeof(char));
    	if (!ptr)
    	{
    		cout << "alloc err\n";
    		exit(1);
    	}
    	*ptr = kr;		
    	
    	tos++;
    	ptr++;
    //	printf("%d\n",ptr);
    }
    
    void stack::pops()
    {
    	if (tos == 0)
    	{
    		cout << "stack empty\n";
    		return;
    	}
    	tos--;
    //	printf(" - %d\n",ptr);	
    	cout << "pushed [" << tos << "] :" << *ptr << endl;
    	ptr-= 16;
    }
    
    int main()
    {
    	int i;
    
    	stack pushpop;
    
    	pushpop.pushes('A');
    	pushpop.pushes('B');
    	pushpop.pushes('C');
    
    	for (i = 0; i < 3; i++)
    	{
    		pushpop.pops();
    	}
    	return 0;
    }
    Want to learn? Then try to teach...

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I'm not sure what your question is, but I see a lot of problems with your pointer arithmetic...You are going outside the bounds of the pointer in pushes() and pops() as far as I can tell.

    This also looks like C code to me...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Well, first you allocate memory for 'ptr' in your constructor and then your very first push overwrites that with new memory thus losing the other memory you allocated forever. Also, I don't know why you're incrementing the pointer and trying to reallocate. The memory given by new is not guaranteed to be contiguous.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    35
    Thank you for you things.

    1. Yes seems C code because i am very new in C++ but liked much

    2.
    Well, first you allocate memory for 'ptr' in your constructor and then your very first push overwrites that
    with new memory thus losing the other memory you allocated forever.
    Thats right too. I know it. But not important now. I am using in pushes() in fact. Thats like check.

    3.
    Also, I don't know why you're
    incrementing the pointer and trying to reallocate. The memory given by new is not guaranteed to be
    contiguous.
    hmm this is the most important point. Then I suppose the 16 is only a compiler choice for next allocation. But it seems very regular since only 16 every time. Then this is not a good algorithm.

    But i was really confused about 16 byte char increasing situation. I couldnt guessed anything.

    Ok understood(i suppose). Then,

    How should i be allocate memory as dynamically and put it in an char array. I dont want to use static variables.

    I suppose didnt tried before. Any advice?
    Want to learn? Then try to teach...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Windows Programming
    Replies: 0
    Last Post: 10-14-2002, 01:29 PM
  5. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 11:51 AM

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