0

I'm having a little problem here. Every time I run the code below, my program just crash.

void Wingcod::push(byte b)
{
    stack[stackp] = b;
    stackp++;
    if(stackp >= stacks)
    {
        stacks += 16;
        try
        {
            realloc(stack,stacks);
        }catch(bad_alloc*)
        {
            cerr << "STACK OVERFLOW";
            exit(1);
        }
    }
}

And stack, stackp and stacks is defined like this:

stacks = 8;
stackp = 0;
stack = new byte[stacks];

And byte is just a unsigned char.

8
  • what type are you declaring stackp? Commented Jan 31, 2012 at 20:37
  • did you try to debug with breakpoints and see what happens? Commented Jan 31, 2012 at 20:37
  • 4
    Why not use C++ instead? You can have a handy std::vector<byte> that does exactly what you need. Commented Jan 31, 2012 at 20:39
  • When i step into the push function, all the variable fields in the debugger turn blank. Weird. Commented Jan 31, 2012 at 20:47
  • @Kerrek SB this might sound a bit weird, but i need something more low level than a vector. Commented Jan 31, 2012 at 20:48

4 Answers 4

6

You're not allowed to use realloc() on a pointer that was allocated with new.

Maybe try something like the following instead of realloc():

        byte* tmp = new byte[stacks];
        delete [] stack;
        stack = tmp;
Sign up to request clarification or add additional context in comments.

Comments

4

realloc doesn't throw exceptions as it's not related to new, but C's malloc. It also doesn't set the pointer.

stacks = 8;
stackp = 0;
stack = static_cast<byte*>(malloc(stacks * sizeof(byte)));

void Wingcod::push(byte b)
{
    stack[stackp] = b;
    stackp++;
    if(stackp >= stacks)
    {
        stacks += 16;
        if(!(stack = static_cast<byte*>(realloc(stack,stacks * sizeof(byte))))) {
            cerr << "STACK OVERFLOW";
            exit(1);
        }
    }
}

6 Comments

"Doesn't change the pointer" seems to be an important part of the crash.
Yeah, it's a solution, but not to my current problem. you see the crash happens at the first line in the function, the first time i run it.
@TheBreadCat You can post another question with more code if you think the problem is something else. I was thinking that the calling object (this pointer) might be invalid.
the wc->push() code is the same as this.push(), it's just a static reference.
@TheBreadCat Why is wc a pointer? Are you 100% sure it's not NULL?
|
4

You cannot use a pointer allocated with new T[n] as an argument for realloc(). The pointers working with realloc() have to come from the malloc() family of functions (malloc(), calloc(), or realloc()). Personally, I wouldn't use any of this but rather use std::vector<T>.

Comments

0

From here, Realloc only works on a pointer to memory previously assigned by alloc. Try using malloc

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.