assigning values to an array of type void? 
Author Message
 assigning values to an array of type void?

I'm creating a function (merely for test purposes) that takes a ptr to
a malloc()d char array and realloc()s space for another character then
assigns a value to that character.  Now, my only question is how?  I'd
like to pass a void ptr to the buffer so that I could do this same
thing for an array of ints.  Here's my code:

int main(void)
{
        char *tmp = malloc(3);

        tmp[0] = 'c';
        tmp[1] = 'h';
        tmp[2] = 'a';

        push(tmp, 'd');

        return 0;

Quote:
}

int push(void *ptr, char ch)
{
        int i;

        ptr = realloc(ptr, _msize(ptr) + sizeof ch);
        i = _msize(ptr);

        ptr[i] = ch;
        printf("%d\n", i);

        return i;

Quote:
}

To clear a few things up... I am aware of the pitfalls in the above
code.  I do normally check the return of malloc(), but this was just
for a test.  How do I cast the array to type char* so that I can assign
a new char to it?  Any help would be appreciated, as always.

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Sun, 21 Jul 2002 03:00:00 GMT  
 assigning values to an array of type void?
: int push(void *ptr, char ch)

pass it as a void **ptr (by reference).  you'll see why later on

: {
:       int i;

:       ptr = realloc(ptr, _msize(ptr) + sizeof ch);

what happens if ptr changes?  the caller (main) will never know about it
because ptr is passed by value.  that == badness.

:       i = _msize(ptr);

this is not portable.

:       ptr[i] = ch;

if ptr were void ** instead of void *, then:

        ((char **)ptr)[i] = ch;

would do the trick i think.  i find this to be more readable though:

        char **ptr_ch = ptr;

        (*ptr_ch)[i] = ch;

:       printf("%d\n", i);

:       return i;
: }

as for the _msize thing: there is no portable way to know how much memory
was allocated for a given pointer UNLESS YOU KEEP TRACK OF IT YOURSELF.  my
recommendation would be that instead of just passing a void * as the stack,
use a structure:

struct stack_t {
        void *ptr;
        size_t length;

Quote:
};

and then instead of _msize(*ptr), you can use ptr->length; (and then you'd
have to use ptr->ptr instead of ptr from then on.  btw i'd recommend picking
better identifiers too; ptr->ptr looks really dumb)


Sun, 21 Jul 2002 03:00:00 GMT  
 assigning values to an array of type void?


Quote:

>: int push(void *ptr, char ch)

>pass it as a void **ptr (by reference).  you'll see why later on

>: {
>:       int i;

>:       ptr = realloc(ptr, _msize(ptr) + sizeof ch);

>what happens if ptr changes?  the caller (main) will never know about it
>because ptr is passed by value.  that == badness.

And what happens if realloc() fails? You have a likely memory leak.

Quote:

>:       i = _msize(ptr);

>this is not portable.

>:       ptr[i] = ch;

>if ptr were void ** instead of void *, then:

>        ((char **)ptr)[i] = ch;

this is attempting to assign a char value to a (probably out of bounds)
void * object while pretending that it is a char * object.

Quote:
>would do the trick i think.

Which trick is that? :-) I'd hazard a guess that you meant this:

         ((char *)*ptr)[i] = ch;

Quote:
> i find this to be more readable though:

>        char **ptr_ch = ptr;

Not valid if ptr has type void **. There are no implicit conversions
to and from void ** as there are for void *. void ** is nothing more
than a pointer that can point at void * objects.

Quote:
>        (*ptr_ch)[i] = ch;

Try

         char *ptr_ch = *ptr;

         ptr_ch[i] = ch;

--
-----------------------------------------


-----------------------------------------



Tue, 23 Jul 2002 03:00:00 GMT  
 assigning values to an array of type void?

Quote:

> Not valid if ptr has type void **. There are no implicit conversions
> to and from void ** as there are for void *. void ** is nothing more
> than a pointer that can point at void * objects.

aww no fun.

Quote:
>>        (*ptr_ch)[i] = ch;
> Try
>          char *ptr_ch = *ptr;
>          ptr_ch[i] = ch;

don't you lose the pass-by-reference-ness then?  the original source isn't
fresh in my mind (in fact it's hardly in my mind at all), but if i remember
right that's the whole reason i changed the void * to void **.  mind you i
guess you could just reset it at the end, so it doesn't matter terribly :)

--
              /"\                              m i k e    b u r r e l l

               X        AGAINST HTML MAIL      http://mikpos.dyndns.org
              / \



Wed, 24 Jul 2002 03:00:00 GMT  
 assigning values to an array of type void?


Quote:

>> Not valid if ptr has type void **. There are no implicit conversions
>> to and from void ** as there are for void *. void ** is nothing more
>> than a pointer that can point at void * objects.

>aww no fun.

>>>        (*ptr_ch)[i] = ch;

>> Try

>>          char *ptr_ch = *ptr;

>>          ptr_ch[i] = ch;

>don't you lose the pass-by-reference-ness then?

No, ptr (with type void **) is still pointing at the void * object in the
caller and *ptr is accessing that object.

Quote:
> the original source isn't
>fresh in my mind (in fact it's hardly in my mind at all), but if i remember
>right that's the whole reason i changed the void * to void **.  mind you i
>guess you could just reset it at the end, so it doesn't matter terribly :)

The code hear isn't modifying the pointer, it is modifying the allocated
character array, and that will still be accessible in the caller through
the void * pointer there.

--
-----------------------------------------


-----------------------------------------



Wed, 24 Jul 2002 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. value types assigned to reference type

2. Assign value to void *

3. assigning a value to void returning funcrions

4. assigning values to an array

5. assigning an array to values.

6. Int86: Error in C ANSI: (DOS.H : Value of type void is not allowed)

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

8. Array to value-types

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

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

11. Assigning Void Pointer, Thanks for the Help!

12. complicated void-malloc-struct-assign combination

 

 
Powered by phpBB® Forum Software