Resizing a char[x] to char[y] at runtime

后端 未结 11 1257
我在风中等你
我在风中等你 2021-02-11 01:33

OK, I hope I explain this one correctly. I have a struct:

typedef struct _MyData
{
   char Data[256];
   int  Index;
} MyData;

Now, I run into

11条回答
  •  轮回少年
    2021-02-11 02:05

    I find KIV's trick quite usable. Though, I would suggest investigating the pointer issue first.

    If you look at the malloc implementations
    (check this IBM article, Listing 5: Pseudo-code for the main allocator),
    When you allocate, the memory manager allocates a control header and
    then free space following it based on your requested size.
    This is very much like saying,

    typedef struct _MyData
    {
       int  size;
       char Data[1]; // we are going to break the array-bound up-to size length
    } MyData;
    

    Now, your problem is,
    How do you pass such a (mis-sized?) structure to this other process?

    That brings us the the question,
    How does the other process figure out the size of this data?
    I would expect a length field as part of the communication.

    If you have all that, whats wrong with passing a pointer to the other process?
    Will the other process identify the difference between a pointer to a structure and that to a allocated memory?

提交回复
热议问题