Can function returning pointer return negative integers? 
Author Message
 Can function returning pointer return negative integers?

  I'm implementing a hash table and when the client puts a value/item into
the table, there are two possibilities.  Either the value/item is already in
the table or it is not.  If it is not, memory needs to be allocated to make
another link in one of the chains of the table.  This leads to two possible
outcomes, either the memory can be allocated or it can not.  It's a generic
hash table meaning the data is stored into it as void pointers and each item
of data has a specific key that indexes it into the hash table.  Here's what
I'd like to do.  If the client tries to put data into the table and data is
already attached to the specific key for that data, to return the old data
(which will be a pointer) to the client and put the new data in the table.
If the key is not already in the table, just store the pointer in the table.
But either the memory for the new "bucket" can be allocated or it can't.
So here's my problem.  I'd like to return any existing data to the client which
would be a positive return value because it's a pointer.  If the key did not
already exist and the malloc succeeded, I'd like to return NULL.  And if the
malloc fails I'd like to return -1.  But the compiler is complaining about me
returning -1 because the function is declared to return pointers and pointers
can't be negative.  For now, I've typecasted the return statement that's
returning the -1 and the compiler is not complaining.  My concern is something
odd happening like returning -1 and the compiler wrapping it to a positive
number because pointers can't be negative (Or memory addresses can't be
negative at least).  Sorry if that's lengthy but I want to give you guys
enough info to help me understand yet another aspect of the C programming
language.  8)

Robert



Thu, 26 Sep 2002 03:00:00 GMT  
 Can function returning pointer return negative integers?


Quote:
>  I'm implementing a hash table and when the client puts a value/item into
>the table, there are two possibilities.  Either the value/item is already
in
>the table or it is not.  If it is not, memory needs to be allocated to make
>another link in one of the chains of the table.  This leads to two possible
>outcomes, either the memory can be allocated or it can not.  It's a generic
>hash table meaning the data is stored into it as void pointers and each
item
>of data has a specific key that indexes it into the hash table.  Here's
what
>I'd like to do.  If the client tries to put data into the table and data is
>already attached to the specific key for that data, to return the old data
>(which will be a pointer) to the client and put the new data in the table.
>If the key is not already in the table, just store the pointer in the
table.
>But either the memory for the new "bucket" can be allocated or it can't.
>So here's my problem.  I'd like to return any existing data to the client
which
>would be a positive return value because it's a pointer.  If the key did
not
>already exist and the malloc succeeded, I'd like to return NULL.  And if
the
>malloc fails I'd like to return -1.  But the compiler is complaining about
me
>returning -1 because the function is declared to return pointers and
pointers
>can't be negative.  For now, I've typecasted the return statement that's
>returning the -1 and the compiler is not complaining.  My concern is
something
>odd happening like returning -1 and the compiler wrapping it to a positive
>number because pointers can't be negative (Or memory addresses can't be
>negative at least).  Sorry if that's lengthy but I want to give you guys
>enough info to help me understand yet another aspect of the C programming
>language.  8)

