0

I am having a pointer or memory-related problem here:

    void nodesVal(struct TreeNode* root, int *returnSize, int **returnArray){
    if (root != NULL)
    {
        *(returnSize) = *(returnSize) + 1;
        *returnArray[*(returnSize) - 1] = root->val;  
// I get a runtime memory error whenever I try to access ths array
        if (root->left != NULL) nodesVal(root->left, returnSize, returnArray);
        
        if (root->right != NULL) nodesVal(root->right, returnSize, returnArray);
    }
    
    
}


int* preorderTraversal(struct TreeNode* root, int* returnSize){
    int *returnArray = (int*)malloc(sizeof(int) * 100);
    *returnSize = 0;
    
    
    nodesVal(root, &returnSize, &returnArray);
    return returnArray;
    
}

Does anyone see where I am going wrong?

blueshift
  • 6,742
  • 2
  • 39
  • 63
  • Please describe the exact behaviour you are seeing. Usually by providing the exact input, expected result and actual result. Also importantly, questions seeking debugging help must provide complete code as a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – kaylum Jun 29 '21 at 23:54
  • It's mostly a shot in the dark, but what happens if you access it as `(*(returnArray))[*(returnSize) - 1]`? – Nick Reed Jun 30 '21 at 00:08

1 Answers1

0

Major changes: //-------->
Minor changes: //

#include <stdio.h>
#include <stdlib.h>

struct TreeNode
{
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
};
void nodesVal(struct TreeNode *root, int *returnSize, int **returnArray)
{
    if (root != NULL)
    {
        //-------> (*returnArray)[0] instead of *returnArray[0] ----> [] has higher priority than *
        // I changed the order of those two lines so that I don't say size++ and then use size-1 (you can ignore this)
        (*returnArray)[*(returnSize)] = root->val;
        *(returnSize) = *(returnSize) + 1;
        // when you enter this, you already check for NULL, so I removed (if) --- However, this can be ignored
        nodesVal(root->left, returnSize, returnArray);
        nodesVal(root->right, returnSize, returnArray);
    }
}

int *preorderTraversal(struct TreeNode *root, int *returnSize)
{
    int *returnArray = (int *)malloc(sizeof(int) * 100);
    *returnSize = 0;

    // ---------> you were passing &returnSize (int**). However, the function needs (int*). So, pass returnSize
    nodesVal(root, returnSize, &returnArray);
    return returnArray;
}

void constructNode(struct TreeNode**root,struct TreeNode*left,struct TreeNode* right,int val){
    (*root)->left = left;
    (*root)->right = right;
    (*root)->val = val;
}
int main()
{
    struct TreeNode*root = (struct TreeNode*)malloc(sizeof(struct TreeNode));
    struct TreeNode*node1 = (struct TreeNode*)malloc(sizeof(struct TreeNode));
    struct TreeNode*node2 = (struct TreeNode*)malloc(sizeof(struct TreeNode));
    struct TreeNode*node3 = (struct TreeNode*)malloc(sizeof(struct TreeNode));
    constructNode(&root,node1,node2,1);
    constructNode(&node1,node3,NULL,2);
    constructNode(&node2,NULL,NULL,3);
    constructNode(&node3,NULL,NULL,4);
    int* returnedSize = malloc(sizeof(int));
    int* arr = preorderTraversal(root,returnedSize);
    for (int i = 0; i < *returnedSize; i++)
    {
        printf("%d ",arr[i]);
    }
    printf("\n");

    free(arr);
    free(returnedSize);
    free(root);
    free(node1);
    free(node2);
    free(node3);
    return 0;
}
Mohamed Akram
  • 416
  • 4
  • 12
  • Amazing! Man, you are a godsend! Seriously. It apears the first major change was what was tricking me, but honestly, I'm not so sure. The second part, I had already tried with (int*) and with (int**) so I believe I just sent the final version with a typo. Thank you, so much. Just one more question: are we suposed to cast the malloc? I've seen people say it is not necessary, some say it is bad practice, and it normaly seems to work regardless of if we do it or not. What is your view on this? – Fernando Loula Jun 30 '21 at 09:19
  • In this [link](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) From the comments, it seems like it used to cause problems in the past. Also, it looks like there are opinions on this topic. – Mohamed Akram Jun 30 '21 at 14:38