can char ** be passed to void * type? 
Author Message
 can char ** be passed to void * type?

Hello everyone
     I am using the realloc() function. The prototype of that function is

void *realloc(void *ptr,size_t size);

And I am passing a pointer of type char ** to void *.

char** command(char *cmd)
{
  char** cmdTokens;
  char** newLoc; /* new location for the command and arguments */
  ....... other codes....

  ++len;
  newLoc = realloc(cmdTokens,len*sizeof(char *));
  if( newLoc == NULL )
  {
    perror("realloc()");
    exit(2);
  }
  cmdTokens = newLoc;
  cmdTokens[len-1] = token; ......

Quote:
}

will this cause any unexpected results?
By the way, can I write

newLoc = realloc(cmdTokens,len);

instead of

newLoc = realloc(cmdTokens,len*sizeof(char *)); ?

Will the compiler figure out that it should allocate
array of char * by knowing cmdTokens's type is char **??

Thank you very much

Dean( a C rookie)



Wed, 05 Oct 2005 01:42:58 GMT  
 can char ** be passed to void * type?

Quote:

> Hello everyone
>      I am using the realloc() function. The prototype of that function is

> void *realloc(void *ptr,size_t size);

> And I am passing a pointer of type char ** to void *.

> char** command(char *cmd)
> {
>   char** cmdTokens;
>   char** newLoc; /* new location for the command and arguments */
>   ....... other codes....

>   ++len;
>   newLoc = realloc(cmdTokens,len*sizeof(char *));
>   if( newLoc == NULL )
>   {
>     perror("realloc()");
>     exit(2);
>   }
>   cmdTokens = newLoc;
>   cmdTokens[len-1] = token; ......
> }

> will this cause any unexpected results?

    It depends on what you expect ;-)

    No; the use of realloc() looks correct, provided the
starting value of `cmdTokens' is valid and `len' is sensible.
Many people (myself included) suggest writing

        newLoc = realloc(cmdTokens, len * sizeof *newLoc);

... which relieves you of the burden of trying to remember
whether `newLoc' is a `char*' or a `char**' or whatever.

    By the way, exit(2) is a bit peculiar.  If `2' means
something special to your platform and you want to indicate
that special meaning when the program fails, go right ahead.
But the Standard also provides `EXIT_FAILURE' as a portable
exit code to indicate "generic" failure (along with zero and
`EXIT_SUCCESS' to indicate "generic" success).

Quote:
> By the way, can I write

> newLoc = realloc(cmdTokens,len);

> instead of

> newLoc = realloc(cmdTokens,len*sizeof(char *)); ?

> Will the compiler figure out that it should allocate
> array of char * by knowing cmdTokens's type is char **??

    No; the second argument to realloc() indicates the number
of *bytes* desired, not the number of "things" desired.  If
you rewrite the call as suggested above, you'll get pretty
much the desired effect, though: the compiler will figure
things out for you.

--



Wed, 05 Oct 2005 02:31:23 GMT  
 can char ** be passed to void * type?

Quote:

> Hello everyone
>      I am using the realloc() function. The prototype of that function is

> void *realloc(void *ptr,size_t size);

> And I am passing a pointer of type char ** to void *.

That's fine.  Conversions between void * and any pointer-to-object
type are well-defined, and round-trip lossless (with respect to value
equality).

Quote:
> By the way, can I write

> newLoc = realloc(cmdTokens,len);

> instead of

> newLoc = realloc(cmdTokens,len*sizeof(char *)); ?

> Will the compiler figure out that it should allocate
> array of char * by knowing cmdTokens's type is char **??

No.  There's nothing special about the the second argument of realloc.

Jeremy.



Wed, 05 Oct 2005 02:45:51 GMT  
 can char ** be passed to void * type?

Quote:
>Hello everyone
>     I am using the realloc() function. The prototype of that function is

>void *realloc(void *ptr,size_t size);

>And I am passing a pointer of type char ** to void *.

>char** command(char *cmd)
>{
>  char** cmdTokens;
>  char** newLoc; /* new location for the command and arguments */
>  ....... other codes....

>  ++len;
>  newLoc = realloc(cmdTokens,len*sizeof(char *));
>  if( newLoc == NULL )
>  {
>    perror("realloc()");
>    exit(2);
>  }
>  cmdTokens = newLoc;
>  cmdTokens[len-1] = token; ......
>}

>will this cause any unexpected results?
>By the way, can I write

>newLoc = realloc(cmdTokens,len);

>instead of

>newLoc = realloc(cmdTokens,len*sizeof(char *)); ?

It will work only if sizeof (char *) happens to be 1. This is not
generally the case.

Quote:
>Will the compiler figure out that it should allocate
>array of char * by knowing cmdTokens's type is char **??

Malloc() and realloc() and friends are blissfully ignorant of the type
of data you put in the storage they provide. Their prototypes act just
like those of any user written function. Pointers in are converted to
void* before the functions are called, (because the prototypes specify
that the pointer argument is a void*.)

Result pointers are void*, implicitly converted when assigned to a
variable of another pointer type.

There is no magic to allow these functions to detect the type of an
argument before conversion, or the type to which the result will be
converted. It's up to the programmer to provide the size value
denominated in bytes.

As a matter of style, I'd write this as:

  {
    void *newLoc = realloc(cmdTokens, len * sizeof *cmdTokens);
    if (newLoc != 0) {
      cmdTokens = newLoc;
    }
    else {
      /* handle failure */
    }
  }

The difference being this version does not restate the type of
cmdTokens explicitly. Therefore, it's less likely to be wrong, and it
does not need to change should the declared type of cmdTokens change.
Also, the scope of newLoc is restricted to the part of program where
the variable is meaningful.

When the type of the pointer is unequivocally char*, it's customary to
omit the sizeof since sizeof (char) is 1 by definition. Thus,

   char **pp = malloc(len * sizeof *pp)
but
   char *pc = malloc(len);



Wed, 05 Oct 2005 07:46:18 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. passing lists as char ** to void **

2. passing pointers of type void?

3. #define void int vs. #define void char

4. passing void func(void *) to a function

5. void pointer (void *) : how to determine type?

6. from void (void*) to void (_cdecl*) (void*)

7. HRESULT return type turns into void when adding class from type library

8. Passing int vs. passing char

9. passing const char* to char* fails

10. Passing a char array[n] to a function expecting a char*

11. what is the difference between this types: char and System.Char

12. (Multiple) converting type list char/CString/char[]

 

 
Powered by phpBB® Forum Software