views:

113

answers:

5

Hi,

static int 
write_stream(const void *buffer, size_t size, void *app_key) 
{
    char *stream = (char *)app_key;

    return 0;
}

what am I doing wrong here? Why pointer casting does not result in a copying of (size_t size) bytes from buffer into the stream defined by app_key?

Thanks

am calling this function as argument of another function to use callback :

static int
write_stream(const void *buffer, size_t size, void *app_key) 
{
    char *stream = (char *)app_key; 
    return 0;
}

int
print_to_2buf(char *ostream, struct asn_TYPE_descriptor_s *td, void *struct_ptr)
{
    asn_enc_rval_t er; /* Encoder return value */

    // write_stream is called as argument
    er = xer_encode
    (
        td,
        struct_ptr,
        XER_F_BASIC,
        /* BASIC-XER or CANONICAL-XER */ write_stream,
        ostream
    );

    return (er.encoded == -1) ? -1 : 0; 
}
+1  A: 

Because a cast only reinterprets data, it doesn't (or should) do any copy.

Besides, how does the cast know it should do a copy of size bytes? You're assuming too much from a simple cast. Do the copy yourself.

(I assume you pasted the right code).

Matias Valdenegro
+4  A: 

All you've done is created a pointer to char that holds the same address as the pointer to void that you received. You haven't done anything to copy bytes from app_key to buffer, so nothing gets copied. Unless I'm misunderstanding the question, you should probably just use memcpy or memmove.

Jerry Coffin
+1  A: 

Why do you think it would? For one thing, char *stream = (char *)app_key doesn't mention size everywhere, so why would its behavior depend on the value of size? For another, you're just turning a pointer into another pointer: nothing in your code says that you want to dereference the pointer and copy stuff that it points to.

To copy size bytes from where buffer points to where app_key points, use the memcpy function (you need to #include <string.h>).

memcpy(app_key, buffer, size);

It's up to you to ensure that there is enough room in app_key, i.e., app_key must point to an allocated region of memory whose size is at least size. This could be a large enough array in the calling function, or memory allocated with malloc.

Note that casts should only be used when you know what you're doing. In particular, it's almost never necessary to cast a pointer to or from void*.

Gilles
A: 

When you use a pointer you have a reference to some type in memory. It's like putting your finger on a square on a game board. You can take another finger (the char pointer stream in this case) and put it on the same square. You now have two pointers to the same square. This doesn't make your fingers into game squares. You need to do some other work to make a copy of the thing being pointed to, like a memcopy.

Paul Rubel
A: 

From your question, I can't be certain your level of understanding, but it looks as though you are not familiar with the mechanics of pointers, so I will try to give a quick explanation.

Your parameter void *app_key is an integer that stores the location in memory of a block of unknown (void) type.

Your char *stream is also an integer, it stores the location in memory of a block of char type (either a single char or the first char in a continuous block).

Your line of code char *stream = (char *)app_key copies the integer value of app_key (that is, the address in memory that app_key refers to), and stores it also in stream.

At this point, both app_key and stream store the same location in memory -- the address of the first character in your app_key (assuming that a string was passed as the parameter).

Realize, however, that the only thing that was copied was the integer memory address of your app_key. There is still only one copy.

What it sounds like you wanted to do was to make an entire new copy of the app_key string, one that was stored in a different location in memory (perhaps so that when the function returns and the original string is destroyed you still have your copy).

In order to do that, you need to do the following:

1: allocate memory space sufficient to hold the new copy
2: copy all of the characters from the original to the copy

There are many ways to do this depending on what your needs are. Here is one:


static int 
write_stream (const void *buffer, size_t size, void *app_key) 
{
    //create a memory buffer large enough to hold size_t size chars.
    char *stream = (char *)malloc(sizeof(char) * size);

    //copy size_t size chars from location at app_key to location at stream
    memcpy(stream, app_key, sizeof(char) * size);

    //rest of your function
}

Because you are working with strings, you could also make use of the strcpy() function (potentially paired with strlen). Those functions all assume that you are using a NULL-terminated string (that is, the string is a sequence of chars, with a final \0 at the end to indicate the end.

Andrew Shelansky
ok thanks for your help.
Said