• Home   /  
  • Archive by category "1"

Gcc Warning Assignment From Incompatible Pointer Type Enabled By Default

  1. Registered User
    Join Date
    Sep 2007
    Posts
    30

    Initialization from incompatible pointer type

    Can anyone help with the below error, I'm not sure where I've gone wrong and why the pointer is wrong.

    tree.c: In function 'freeTree':
    tree.c:85: warning: initialization from incompatible pointer type

    #include <stdio.h> #include <stdlib.h> /* Small program to explore using binary trees. The program takes an array filled with random numbers and inserts the numbers into a binary tree. The program then prints the contents of the tree in key order. It should print - 0 1 2 3 4 5 6 7 8 9 The program then free's up the tree. */ struct treeRecord { int key; struct treeRecord *left, *right; }; typedef struct treeRecord treeNode; void insertArrayInTree(treeNode **root, int array[], int size); void insertElementInTree(treeNode **root, int number); void printTree(treeNode *start); void freeTree(treeNode **start); treeNode* newNode(int number); int main() { int array[] = { 6, 3, 8, 1, 5, 2, 9, 7, 4, 0 }; treeNode *root = NULL; insertArrayInTree(&root, array, 10); printTree(root); printf("\n"); freeTree(&root); return 0; } void insertArrayInTree(treeNode **root, int array[], int size) { /* insert elements of array into correct position in binary tree */ int i; for (i=0; i<size; i++) { insertElementInTree(root, array[i]); } } void insertElementInTree(treeNode **root, int number) { treeNode *temp; if (*root == NULL) { *root = newNode(number); } else { temp = *root; if (number < temp->key) insertElementInTree(&temp->left, number); else insertElementInTree(&temp->right, number); } } void printTree(treeNode *root) { /* print the contents of the tree in order */ while (root != NULL) { printTree(root->left); printf(" %d", root->key); printTree(root->right); } } void freeTree(treeNode **root) { /* free up memory allocated to binary tree */ treeNode *temp = root; if (temp != NULL) { if (temp->left != NULL) freeTree(&temp->left); if (temp->right != NULL) freeTree(&temp->right); free(temp); *root = NULL; } } treeNode* newNode(int number) { /* dynamicaly allocate memory for a treeNode make sure it is initialised correctly */ treeNode *temp; temp = (treeNode *) malloc(sizeof(treeNode)); if (temp == NULL) { printf("WARNING - Memory allocation error\n"); exit(EXIT_FAILURE); } temp->key = number; temp->left = NULL; return temp; }

  2. Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.
    I'm only guessing that this is line 85:
    treeNode *temp = root;
    Perhaps you want *temp = *root, since root is treeNode **, and thus *root has teh type treeNode *??

    --
    Mats


  3. Registered User
    Join Date
    Sep 2007
    Posts
    30
  4. Registered User
    Join Date
    Sep 2007
    Posts
    30
    I've got another error in the code somewhere but I can't figure out where it is.

    At the moment when I run it I'm not getting any output, just a blank line.

    I'm not to sure what the error is though.

    Any help would be appreciated it.

    #include <stdio.h> #include <stdlib.h> /* Small program to explore using binary trees. The program takes an array filled with random numbers and inserts the numbers into a binary tree. The program then prints the contents of the tree in key order. It should print - 0 1 2 3 4 5 6 7 8 9 The program then free's up the tree. */ struct treeRecord { int key; struct treeRecord *left, *right; }; typedef struct treeRecord treeNode; void insertArrayInTree(treeNode **root, int array[], int size); void insertElementInTree(treeNode **root, int number); void printTree(treeNode *start); void freeTree(treeNode **start); treeNode* newNode(int number); int main() { int array[] = { 6, 3, 8, 1, 5, 2, 9, 7, 4, 0 }; treeNode *root = NULL; insertArrayInTree(&root, array, 10); printTree(root); printf("\n"); freeTree(&root); return 0; } void insertArrayInTree(treeNode **root, int array[], int size) { /* insert elements of array into correct position in binary tree */ int i; for (i=0; i<size; i++) { insertElementInTree(root, array[i]); } } void insertElementInTree(treeNode **root, int number) { treeNode *temp; if (*root == NULL) { *root = newNode(number); } else { temp = *root; if (number < temp->key) insertElementInTree(&temp->left, number); else insertElementInTree(&temp->right, number); } } void printTree(treeNode *root) { /* print the contents of the tree in order */ while (root == NULL) { printTree(root->left); printf(" %d", root->key); printTree(root->right); } } void freeTree(treeNode **root) { /* free up memory allocated to binary tree */ treeNode *temp = *root; if (temp != NULL) { if (temp->left != NULL) freeTree(&temp->left); if (temp->right != NULL) freeTree(&temp->right); free(temp); *root = NULL; } } treeNode* newNode(int number) { /* dynamicaly allocate memory for a treeNode make sure it is initialised correctly */ treeNode *temp; temp = (treeNode *) malloc(sizeof(treeNode)); if (temp == NULL) { printf("WARNING - Memory allocation error\n"); exit(EXIT_FAILURE); } temp->key = number; temp->left = NULL; return temp; }

  5. Chinese pâté
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Hum... let me guess...
    while (root == NULL) { printTree(root->left); printf(" &#37;d", root->key); printTree(root->right); }
    ...maybe you wanted to write this....

    while (root != NULL) { printTree(root->left); printf(" %d", root->key); printTree(root->right); }
    Note that you'll have infinte loop since you aren't changing the value of root. (Why a loop on a recursive function like this by the way? Maybe an if statement would be more appropriate). I didn't read the whole code by the way, just took a fast look at it.

  6. Registered User
    Join Date
    Sep 2007
    Posts
    30
    Hmm I changed that from != to == to stop the inifite "0" output. So I guess I would need to think of another way to get around that then...

  7. Registered User
    Join Date
    Sep 2007
    Posts
    30
    We aren't allowed to change the structure of the code, we can only make it work so I have to use the loop unfortunately.

    If I shifted the code around a little so that it was like below would that fix it?

    while (root == NULL) { printTree(root->left); printTree(root->right); printf(" %d", root->key); }

  8. Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    No that won't work. That loop will never run.
    The real problem seems to be that you never initialize node->right in function newNode().
    Kurt
    If I shifted the code around a little so that it was like below would that fix it?

    while (root == NULL) { printTree(root->left); printTree(root->right); printf(" %d", root->key); }

  9. Registered User
    Join Date
    Sep 2007
    Posts
    30
    So I would need to use something like:

    treeNode* newNode(int number) { /* dynamicaly allocate memory for a treeNode make sure it is initialised correctly */ treeNode *temp; temp = (treeNode *) malloc(sizeof(treeNode)); if (temp == NULL) { printf("WARNING - Memory allocation error\n"); exit(EXIT_FAILURE); } temp->key = number; temp->left = NULL; temp->right = NULL; return temp; }

  10. Registered User
    Join Date
    Sep 2007
    Posts
    30
    Ok so that didn't work and I'm really not sure of anywhere else to go... Any other ideas of where it is wrong?


