Thread: runtime error

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    5

    Exclamation runtime error

    Hi everyone, working on a data structers project on dev-c++ as compiler and problem is when compile&run it freezes but when click debug, (magically) it works. So what do you think I missing

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Your code is probably doing an invalid operation with a pointer. Anything that reorganises how your program uses memory can affect what happens as a result of an invalid pointer operation (including symptoms seeming to disappear when debugging, but coming back otherwise).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    5
    my struct is like that:
    Code:
    struct charDesc
    {
          char ch;
          int freq;
          int index;
    };
    struct charDesc chars[256];
    
    
    struct HeapStruct
    {
          int capacity;
          int size;
          struct charDesc *elements;
    };
    typedef struct HeapStruct *priorityQueue;
    So can't see the Error in this function.
    Code:
    void InsertPriorityQueue(int index,int freq,char ch, PriorityQueue h) {
        int i;
    
    
        if (IsFullPriorityQueue(h)) 
        {
            printf("Priority queue is full!\n");
            return;
        }
        h->size++;
        for (i = h->size; h->elements[i/2].freq > freq; i /= 2)
            {
                 h->elements[i].ch = h->elements[i/2].ch;
                 h->elements[i].freq = h->elements[i/2].freq;
                 h->elements[i].index = h->elements[i/2].index;
            }
        h->elements[i].index = index;
        h->elements[i].freq = freq;
        h->elements[i].ch = ch;
    }
    Last edited by zebani; 12-28-2011 at 11:57 AM.

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    5
    any opinion?

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You are starting your for loop at i = h->size, which may be out of range (depending on the rest of your code). You may want i = h->size - 1 instead.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Don't know about your runtime error as there isn't enough info to find that, but I'd certainly change this:
    Quote Originally Posted by zebani View Post
    Code:
                 h->elements[i].ch = h->elements[i/2].ch;
                 h->elements[i].freq = h->elements[i/2].freq;
                 h->elements[i].index = h->elements[i/2].index;
    to just this:
    Code:
                 h->elements[i] = h->elements[i/2];
    Entire structures can be copied with a single assignment statement.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Dec 2011
    Posts
    5
    Thank you for all guys. The thing is code working now without changes. I guess dynamically allocated memory caused this crash so when debugging there was no error. And thanks for hint iMalc, I wasn't sure this way is OK

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Runtime error
    By MarlonDean in forum C++ Programming
    Replies: 2
    Last Post: 07-02-2008, 02:28 AM
  2. compile time error or runtime error?
    By George2 in forum C# Programming
    Replies: 3
    Last Post: 05-07-2008, 07:08 AM
  3. runtime error
    By chatesmick in forum C Programming
    Replies: 4
    Last Post: 11-22-2005, 08:48 AM
  4. runtime error
    By poorman in forum C Programming
    Replies: 1
    Last Post: 06-23-2002, 03:19 AM
  5. Runtime error! Please help
    By Garfield in forum C Programming
    Replies: 7
    Last Post: 09-22-2001, 05:40 AM