Thread: Rough cut stack manipulation

  1. #1
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681

    Rough cut stack manipulation

    Ok just created this and have only done some real limited testing on it.
    This was actually one time I wish'd for the C++ classes.

    Would like to hear if anyone sees anything alarmily wrong with it. I haven't taken the time to try to optimise it yet but I will tomorrow

    Thanks

    Warning: 75 line program
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    /* type define the stack */
    typedef struct {
      void *ptr;
      void *lower;
      void *upper;
    }Stack;
    
    int stack_init(Stack *, size_t);
    int push(Stack *, void *, size_t);
    int pop(Stack *, void *, size_t);
    int stack_destroy(Stack *);
    
    int main(void){
      Stack stack;
      int x;
      if ( !stack_init(&stack, 100) )  {
        perror("Problem with initalizing the stack");
        return 1;
      }
    
      x = 5;
      printf("1) %d\n", x);
      push (&stack, &x, sizeof x);
      x = 100;
      printf("2) %d\n", x);
      pop (&stack, &x, sizeof x);
      printf("3) %d\n", x);
    
      if ( !stack_destroy(&stack) )  {
        perror("problem with destroying the stack");
      }
      return 0;
    }
    
    int stack_init(Stack *stack, size_t size){
      stack->lower = calloc (1, size);
      if ( stack->lower == NULL )
        return 0;
      stack->ptr = stack->upper = stack->ptr + size;
      return 1;
    }
    
    int stack_destroy(Stack *stack){
      if ( stack->lower == NULL)
        return 0;
    
      free(stack->lower);
      stack->lower = stack->ptr = stack->upper = NULL;
      return 1;
    }
    
    int push (Stack *stack, void *var, size_t size){
      int count;
    
      if ( !((stack->ptr - size) > stack->lower) )
        return 0;
      stack->ptr -= size;
      for ( count=0; count<size; count++)
        *(char *)((stack->ptr)+count) = *(char *)(var+count);
      return 1;
    }
    
    int pop ( Stack *stack, void *var, size_t size)
    {
      int count;
      if ( ! ( (stack->ptr+size) <= stack->upper) )
        return 0;
      for ( count=0; count<size; count++)
        *(char *)(var+count) = *(char *)((stack->ptr)+count);
      stack->ptr += size;
      return 1;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You're doing arithmetic on void pointer types
    > stack->ptr - size
    This is not permitted in ANSI-C, since void has no size

    > for ( count=0; count<size; count++)
    For the sake of clarity, why not just use memcpy(), at least until the thing is debugged and you've had a chance to profile it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Is there a type that will be a size of 1 regardless of of the system? I know char is most of the time, but Stroustrup stated in one of his books that its not guarnteed on all systems. For some reason I don't thats just a C++ thing.

    If not I might have to go with a non ANSI solution.

    Thanks for the memcpy suggestion.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Is there a type that will be a size of 1 regardless of of the system
    That would be char
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I meant 1 byte. And as I mentioned:
    The char type is supposed to be chosen by the implementation to be the most suitable type for holding and manipulating characters on a given computer; it is typically an 8-bit byte. Similarly, the int type is supposed to be chosen to be the most suitable for holding and manipulating integers on a given computer; it is typically a 4-byte (32 bit) word. It is unwise to assume more. For example, there are machines with 32 bit chars.
    From: "The C++ Programming Language Third Edition"

    From his wording I have to assume that is language independant.

  6. #6
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    for one byte, you might want to mess with bitfields and use 8 bits, but of course 8 bits is not guaranteed to by 1 byte on all systems, and the struct my encapsulate a little more memory. Might be an option though.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > For example, there are machines with 32 bit chars.
    Yes, and on those machines, sizeof(char) == 1 as well
    CHAR_BIT only has to be at LEAST 8, it doesn't have to be exactly 8
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Ok I see what you are saying. So on those systems each byte of ram will be equivlent to the size of the character.

    Thanks for the clear up.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  2. infix evaluation using stack
    By lewissi in forum C++ Programming
    Replies: 0
    Last Post: 11-03-2005, 02:56 AM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM