Corrupted Heap error c - c

I am trying to create a tree from a vector of parents. However I get a "corrupted heap error" when I create the nodes with malloc. It works for the first two children , however crashes on the third(or terminates but does not connect the root with the child.)
Unhandled exception at 0x77E8A879 (ntdll.dll) in lab7.exe: 0xC0000374:
A heap has been corrupted (parameters: 0x77EC5910).
I have implemented this by, first extracting the root and creating the node and after that extracting the children of the root and creating them.
search root-> searches for the root and returns the value of it.
the function that searches for the children(search key )
void create->The function that creates the children. I send a vector that contains only the children of that specific parent and not other children.
*typedef struct node
{
int value;
node *left;
node *right;
node *middle;
}TreeNodeParent;
int search_root(int in[9])
{
for (int i = 0; i < 9; i++)
{
if (in[i] == -1)
{
int var = i+1;
in[i] = -2;
return var;
}// pe else nu facem nimic
}
return -2;
}
int search_key(int in[9], int radacina)
{
for (int i = 0; i < 9; i++)
{
if (in[i] == radacina)
{
int var = i + 1;
in[i] = -2;
return var;
}
}
return -3;
}
TreeNodeParent *createOneNode(int value)
{
//the error appears here
TreeNodeParent* create = (TreeNodeParent*)malloc(sizeof(TreeNodeParent));
create->value = value;
create->left = create->middle = create->right = NULL;
return create;
}
void create(int vector[], TreeNodeParent* radacina)
{
for (int i = 0; i < 9; i++)
{
if (vector[i] == -3)//am stabilit ca -3 ii oprirea
{
break;
}
else
{
TreeNodeParent* create = createOneNode(vector[i]);
if (radacina->left == NULL)
{
radacina->left = create;
}
else if (radacina->middle == NULL)
{
radacina->middle = create;
}
else
{
radacina->right = create;
}
}
}
}
int main()
{
int input[9] = { 2,7,5,2,7,7,-1,5,2 };
int root = search_root(input);
if (root == -2)
{
printf("Nu gasim radacina, arbore incorect!");
}
else { printf("root %d", root); }
//crearea nodului parinte
TreeNodeParent* rootParent = (TreeNodeParent*)malloc(sizeof(TreeNodeParent*));
rootParent->value = root;
rootParent->left = NULL;
rootParent->right = NULL;
rootParent->middle = NULL;
int vect2[9];
for (int i = 0; i < 9; i++)//worst case, tot arborele is copii ai lui root->o(n2)
{
vect2[i] = search_key(input, root);
printf("copii rootului %d", vect2[i]);
if ( vect2[i] == -3)
{
break;
}
}
create(vect2,rootParent);
_getch();
return 0;
}
checked online with gdb :
> tin_offsetof (struct malloc_chunk, fd_nextsize))+((2 *(sizeof(size_t))) - 1)) & ~((2 *(sizeof(size_t))) - 1))) && ((ol
> d_top)->size & 0x1) && ((unsigned long) old_end & pagemask) == 0)' failed.
program exited
> with code 134
I can't understand why the function crashes only on the third(and not before). Also it does not always appear. Sometimes it works fine, other times it stops with that error.
Also If there is a better way to create a tree with a parent representation?

I think the problem is in your main() method, where you malloc() with sizeof(TreeNodeParent*) instead of sizeof(TreeNodeParent) and assign it to rootParent:
//crearea nodului parinte
//TreeNodeParent* rootParent = (TreeNodeParent*)malloc(sizeof(TreeNodeParent*));
TreeNodeParent* rootParent = (TreeNodeParent*)malloc(sizeof(TreeNodeParent)); `

Related

Searching for A SUFFIX in a tree

So, I'm trying to do an impletation of Sharing Suffixes WITHOUT USING UKKONNENS ALGORITHM (don't understand ukkonnens). So basicaly what I'd do is searching for a word in every node that isn't ending. However, when I try to debug it, it returns a negative value. I honestly don't know how to solve it, I made constant breakpoints so it would come out of the loop and not do as many iterations but it just keeps going.
I'm very new to C so for me this is a hard thing and I appreciate all the help.
#include "trie.h"
tree_t create_trie()
{
int i = 0;
tree_t node = (tree_t)malloc(sizeof(struct _node));
for(i = 0; i < 26; i++)
{
node->sons[i] = NULL;
}
node->ending = false;
return node;
}
void delete_trie(tree_t trie)
{
int i = 0;
for(i = 0; i < 26;i++)
{
if(trie->sons[i]!=NULL)
{
delete_trie(trie->sons[i]);
}
else
{
continue;
}
}
free(trie);
}
tree_t put_trie(tree_t trie, char* s)
{
int i = 0;
char* t = s;
tree_t aux = trie;
if(lookup_trie(trie, s))
return trie;
for(i = 0; t[i]!='\0'; i++)
{
int it = (int)((char)tolower(t[i]) - 'a'); //le index
printf("\niterator %d\n", it);
if(aux->sons[it] == NULL)
{
aux->sons[it] = create_trie();
//aux->sons[it]->s = (char *)malloc(sizeof(char*));
//printf("created tree");
}
aux = aux->sons[it];
}
aux->ending = true;
return trie;
}
bool lookup_trie(tree_t trie, char* s)
{
int i = 0;
char* t = s;
tree_t aux = trie;
for(i = 0; i<strlen(t); i++)
{
int it = (int)((char)tolower(t[i]) - 'a'); //le index
if(aux->sons[it] == NULL)
{
return false;
}
aux = aux->sons[it];
}
if(aux->ending)
{
return true;
}
return false;
}
void print_trie(tree_t trie)
{
int i = 0;
for(i = 0; i<26; i++)
{
if(trie->sons[i] == NULL)
{
continue;
}
else
{
printf("%c", ((char)(i) + 'a'));
print_trie(trie->sons[i]);
}
//print_trie(trie->sons[i])
printf("\n");
}
}
tree_t share_word(tree_t trie, tree_t auxiliar, char* s)
{
int x = 0, j = 0; //auxiliar to compare and for variable
printf("Share word debug\n");
/*if(!is_vide(trie))
return (tree_t)NULL;*/
printf("here");
printf("%s\n",s);
for(j=0; j<26; j++)
{
for(x = 0; x < strlen(s); x++)
{
int it = (int)((char)tolower(s[x]) - 'a');
printf("%d\n", it);
//if its null i'm gonna look into other nodes from the first for
if(trie->sons[j] == NULL)
{
continue;
}
if(trie->sons[j] == NULL)
{
break;
}
else if(trie->sons[it] == NULL) //might pose a problem
{
//if the node is empty, we just jump to the next iteration
if(lookup_trie(trie->sons[j], s)) //if the suffix is present, return the subtree
{
printf("Word belongs\n");
auxiliar = trie->sons[j]; //is auxiliar JUST A POINTER?
return auxiliar;
}
else //if not present, go again through the function
{
auxiliar = share_word(trie->sons[j], auxiliar, s);
}
}
else //trie->sons[it] not null
{
printf("iterator not null\n");
if(lookup_trie(trie->sons[it], s+x)) //if the word is present
{
return trie->sons[it];
}
}
}
}
return auxiliar;
}
tree_t put_word(tree_t trie, char *s)
{
int i = 0;
tree_t auxiliar = create_trie();
printf("Beggining\n");
//missing a condition so it doesnt add all the suffixes;
auxiliar = share_word(trie, auxiliar, s);
/*for(i = 0; i < strlen(s); i++)
{
int it = (int)((char)tolower(s[i]) - 'a');
auxiliar = share_word(trie, auxiliar, s + i); //should look for the suffixes;
if(auxiliar->sons[it] != NULL )
{
if(trie->sons[it] == NULL)
{
trie->sons[it] = create_trie();
trie->sons[it] = auxiliar;
return trie;
}
}
else
{
printf("Auxiliar null\n");
trie->sons[it] = create_trie(); //proceeds to the next iteration by creating the node
}
//}
trie = trie->sons[it];
}*/
return trie;
}
bool is_vide(tree_t trie)
{
int i =0;
for(i =0; i <26;i++)
{
if(trie->sons[i] != NULL)
{
return false;
}
else
{
return true;
}
}
return true;
}
bool leads_leaf(tree_t trie, char *s)
{
int i = 0;
tree_t aux = trie;
for(i = 0; i < strlen(s); i++)
{
int it = (int)((char)tolower(s[i]) - 'a');
aux = aux->sons[it];
if(i == (strlen(s) - 1) && aux->ending)
{
return true;
}
else
{
return false;
}
}
return false;
}
int main()
{
tree_t tree = create_trie();
tree = put_trie(tree, "abcd");
tree = put_word(tree, "decd");
delete_trie(tree);
return 0;
}
I tried testing putting a string in a single way (abcd) and then searching the string (decd), which would add d, e, and when it would go for c it would find the node of the first strings that leads to c -> d -> '\0'. However, it sometimes just keeps veryfing the same letter (d) and doesn't add it or it just never goes to the next (e).
I think there is a problem with the function put_word and how it calls the share suffixes multiple times.

Anyone know why this code gives wrong output in leet code and works fine in vs code

so basically i am trying to solve a leet code problem called [two sum II] using hashing
but i am getting error in this test case 1,2,3,4,4,9,56,90 where i have to find two index those elements sum is equal to target 8
well the answer of this test case is 4,5 because the sum of index4 and index5 in array[1-8] is 8
Here the problem is when i compiled this below code in vs code it works perfectly fine and gives correct output 4,5
but during leet code submission it throws wrong answer and showing output 1,3 instead of 4,5
// here is my hash implemention code
#include <stdio.h>
#include <stdlib.h>
typedef struct Hash {
int value;
int index;
struct Hash *next;
} hash;
hash *Hashes[10];
int hashify(int value) { return abs(value) % 10; }
void insert(int value, int index) {
int key = hashify(value);
if (Hashes[key] == NULL) {
Hashes[key] = malloc(sizeof(hash));
Hashes[key]->value = value;
Hashes[key]->index = index;
Hashes[key]->next = NULL;
return;
}
hash *ptr = Hashes[key];
while (ptr->next != NULL) ptr = ptr->next;
ptr->next = malloc(sizeof(hash));
ptr->next->value = value;
ptr->next->index = index;
ptr->next->next = NULL;
return;
}
int search(int value) {
int key = hashify(value);
if (Hashes[key] == NULL) return -1;
if (Hashes[key]->value == value)
return Hashes[key]->index;
else {
hash *ptr = Hashes[key]->next;
while (ptr != NULL) {
if (ptr->value == value) return ptr->index;
ptr = ptr->next;
}
return -1;
}
}
// here is hash_free function
void Hash_free() {
for (int i = 0; i < 10; i++) {
if (Hashes[i] == NULL)
continue;
else {
if (Hashes[i]->next == NULL) {
free(Hashes[i]);
Hashes[i] = NULL;
} else {
hash *ptr;
while (ptr != NULL) {
ptr = Hashes[i]->next;
free(Hashes[i]);
Hashes[i] = ptr;
}
}
}
}
}
// here is two sum function code
int *twoSum(int *numbers, int numbersSize, int target, int *returnSize) {
int *result;
if (numbersSize == 2) {
result = malloc(2 * sizeof(int));
result[0] = 1;
result[1] = 2;
*returnSize = 2;
return result;
} else {
int val, element;
for (int i = 0; i < numbersSize; i++) {
val = target - numbers[i];
element = search(val);
if (element != -1) {
result = malloc(2 * sizeof(int));
if (element < i) {
result[0] = element + 1;
result[1] = i + 1;
} else {
result[0] = i + 1;
result[1] = element + 1;
}
*returnSize = 2;
Hash_free();
return result;
}
insert(numbers[i], i);
}
}
return NULL;
}
// here is main code
int main() {
int numbers[] = {1, 2, 3, 4, 4, 9, 56, 90};
int target = 8;
int numberSize = sizeof(numbers) / sizeof(int);
int returnSize;
int *res = twoSum(numbers, numberSize, target, &returnSize);
for (int i = 0; i < returnSize; i++) {
printf("%d ", res[i]);
}
free(res);
return 0;
}
Your "hash" variable is global, so it keeps preserving data of previous testcase executed. I have faced the same when using global variable.
Solution:
Just clear your hash or initialize it, in your main function( or the entry point function), so that it will ensure a fresh start for each of the test cases those are executed.

Passed pointer becomes 0x1

I'm working on a school assignment and am stuck on this part. To summarize what the code is supposed to do, it is meant to put 100 random integers into a B-Tree and print out the sorted list of numbers. I get my random numbers easily enough and begin to insert them using the following function:
void BTree_Insert(BTREE* tree, int* dataInPtr) {
bool taller;
NODE* newPtr;
ENTRY upEntry;
if (tree->root == NULL) {
if (newPtr = (NODE*)malloc(sizeof(NODE))) {
newPtr->firstPtr = NULL;
newPtr->numEntries = 1;
newPtr->entries[0].dataPtr = dataInPtr;
newPtr->entries[0].rightPtr = NULL;
tree->root = newPtr;
(tree->count)++;
for (int i = 1; i < ORDER - 1; i++) {
newPtr->entries[i].dataPtr = NULL;
newPtr->entries[i].rightPtr = NULL;
}
return;
}
else {
printf("Overflow error 100 in BTree_Insert\a\n"), exit(100);
}
}
taller = _insert(tree, tree->root, dataInPtr, &upEntry);
if (taller) {
newPtr = (NODE*)malloc(sizeof(NODE));
if (newPtr) {
newPtr->entries[0] = upEntry;
newPtr->firstPtr = tree->root;
newPtr->numEntries = 1;
tree->root = newPtr;
}
else {
printf("Overflow error 101\a\n"), exit(100);
}
}
(tree->count)++;
return;
}
The first number enters fine (tree->root would be null at this point), but when the second number is inserted, it calls _insert, which is where my problems start. Here is the code for _insert:
bool _insert(BTREE* tree, NODE* root, int* dataInPtr, ENTRY* upEntry) {
if (root == 0x1) {
//printf("root is now 0x1");
}
int compResult;
int entryNdx;
bool taller;
NODE* subtreePtr;
if (!root) {
(*upEntry).dataPtr = dataInPtr;
(*upEntry).rightPtr = NULL;
return true;
}
entryNdx = _searchNode(tree, root, dataInPtr);
compResult = tree->compare(dataInPtr, root->entries[entryNdx].dataPtr);
if (entryNdx <= 0 && compResult < 0) {
subtreePtr = root->firstPtr;
}
else {
subtreePtr = root->entries[entryNdx].rightPtr;
}
taller = _insert(tree, subtreePtr, dataInPtr, upEntry);
if (taller) {
if (root->numEntries >= ORDER - 1) {
_splitNode(root, entryNdx, compResult, upEntry);
taller = true;
}
else {
if (compResult >= 0) {
_insertEntry(root, entryNdx + 1, *upEntry);
}
else {
_insertEntry(root, entryNdx, *upEntry);
}
(root->numEntries)++;
taller = false;
}
}
return taller;
}
When passing through root, it for some reason becomes 0x1 and I get a read access violation. I have verified that in BTree_Insert tree->root is not 0x1, so I really have no idea what's going on. Any insight as to how to fix this would be much appreciated.

priority queue utilizing a heap working except for freeC

I have my priority queue working and printing as I expected it to, but the free is invalid and I'm not sure why, i'm following all the protocols for the heap that I have learned so far, but my program will not finish.
#include <stdlib.h>
#include "priority_queue.h"
struct item{
int priority;
int value;
};
typedef struct item Item;
struct priority_queue{
int size;
int capacity;
int front;
Item* data;
};
typedef struct priority_queue Priority_queue;
void priority_queue_fix_down(PRIORITY_QUEUE hQueue, int index, int size);
PRIORITY_QUEUE priority_queue_init_default(void){
Priority_queue* pQueue_ptr;
pQueue_ptr = (Priority_queue*)malloc(sizeof(Priority_queue));
if(pQueue_ptr != NULL){
pQueue_ptr->size = 0;
pQueue_ptr->capacity = 1;
pQueue_ptr->data = (Item*)malloc(sizeof(Item)*pQueue_ptr->capacity);
if (pQueue_ptr->data == NULL){
free(pQueue_ptr);
pQueue_ptr = NULL;
}
}
return pQueue_ptr;
}
Status priority_queue_insert(PRIORITY_QUEUE hQueue, int priority_level, int data_item)
{
Priority_queue* pQueue_ptr = (Priority_queue*)hQueue;
Item* temp, temp2;
//temp = (Item*)malloc(sizeof());
int i;
if (pQueue_ptr->size >= pQueue_ptr->capacity)
{
//not enough space
temp = (Item*)malloc(sizeof(Item) * 2 * pQueue_ptr->capacity);
if (temp == NULL)
{
return FAILURE;
}
for (i = 0; i < pQueue_ptr->size; i++)
{
temp[i] = pQueue_ptr->data[i];
}
pQueue_ptr->front = 0;
pQueue_ptr->capacity *= 2;
free(pQueue_ptr->data);
pQueue_ptr->data = temp;
}
i = pQueue_ptr->size;
(pQueue_ptr->data[i]).priority = priority_level;
(pQueue_ptr->data[i]).value = data_item;
int index_of_parent;
index_of_parent = (i - 1) / 2;
while (i >= 1 && ((pQueue_ptr->data[i]).priority > (pQueue_ptr->data[index_of_parent]).priority))
{
temp2 = pQueue_ptr->data[index_of_parent];
pQueue_ptr->data[index_of_parent] = pQueue_ptr->data[i];
pQueue_ptr->data[i] = temp2;
i = index_of_parent;
index_of_parent = (i - 1) / 2;
}
pQueue_ptr->size++;
// pQueue_ptr->front = 0;
// pQueue_ptr->back = pQueue_ptr->size-1;
return SUCCESS;
}
void print_heap(PRIORITY_QUEUE hQueue){
Priority_queue* pQueue_ptr = (Priority_queue*) hQueue;
for(int x = 0; x<pQueue_ptr->size;x++){
printf("%d\n", pQueue_ptr->data[x].priority);
}
}
Status priority_queue_service(PRIORITY_QUEUE hQueue){
//set variables
Priority_queue* pQueue_ptr = (Priority_queue*) hQueue;
Item temp;
int size, index, index_left_child, index_right_child;
if(pQueue_ptr->size == 0){
return FAILURE;
}
index = 0;
temp = pQueue_ptr->data[0];
pQueue_ptr->data[0] = pQueue_ptr->data[pQueue_ptr->size-1];
pQueue_ptr->data[size-1] = temp;
pQueue_ptr->size--;
priority_queue_fix_down(pQueue_ptr, pQueue_ptr->front, pQueue_ptr->size);
return SUCCESS;
}
int priority_queue_front(PRIORITY_QUEUE hQueue, Status* status)
{
Priority_queue* pPriority_queue = (Priority_queue*)hQueue;
if (priority_queue_is_empty(pPriority_queue))
{
if (status != NULL)
{
*status = FAILURE;
}
return 0;
}
if (status != NULL)
{
*status = SUCCESS;
}
return (pPriority_queue->data[pPriority_queue->front]).value;
}
Boolean priority_queue_is_empty(PRIORITY_QUEUE hQueue){
Priority_queue* pQueue_ptr = (Priority_queue*) hQueue;
if (pQueue_ptr->size <= 0)
return TRUE;
else
return FALSE;
}
This is where I get the error when debugging, on free(pPriority_queue->data); It doesn't even reach the line below. I tried taking someone elses "service" function and it worked, but they implemented "fix down" in their service function, while I'm trying to do it outside in a separate function.
void priority_queue_destroy(PRIORITY_QUEUE* phQueue){
Priority_queue* pPriority_queue = (Priority_queue*)*phQueue;
free(pPriority_queue->data);
free(*phQueue);
// *phQueue = NULL;
return;
}
void priority_queue_fix_down(PRIORITY_QUEUE hQueue, int index, int size){
Priority_queue* pQueue_ptr = (Priority_queue*) hQueue;
Item* temp;
temp = (Item*)malloc(sizeof(Item));
if (temp == NULL)
return NULL;
//int front = priority_queue_front(pQueue_ptr, NULL);
int index_left_child = 2* index + 1;
int index_right_child = 2* index + 2;
// print_heap(pQueue_ptr);
// printf("\nsize: %d\nindex: %d\n", size, index);
// // printf("Front: %d\n", pQueue_ptr->data[0]);
// if(pQueue_ptr->data[index_left_child].priority > (pQueue_ptr->data[index]).priority){
// printf("Left child index %d\nRight child index: %d\nLeft child has highest priority of: %d\n",index_left_child, index_right_child, pQueue_ptr->data[index_left_child].priority);
// }
// else
// printf("Right child index: %d\nLeft child index %d\nRight child has highest priority of: %d\n",index_right_child, index_left_child, pQueue_ptr->data[index_right_child].priority);
if (index_left_child < size){
if (index_right_child < size && pQueue_ptr->data[index_right_child].priority > (pQueue_ptr->data[index_left_child]).priority){
if(pQueue_ptr->data[index_right_child].priority > (pQueue_ptr->data[index]).priority){
*temp = pQueue_ptr->data[index];
pQueue_ptr->data[index] = pQueue_ptr->data[index_right_child] ;
pQueue_ptr->data[index_right_child] = *temp;
priority_queue_fix_down(pQueue_ptr, index_right_child, size);
}
}
if(pQueue_ptr->data[index_left_child].priority > (pQueue_ptr->data[index]).priority){
*temp = pQueue_ptr->data[index];
pQueue_ptr->data[index] = pQueue_ptr->data[index_left_child];
pQueue_ptr->data[index_left_child] = *temp;
priority_queue_fix_down(pQueue_ptr, index_left_child, size);
}
}
}

How to create AVL

I enter several numbers(2,1,4,5,9,3,6,7),after I enter the number '3', there something wrong,the function can not return correctly.
#include <stdio.h>
#include <stdlib.h>
typedef struct AVLNode
{
int data;
int height;
struct AVLNode *LChild;
struct AVLNode *RChild;
}*AVLTree;
typedef struct AVLNode *Position;
static int Height(Position T)
{
if (T == NULL)
return -1;
else
return T->height;
}
static Position SingleLeft(Position k2)
{
Position k1;
k1 = k2->LChild;
k2->LChild = k1->RChild;
k1->RChild = k2;
k2->height = max(Height(k2->LChild), Height(k2->RChild)) + 1;
k1->height = max(Height(k1->LChild), Height(k1->RChild)) + 1;
return k1;
}
static Position SingleRight(Position k1)
{
Position k2;
k2 = k1->RChild;
k1->RChild = k2->LChild;
k2->LChild = k1;
k1->height = max(Height(k1->LChild), Height(k1->RChild)) + 1;
k2->height = max(Height(k2->LChild), Height(k2->RChild)) + 1;
return k2;
}
static Position DoubleLeft(Position k3)
{
k3->LChild = SingleRight(k3->LChild);
return SingleLeft(k3);
}
static Position DoubleRight(Position k1)
{
k1->RChild = SingleLeft(k1->RChild);
return SingleRight(k1);
}
void PrePrint(AVLTree T)
{
if (T != NULL)
{
printf("%d ", T->data);
PrePrint(T->LChild);
PrePrint(T->RChild);
}
}
AVLTree Insert(int x, AVLTree T)
{
if (T == NULL)
{
T = (AVLTree)malloc(sizeof(struct AVLNode));
T->data = x;
T->LChild = T->RChild = NULL;
}
else if (x < T->data)
{
T->LChild = Insert(x, T->LChild);
if (Height(T->LChild) - Height(T->RChild) == 2)
{
if (x<T->LChild->data)
T = SingleLeft(T);
else
T = DoubleLeft(T);
}
}
else if (x > T->data)
{
T->RChild = Insert(x, T->RChild);
if (Height(T->RChild) - Height(T->LChild) == 2)
{
if (x>T->RChild->data)
T = SingleRight(T);
else
T = DoubleRight(T);
}
}
T->height = max(Height(T->LChild), Height(T->RChild)) + 1;
return T;
}
I think there is something wrong in my main function, I think I shouldn't write
T=(AVLTree)malloc(sizeof(struct AVLNode));
T->LChild = T->RChild = NULL;
those code in mian function, I try to add a 'Init' function, but it doesn't work. It always said "'T' is being used without initialized"
int main()
{
AVLTree T;
T=(AVLTree)malloc(sizeof(struct AVLNode));I think there is wrong
T->LChild = T->RChild = NULL;
int x;
printf("please enter the data(0 to quit):");
scanf("%d", &x);
T->data = x;
while (x != 0)
{
Insert(x, T);
printf("enter a number(0 to quit):");
scanf("%d", &x);
}
PrePrint(T);
}
When your insertion makes a new root node, this fact is not propagated back to main in any way. The value of T inside the Insert function changes, but main has its own variable called T that isn't changed, and that's the one that you then use to print out the tree.
I notice that your Insert function returns an AVLTree, but when main calls it it doesn't do anything with the return value.
(This is not the only thing that's amiss in your code, but it would be a good place to start.)

Resources