While it certainly is improbable that a pointer set to -1 will cause actual
run-time trouble (as long as you don't touch what it points to), your
compiler was right to complain. Generally, 'cramming' variables or return
values is more trouble than it's worth. Lots of standard C functions return
more than one value, but only one value comes out of the function outside
the parenthesis. The only real pointer overloading done to add NULL to
indicate some type of error occurred.

As far as your wrapping concern, well, just don't touch *ptr when ptr==-1.

BTW: IFAIK, pointers are always unsigned. On Intel/DOS, you get near and far
pointers. nears are only 16-bit guys (offset in current segment). fars are
2x16-bits (segment:offset). Setting a near pointer to -1 would yield a near
pointer to 0xFFFF. The only big danger comes in when you have value range
collisions. It is highly improbable that any pointer you actually use will
be 0xFFFF, and as long as you don't point with that crammed value, you can
get away with it.... for now. Pointer cramming is a totally ++ungood
practice.

Even same data type cramming is considered, in most circles, bad code. I've
seen lots of code that would return a positive integer result on success,
but some negative number, or super-high positive number, to indicate what
kind of error occurred. Yuck. That's cramming. Two different types of
information, crammed into one data unit. Avoid it.

instead of your cramming...
    ptr = pHashTable(value, item);
    if ((int)ptr==-1)
        ; // malloc failed
    else if (ptr==NULL)
        ; // malloc did good
    else
        ; // here's the existing data

can't you do this?
    iErr = iHashTable(&ptr, value, item);
    if (!iErr)
        if (ptr!=NULL)
            ; // old key replace
        else
            ; // new key added
    else
        printf("Error #%i occured in iHashTable\n", iErr);

-LZ



Thu, 26 Sep 2002 03:00:00 GMT  
 Can function returning pointer return negative integers?
/ odd happening like returning -1 and the compiler wrapping it to a positive
/ number because pointers can't be negative (Or memory addresses can't be
/ negative at least).  Sorry if that's lengthy but I want to give you guys

An address can have the sign or highest order bit set. It is possible that
an address can have the same bit pattern as -1.

Why not return an address you know can never be duplicated by any other pointer?

    xyz.h

        void *xyz(...);
        void *xyz_mallocFailed;

    xyz.c

        static char xyz_unique;
        void *xyz_mallocFailed = (void*)&xyz_unique;
        void *xyz(...) {...}

    void *v = xyz(...);
    if (v==xyz_mallocFailed) {...}
    else if (v==NULL) {...}
    else {...}

--
CACS: Collective Against Consensual Sanity       v0.123
Now a text site map! http://www.angelfire.com/ca3/cacs/
pretty?     http://www.geocities.com/SoHo/Studios/5079/
:)-free zone.  Don't speak Latin in front of the books!



Thu, 26 Sep 2002 03:00:00 GMT  
 Can function returning pointer return negative integers?

Quote:
>So here's my problem.  I'd like to return any existing data to the
>client which
>would be a positive return value because it's a pointer.  

What makes you think that a "positive" pointer value that points
to valid memory exists?  There's no guarantee that it does.
(Your implementation might treat pointers as unsigned, but there's
no guarantee another one will.)

Quote:
>If the key did not
>already exist and the malloc succeeded, I'd like to return NULL.  And if the
>malloc fails I'd like to return -1.  But the compiler is complaining about me
>returning -1 because the function is declared to return pointers and pointers
>can't be negative.  

Are you sure it's not complaining about returning -1 because the
function is declared to return pointers and -1 isn't a pointer?

Quote:
>For now, I've typecasted the return statement that's
>returning the -1 and the compiler is not complaining.  My concern is something
>odd happening like returning -1 and the compiler wrapping it to a positive
>number

One thing you might have to worry about is malloc() returning -1
when you allocate a hash table entry.  One solution is to check
if it does return (void *)-1, and if so, leak that chunk of memory
and get another one.  This should happen at most once.

Quote:
>because pointers can't be negative

What makes you believe that pointers can be non-negative?

Quote:
>(Or memory addresses can't be
>negative at least).  

What makes you believe this?  Where can I get some of it without
getting nabbed by undercover cops?

                                        Gordon L. Burditt



Fri, 27 Sep 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Pointer to function returning pointer to function returning...

2. Question about signal()/pointers to functions that return pointers to functions

3. Returning Pointer To Pointer From Function

4. Function returning a function-pointer - how to prototype ?

5. Looking for a function which returns the position (integer) of a string in a line

6. Declaring static function returning pointer to extern function

7. About function that returns function pointer

8. Function returning pointers to functions (repost)

9. Function returning pointers to functions

10. Return Structure or return Pointer??

11. (member) function returning pointer to functions like itself?

12. functions returning pointers to functions

 

 
Powered by phpBB® Forum Software