0

I'm stocking in an implementation of a global array of structs. Now the Problem may or may not to be that this "test" only allocating ONE Element of these Array. So i Hope someone knows what I'm doing wrong. My global Target is to implement a dynamic array with dynamic size using free() and realloc

kind regards

typedef struct IntegerBuffer {
unsigned DataSize;
}IntegerBuffer;

void PrintDummy();
void AddBufferSize(size_t Size);

IntegerBuffer *SendIntBuffer = NULL;

int main()
{
    AddBufferSize(3);
    PrintDummy();
}

void AddBufferSize(size_t Size)
{
    unsigned i;
    SendIntBuffer = (IntegerBuffer*)malloc(Size*sizeof(IntegerBuffer));

    for (i = 0;  i < (unsigned)Size; i++)
    {
        SendIntBuffer[i].DataSize = i;
    }
    return;
}
void PrintDummy()
{
    unsigned size = (sizeof(SendIntBuffer) / sizeof(SendIntBuffer[0]));
    for (unsigned i = 0; i < size; i++)
    {
        printf("\nTestprint %i", SendIntBuffer[i].DataSize);
    }
    return;
}
JNgoon
  • 13
  • 3
  • 1
    What problem may or may not be what? This is very unclear. You should probably store the allocated size in the `IntegerBuffer`, makes it far easier to `realloc()` when needed. – unwind May 23 '17 at 11:33
  • Having a structure with a single member is somewhat pointless. – Jabberwocky May 23 '17 at 11:33
  • And `sizeof(SendIntBuffer)` does not what you think. You cannot get the size of a piece of allocated memory with `sizeof`. – Jabberwocky May 23 '17 at 11:35
  • This is just a test struct there will be more elements in later @MichaelWalz – JNgoon May 23 '17 at 12:01
  • so the better way is to store the size (Number of elements of these array) in every element of the array ? or do i have to Use a struct in a struct where the outer contains the Size of the inner struct array ? – JNgoon May 23 '17 at 12:06

0 Answers0