Thread: Use of 'const' with pointers to pointers

  1. #1
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303

    Use of 'const' with pointers to pointers

    I've gotten myself into a little confusion regarding the use of const and pointers to pointers of const arrays. Take for example this function:

    Code:
    void list_alpha(const struct seat ar[])
    {
        const struct seat** pfull;
        int nFull = MAXSEATS - num_empty(ar);
        int i, j, count;
        const struct seat* temp;
        char iname[NAMESIZE * 2];
        char jname[NAMESIZE * 2];
    
        pfull = (struct seat**) malloc(nFull * sizeof(struct seat*));
        for (i = 0, count = 0; i < MAXSEATS; i++)
            if (ar[i].taken)
            {
                pfull[count] = &(ar[i]);
                count++;
            }
    
        for (i = 0; i < (count - 1); i++)
            for (j = i + 1; j < count; j++)
            {
                strcpy(iname, pfull[i]->lname);
                strcat(iname, pfull[i]->fname);
                strcpy(jname, pfull[j]->lname);
                strcat(jname, pfull[j]->fname);
                if (strcmp(iname, jname) > 0)
                {
                    temp = pfull[i];
                    pfull[i] = pfull[j];
                    pfull[j] = temp;
                }
            }
    
        for (i = 0; i < count; i++)
            printf("%d: %s, %s\n", pfull[i]->id, pfull[i]->lname, pfull[i]->fname);
    
        free(pfull);
        return;
    }
    The call to free() at the end brings forth the warning : 'function' : different 'const' qualifiers - and I just can't get my pretty little head around the reason why. Any help?

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    free() takes a non-const pointer, plain and simple. The assumption is that you would never have dynamically allocated a const array since the only purpose in doing that is to write data into it. (I know this argument is weak and not even completely correct, but it's the only reason I can imagine why free() would have been made to take a non-const)

    You can still maintain a const pointer to the array for any code which doesn't need to modify it, but as far as allocating/deallocating, it should not be const. Either that, or cast away the const-ness when you call free(), but I'd recommend the former solution.
    Last edited by brewbuck; 03-27-2009 at 12:01 AM.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    My guess is that you should change this:
    Code:
    pfull = (struct seat**) malloc(nFull * sizeof(struct seat*));
    to:
    Code:
    pfull = malloc(nFull * sizeof(*pfull));
    Quote Originally Posted by brewbuck
    free() takes a non-const pointer, plain and simple.
    Yes, but pfull is a non-const pointer to a pointer to const, unless I read it wrongly.
    Last edited by laserlight; 03-27-2009 at 12:02 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    Point taken! I un-const'd pfull and temp and the warning is no more. Thanks.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by laserlight View Post
    My guess is that you should change this:
    Code:
    pfull = (struct seat**) malloc(nFull * sizeof(struct seat*));
    to:
    Code:
    pfull = malloc(nFull * sizeof(*pfull));

    Yes, but pfull is a non-const pointer to a pointer to const, unless I read it wrongly.
    I read it as "a pointer to a pointer to a struct seat, which is const." At any rate, the compiler doesn't like it.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    Quote Originally Posted by laserlight View Post
    My guess is that you should change this:
    Code:
    pfull = (struct seat**) malloc(nFull * sizeof(struct seat*));
    to:
    Code:
    pfull = malloc(nFull * sizeof(*pfull));
    But if I don't cast pfull, how can I use array notation on it? My understanding was that without the cast, malloc returns pointer-to-void?

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Sharke View Post
    But if I don't cast pfull, how can I use array notation on it? My understanding was that without the cast, malloc returns pointer-to-void?
    it is not important what malloc returns - important where you store the value - I mean what it the type of pfull

    if the type is incorrect - cast while assigning value will not help later access it. Value has no type - variable has type
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    I see! Another point taken.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function template has already been defined
    By Elysia in forum C++ Programming
    Replies: 19
    Last Post: 04-14-2009, 10:17 AM
  2. The so annoying LNK2005... please help!
    By Mikey_S in forum C++ Programming
    Replies: 14
    Last Post: 02-01-2009, 04:22 AM
  3. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  4. fatal error LNK1104
    By DMH in forum C++ Programming
    Replies: 2
    Last Post: 11-16-2005, 03:46 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM