Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Help! C program behaves oddly  (Read 1412 times)

aeonmyst

  • Bay Watcher
    • View Profile
Help! C program behaves oddly
« on: February 26, 2010, 07:53:44 am »

I was happily coding my exercise for a subject but then i got this weird thing where a variable is modified by a function but i did not pass the variable...

it's in the function heapsort, in the for loop with the delete(&localheap)....

here's the code
Code: [Select]
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ARRAY_SIZE 20

typedef struct hp{
int array[ARRAY_SIZE];
int count;
}HEAP;

void print_heap_array(HEAP *heap){ //print heap array contents
int c = 0;
printf("array: ");
if(heap->count == 0)
printf("empty");
else{
while(c < heap->count){
printf("%d ",heap->array[c]);
c++;
}
}
printf("\n");
}

void print_heap_array_graphical(HEAP *heap){ //fail
int x=0,c=1,d=1,i;
while(x<heap->count){
printf("%3d ",heap->array[x]);
x++;
c--;
if(c<=0){
printf("\n");
d *= 2;
c = d;
}
}
printf("\n");
}

void swap(HEAP *heap,int x,int y){ //trivial
int temp;
temp = heap->array[x];
heap->array[x] = heap->array[y];
heap->array[y] = temp;
}

void insert(HEAP *heap,int x){ //insert x on the heap
int temp;
if(heap->count<ARRAY_SIZE){ //if not full
heap->array[heap->count++] = x; //add at end
for(temp = heap->count-1;temp > 0;temp = (temp-1)/2){ //fixup
if(heap->array[(temp-1)/2] < heap->array[temp])
swap(heap,(temp-1)/2,temp);
else
break;
}
}
else{
printf("heap array is full\n");
}
}

int child_max(HEAP *heap, int temp){ //returns the child with highest value of the parent on index temp
int x,y;
x = temp*2+1;
y = x+1;
if(y>heap->count-1) //if there is no right child
if(x>heap->count-1) //if there is no left child
return -1; //return sentinel
else
return x; //return left child
else
if(heap->array[x] > heap->array[y]) //compare left and right
return x;
else
return y;
}

void delete(HEAP *heap){ //delete
int temp = 0,x;
//
if(heap->count == 0){ //return if empty
return;
}
swap(heap,0,(heap->count--)-1); //swap root and last element
//
while(temp < heap->count-1 && temp != -1){ //fixup
//compare to children
x = child_max(heap,temp);
if(heap->array[x] > heap->array[temp]){ //if child>parent
swap(heap,x,temp); //swap
temp = x;
}
else{
break;
}
}
}

void heap_sort(HEAP *heap){
HEAP localheap;
int i;
//copy
for(i=0;i<heap->count;i++)
localheap.array[i] = heap->array[i];
localheap.count = heap->count;
//sort
//delete until root+1


for(i=localheap.count;i>0;i--){
printf("var i before delete: %d\n",i);
delete(&localheap);
printf("var i after delete: %d\n",i);
}
localheap.count = heap->count; //reset count
printf("Sorted heap ");
print_heap_array(&localheap);
//lmao
}

main(){
HEAP myheap;
int c,d;
myheap.count = 0;
while(c!=4){
printf("[1]Insert\n[2]Delete\n[3]Sort\n[4]Exit\n");
scanf("%d",&c);
switch(c){
case 0: //insert random
printf("Value: ");
scanf("%d",&d);
getchar();
for(;d>0;d--)
insert(&myheap,rand()%100);
break;
case 1:
printf("Value: ");
scanf("%d",&d);
insert(&myheap,d);
break;
case 2:
delete(&myheap);
break;
case 3:
heap_sort(&myheap);
break;
case 4:
break;
case 5:
d = myheap.count;
myheap.count = 20;
print_heap_array(&myheap);
myheap.count = d;
break;
default:
print_heap_array_graphical(&myheap);
break;
}
printf("Original heap ");
print_heap_array(&myheap);
}
}


i compiled it in mingw and cygwin, and gave the same result.... please help me