The following sentence: "If the size is zero and the pointer is not null, then the pointer will be freed." along with second row in Table 2-2: "Not null 0 Original block is freed" is not entirely true in my opinion. The actual behaviour is implementation-defined. As by C11 7.22.3/p1 Memory management functions: If the size of the space requested is zero, the behavior is implementation-defined: either a null pointer is returned, or the behavior is as if the size were some nonzero value, except that the returned pointer shall not be used to access an object. and C11 7.22.3.5: 3) (...) If memory for the new object cannot be allocated, the old object is not deallocated and its value is unchanged. 4) The realloc function returns a pointer to the new object (which may have the same value as a pointer to the old object), or a null pointer if the new object could not be allocated. I made some basic example, that compiles with GCC 4.4.7 on GNU/Linux CentOS 6, that indicates that memory is not freed: #include <mcheck.h> #include <stdio.h> #include <stdlib.h> int main(void) { int a = 5; int *p, *q; mtrace(); p = malloc(sizeof(int)); q = &a; printf("%p\n", (void *) p); printf("%p\n", (void *) q); q = realloc(p, 0); printf("%p\n", (void *) p); printf("%p\n", (void *) q); return 0; } $ gcc -g check.c $ export MALLOC_TRACE=report $ ./a.out 0xd4e460 0x7fff5fa0ecdc 0xd4e460 (nil) $ mtrace a.out report Memory not freed: ----------------- Address Size Caller 0x0000000000d4e460 0x4 at /home/grzegorz/workspace/check.c:12 As you can see the q was set to NULL with realloc call. According to spec this clearly means that realloc failed to allocate block with zero, thus p is not changed. The mtrace tool also indicates that it happened in that way. I am marking it as question, because I am not sure how to rewrite the sentence. Maybe the simplest way is to put this as "implementation-defined" and that deallocation is not guaranteed. Curiously, man realloc (on my system) says that: If ptr is NULL, then the call is equivalent to malloc(size), for all values of size; if size is equal to zero, and ptr is not NULL, then the call is equivalent to free(ptr). which seems to untrue according to above experiment.

Note from the Author or Editor:
Page 44 Last sentence "The function's behavior is summarized in Table 2-2." Add the following statement after the sentence: "Bear in mind that the actual behavior of the realloc function is implementation dependent. Always verify its behavior before use."

One thought on “Gcc Warning Assignment From Incompatible Pointer Type Enabled By Default

Leave a comment

L'indirizzo email non verrà pubblicato. I campi obbligatori sono contrassegnati *