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

后端 未结 11 1120
我在风中等你
我在风中等你 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 01:55

    Let me sum up two important points I see in this thread:

    1. The structure is used to interact between two programs through some IPC mechanism
    2. The destination program cannot be changed

    You cannot therefore change that structure in any way, because the destination program is stuck trying to read it as currently defined. I'm afraid you are stuck.

    You can try to find ways to get the equivalent behavior, or find some evil hack to force the destination program to read a new structure (e.g., modifying the binary offsets in the executable). That's all pretty application specific so I can't give much better guidance than that.

    You might consider writing a third program to act as an interface between the two. It can take the "long" messages and do something with them, and pass the "short" messages onward to the old program. You can inject that in between the IPC mechanisms fairly easily.

    0 讨论(0)
  • 2021-02-11 01:57

    You may be able to do this like this, without allocating a pointer for the array:

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

    Later, you allocate like this:

    int bcount = 256;
    MyData *foo;
    
    foo = (MyData *)malloc(sizeof(*foo) + bcount);
    

    realloc:

    int newbcount = 512;
    MyData *resized_foo;
    
    resized_foo = realloc((void *)foo, sizeof(*foo) + newbcount);
    
    0 讨论(0)
  • 2021-02-11 02:02

    It looks like from what you're saying that you definitely have to keep MyData as a static block of data. In which case I think the only option open to you is to somehow (optionally) chain these data structures together in a way that can be re-assembled be the other process.

    You'd need and additional member in MyData, eg.

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

    Where Sequence identifies the descending sequence in which to re-assemble the data (a sequence number of zero would indicate the final data buffer).

    0 讨论(0)
  • 2021-02-11 02:04

    The problem here is your statement "I can't use a pointer". You will have to, and it will make everything much easier. Hey, realloc even copies your existing data, what do you want more?

    So why do you think you can't use a pointer? Better try to fix that.

    0 讨论(0)
  • 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?

    0 讨论(0)
提交回复
热议问题