sorry for posting it all of a sudden, and this is my first post in this forum. it's just that i'm uncomfortable when i encounter something that i'm sure that it wasn't supposed to be there and there is no explanation  :P

edit: oh noes, i pressed enter then it post... gimme 5 minutes
ok done  ;D
« Last Edit: February 26, 2010, 08:06:42 am by aeonmyst »
Logged

Jookia

  • Bay Watcher
    • View Profile
Re: Help! C program behaves oddly
« Reply #1 on: February 26, 2010, 08:09:42 am »

Unless this is an assignment or something for a degree or somebody is holding a gun to your head forcing to you to code, this isn't really life advice.

That said, shouldn't you use a for() loop instead of while()?
Logged

aeonmyst

  • Bay Watcher
    • View Profile
Re: Help! C program behaves oddly
« Reply #2 on: February 26, 2010, 08:17:41 am »

Unless this is an assignment or something for a degree or somebody is holding a gun to your head forcing to you to code, this isn't really life advice.

That said, shouldn't you use a for() loop instead of while()?


no... not at the main().. it's in the heap_sort(), in the second for loop.

and kind of, since i passed this assignment some hours ago, and i was hoping i could get a perfect score. but then this thing showed up when i checked it after passing and  it wasn't supposed act like that, and im getting seizures because i don't know what's wrong with the code... :P ... ok just a little uneasy

i've also had a case in the same subject where my code works in ubuntu compiler but not in windows, and got a below average score for that work
Spoiler (click to show/hide)
[/size]

Spoiler (click to show/hide)
« Last Edit: February 26, 2010, 08:29:35 am by aeonmyst »
Logged

Jookia

  • Bay Watcher
    • View Profile
Re: Help! C program behaves oddly
« Reply #3 on: February 26, 2010, 05:32:08 pm »

You didn't have to use spoiler tags for that. Also, we really shouldn't be helping you on assignments as that's cheating.
Logged

winner

  • Bay Watcher
    • View Profile
Re: Help! C program behaves oddly
« Reply #4 on: February 26, 2010, 06:48:12 pm »

when I compile and run the program it works fine for me.
Logged
The great game of Warlocks!

eerr

  • Bay Watcher
    • View Profile
Re: Help! C program behaves oddly
« Reply #5 on: March 02, 2010, 01:21:12 am »

did you try turning off compiler opitimization?
Logged

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
Re: Help! C program behaves oddly
« Reply #6 on: March 02, 2010, 07:55:26 am »

Is the problem soomething like i changing from a function call that doesn't pass it on?
(By the consecutive printf()s, that looks like the odd behaviour)

If so, it would be because the HEAP is on the stack, consecutive to i, so an overflow when looping the HEAP would affect i. If so, it is NOT a good thing. Such behaviour is bad, since it relies on the compiler's output to be consistant, so if it works on one compiler and not others, that would be why.
Logged
Eh?
Eh!

eerr

  • Bay Watcher
    • View Profile
Re: Help! C program behaves oddly
« Reply #7 on: March 03, 2010, 08:35:22 pm »

A really good debugger could help here, The Microsoft Visual C++ IDE lets me look at each variable after every change.

Fantastic for not screwing up arrays, ect.
Logged

kcwong

  • Bay Watcher
    • View Profile
Re: Help! C program behaves oddly
« Reply #8 on: March 03, 2010, 09:20:24 pm »

Unless this is an assignment or something for a degree or somebody is holding a gun to your head forcing to you to code, this isn't really life advice.

OP's asking at the wrong place, although we do have a bunch of programmers around. There's usenet, and plenty of programming forums and sites for programming questions.

In general if memory is changed when you didn't intend to, this means you made a buffer overflow bug somewhere, writing data outside the memory allocated.
Logged

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
Re: Help! C program behaves oddly
« Reply #9 on: March 04, 2010, 12:24:05 am »

Try changing
Code: [Select]
HEAP localheap;
int i;
to
Code: [Select]
int i;
HEAP localheap;

If it fails when it used to work, then there is a large security flaw when it checks the buffer size.
Logged
Eh?
Eh!