Thread: Another 'dereferencing pointer to incomplete type'

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    33

    Another 'dereferencing pointer to incomplete type'

    Essence of the code regarding issue:
    Code:
    typedef struct BMS_Chkmem{
      int elemsize;
    } BMS_CHKMEM;  
    
    typedef struct BMS_Blkmem{
      BMS_CHKMEM* chkmemhash[1013];
    } BMS_BLKMEM;
    
    typedef struct SCIP_Mem{
      BMS_BLKMEM* setmem;
    } SCIP_MEM;
    
    void* allocMemory(size)
    {
      void* ptr;
      ptr = malloc(size)
      return ptr;
    }
    
    BMS_BLKMEM* createBlockMemory(void)
    {
      int i;
      BMS_BLKMEM* blkmem;
      blkmem = allocMemory(sizeof(*(blkmem)));
    
      for (i=0; i < 1013; i++)
      {
        blkmem->chkmemhash[i] = NULL;
      }
    
      return blkmem;
    }
    
    void memCreate(SCIP_MEM** mem)
    {
      mem = allocMemory(sizeof(**(mem)));
    
      (*mem)->setmem = createBlockMemory();
    
      if ( (*mem)->setmem->chkmemhash[60] == NULL)
        printf("Is null\n");
      else
        printf("Is not null\n");
    }
    
    int main(void)
    {
      SCIP_MEM* mem;
      memCreate(&mem);
      return 0;
    }
    The above code is simple the essence of what I'm using, I'm not even sure if it would compile, but I hope it contains everything needed.

    memCreate is called, and is passed a pointer of a higher struct, which at the time is NULL, the idea being to allocate it some memory, and initialize it. When I try to compile the code that this is taken from, I get an error of:
    dereferening pointer to incomplete type
    This error relates to:
    if ( (*mem)->setmem->chkmemhash[60] == NULL)
    From what I can see, this should work, but clearly I'm overlooking something.
    Last edited by thealmightyone; 11-24-2010 at 05:26 PM. Reason: Edit to code to make it compiliable.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Well, I was able to make small modifications to the posted code and it worked just fine, so it must be something in the code you didn't post.

    My code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct BMS_Chkmem{
      int elemsize;
    } BMS_CHKMEM;  
    
    typedef struct BMS_Blkmem{
      BMS_CHKMEM* chkmemhash[1013];
    } BMS_BLKMEM;
    
    typedef struct SCIP_Mem{
      BMS_BLKMEM* setmem;
    } SCIP_MEM;
    
    void* allocMemory(size)
    {
      void* ptr = malloc(size);
      return ptr;
    }
    
    BMS_BLKMEM* createBlockMemory(void)
    {
      int i;
      BMS_BLKMEM* blkmem;
      blkmem = allocMemory(sizeof(*(blkmem)));
    
      for (i=0; i < 1013; i++)
      {
        blkmem->chkmemhash[i] = NULL;
      }
    
      return blkmem;
    }
    
    void memCreate(SCIP_MEM** mem)
    {
      mem = allocMemory(sizeof(**(mem)));
    
      (*mem)->setmem = createBlockMemory();
    
      if ( (*mem)->setmem->chkmemhash[60] == NULL)
        printf("Is null\n");
      else
        printf("Is not null\n");
    }
    
    int main(void)
    {
        SCIP_MEM *mem;
        memCreate(&mem);
        return 0;
    }
    
    My output:
    Is null
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    33
    Quote Originally Posted by itsme86 View Post
    Well, I was able to make small modifications to the posted code and it worked just fine, so it must be something in the code you didn't post.
    Well, the code I posted is a summary of what happens to variables related to the issue I had, thus I left out a LOT of code which I deemed to be unrelated.

    Interestingly, when I compile the code that you posted (gcc -Wall prog.c -o prog) and run, I get a seg fault at:
    Code:
    (*mem)->setmem = createBlockMemory();
    Ie, it appears to occur duing the assignment, rather than during the function execution, as the function executes fine all the way through to "return blkmem". Any reason why I would get a seg fault and you not? I'm just wondering if this difference could explain my original issue.
    Last edited by thealmightyone; 11-24-2010 at 05:38 PM.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I honestly cannot see any errors in the code I posted. Maybe someone else can spot something. I even tried compiling with gcc -Wall -std=c99 -pedantic and the only warning I get is that the type of size defaults to int (here: void* allocMemory(size)).
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Looking at how you call this function.


    Code:
    void memCreate(SCIP_MEM** mem)
    {
      mem = allocMemory(sizeof(**(mem)));  
    // shouldn't it be   *mem = allocMemory( sizeof(**mem) );  since you can't modify mem
    // to change in caller!
    
      (*mem)->setmem = createBlockMemory();
    
      if ( (*mem)->setmem->chkmemhash[60] == NULL)
        printf("Is null\n");
      else
        printf("Is not null\n");
    }

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by Bayint Naung View Post
    Looking at how you call this function.


    Code:
    void memCreate(SCIP_MEM** mem)
    {
      mem = allocMemory(sizeof(**(mem)));  
    // shouldn't it be   *mem = allocMemory( sizeof(**mem) );  since you can't modify mem
    // to change in caller!
    
      (*mem)->setmem = createBlockMemory();
    
      if ( (*mem)->setmem->chkmemhash[60] == NULL)
        printf("Is null\n");
      else
        printf("Is not null\n");
    }
    Ahh, good catch! Can't believe I did that. Must have been day-before-thanksgiving-blindness.
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    33
    Hi itsme86. Could I get you to try running the following code, and let me know if it seg faults. If not, can you post your gcc version ("gcc -v").

    [CODE]#include <stdio.h>
    #include <stdlib.h>

    typedef struct BMS_Chkmem{
    int elemsize;
    } BMS_CHKMEM;

    typedef struct BMS_Blkmem{
    BMS_CHKMEM* chkmemhash[1013];
    } BMS_BLKMEM;

    typedef struct SCIP_Mem{
    BMS_BLKMEM* setmem;
    } SCIP_MEM;

    typedef struct Scip{
    SCIP_MEM* memblk;
    } SCIP;

    void* allocMemory(size)
    {
    void* ptr = malloc(size);
    return ptr;
    }

    BMS_BLKMEM* createBlockMemory(void)
    {
    int i;
    BMS_BLKMEM* blkmem;
    blkmem = allocMemory(sizeof(*(blkmem)));

    for (i=0; i < 1013; i++)
    {
    blkmem->chkmemhash[i] = NULL;
    }

    return blkmem;
    }

    void memCreate(SCIP_MEM** mem)
    {
    *mem = allocMemory(sizeof(**(mem)));

    (*mem)->setmem = createBlockMemory();
    }

    void scipCreate(SCIP** scip)
    {
    SCIP_MEM* mem;
    memCreate(&mem);
    printf("Before assignment\n");
    printf("Pointer check: %p\n", mem);
    (*scip)->memblk = mem;
    printf("After assignment\n");

    if ( (*scip)->memblk->setmem->chkmemhash[60] == NULL)
    printf("Is null\n");
    else
    printf("Is not null\n");
    }

    int main(void)
    {
    SCIP* scip;
    scipCreate(&scip);

    return 0;
    }[/CODE/

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Yup, it crashes right after the before assignment pointer check.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error: dereferencing pointer to incomplete type
    By kiros88 in forum C Programming
    Replies: 4
    Last Post: 09-16-2009, 02:43 PM
  2. error: dereferencing pointer to incomplete type
    By surlyTomato in forum C Programming
    Replies: 10
    Last Post: 08-22-2009, 08:04 AM
  3. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  4. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  5. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM