-2

I'm trying to compile c++ on visual studio for the first time, and I'm countering an error.

/***Setup****/
struct id_priority{
    int id;
    int priority;
};
struct temp_heap{
    int id;
    int priority;
};

/**heapify up**/
void heapify(id_priority heap[], int index, int length, temp_heap temp){
}

int main(){

    int *command_processed;
    command_processed = new int[6];
    id_priority *heap;
    heap = new id_priority[1000];

    temp_heap temp;
    int index = 0;
    int length = 0;
    heapify(heap, index, length, temp);
    return (0);
}

heapify(heap, index, length, temp); this line it says uninitialized local variable "temp" used, but this code works fine on terminal on Linux. I dont know whats wrong here.

DigitalMan
  • 41
  • 1
  • 7
  • 5
    `temp` is uninitialized, so it says it is uninitialized. – MikeCAT Nov 27 '15 at 15:56
  • 1
    Not enabling compiler warnings on terminal on Linux should be wrong here. – MikeCAT Nov 27 '15 at 15:56
  • Just to be clear, an uninitialized variable has an indeterminate value and using [and indeterminate value is undefined behavior](http://stackoverflow.com/a/23415662/1708801). In your case you are passing `temp` by value which means this will produce an indeterminate value and this invoke undefined behavior. – Shafik Yaghmour Nov 27 '15 at 15:57
  • @MikeCAT that is not clear, I don't think clang or gcc will warn about this case, or at least not with the code given – Shafik Yaghmour Nov 27 '15 at 16:05
  • so what should i do to fix this segment of code ? how can i initialize a struct? – DigitalMan Nov 27 '15 at 17:06

1 Answers1

1

We don't know what temp should be initialized to. But here are two ways to initialize it. I'll assume you wanted all 0's for its fields.

    temp_heap temp = {0, 0};

and

    temp_heap temp;
    temp.id = 0; temp.priority=0;
Topological Sort
  • 2,733
  • 2
  • 27
  • 54