Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

initializing Byte array with malloc is problematic?

Tags:

arrays

c

I have a struct type, MESGB with details:

typedef unsigned char Byte; 
typedef struct MESGB 
{ 
unsigned int soh; 
unsigned int stx; 
unsigned int etx; 
Byte checksum; 
Byte msgno; 
Byte *data; 
} MESGB; 

and I have a variable named frameBuf, which is array of MESGB:

#define buffSize 6
#define maxElmt 3
static MESGB frameBuf[buffSize]

when I want to Initialize frameBuf with this function:

int k;
for(k = 0; k<=buffSize-1; k++)
{
    frameBuf[k].soh = SOH;
    frameBuf[k].stx = STX;
    frameBuf[k].etx = ETX;
    frameBuf[k].checksum = 0;
    frameBuf[k].msgno = -1;
    frameBuf[k].data = (Byte*) malloc (maxElmt*sizeof(Byte));               
}

what I found strange is that, if I write a value to frameBuf data like this:

frameBuf[0].data[0] = 'a'
frameBuf[0].data[1] = 'b'    
frameBuf[0].data[2] = 'c'
frameBuf[1].data[0] = 'd'
frameBuf[1].data[1] = 'e'    
frameBuf[1].data[2] = 'f'
frameBuf[2].data[0] = 'g'
frameBuf[2].data[1] = 'h'    
frameBuf[2].data[2] = 'i'

when I try to print all of frameBuf data, I get the output like this:

frameBuf[0].data[0] = 'g'
frameBuf[0].data[1] = 'b'    
frameBuf[0].data[2] = 'c'
frameBuf[1].data[0] = 'g'
frameBuf[1].data[1] = 'e'    
frameBuf[1].data[2] = 'f'
frameBuf[2].data[0] = 'g'
frameBuf[2].data[1] = 'h'    
frameBuf[2].data[2] = 'i'

and I came to conclusion that if I change the value of frameBuf[i].data[0], all of other frameBuf[j].data[0] will change too, which I found disturbing. Even though if I change the data in data[k] where k is other integer in 0, it will not change any other data[k] in other frameBuf[]

Can somebody explain to me how to fix this problem? Thanks in advance! :D

[Edited]: you can see the code in: http://ideone.com/6BRsU
I initialize frameBuf in initFrameBuf, assign data to frameBuf in transMessage, and the strange output is in retryTransWindow

[Edited]: Solved. Okay, the problem is not in array initializing. All of it is my clumsiness in coding. Thanks for all of your's help! ^^

like image 300
gagal_axzel Avatar asked Mar 28 '26 23:03

gagal_axzel


1 Answers

I suspect maxElmt is not what you think it is.

I get this output:

frameBuf[0].data[0]='a'
frameBuf[0].data[1]='b'
frameBuf[0].data[2]='c'

frameBuf[1].data[0]='d'
frameBuf[1].data[1]='e'
frameBuf[1].data[2]='f'

frameBuf[2].data[0]='g'
frameBuf[2].data[1]='h'
frameBuf[2].data[2]='i'

frameBuf[3].data[0]='j'
frameBuf[3].data[1]='k'
frameBuf[3].data[2]='l'

frameBuf[4].data[0]='m'
frameBuf[4].data[1]='n'
frameBuf[4].data[2]='o'

frameBuf[5].data[0]='p'
frameBuf[5].data[1]='q'
frameBuf[5].data[2]='r'

...using this code:

typedef unsigned char Byte;

typedef struct MESGB_T
{
    unsigned int soh;
    unsigned int stx;
    unsigned int etx;
    Byte checksum;
    Byte msgno;
    Byte *data;
} MESGB;

#define buffSize 6
#define maxElmt 3

static MESGB frameBuf[buffSize];

Byte curChar = 97;

void Init()
{
    int k, i;
    for(k = 0; k<=buffSize-1; k++)
    {
        frameBuf[k].soh = 23; // SOH;
        frameBuf[k].stx = 24; // STX;
        frameBuf[k].etx = 25; //ETX;
        frameBuf[k].checksum = 0;
        frameBuf[k].msgno = -1;
        frameBuf[k].data = (Byte*) malloc (maxElmt*sizeof(Byte));
        for (i=0; i < maxElmt; i++)
        {
            frameBuf[k].data[i]=curChar++;
        }
    }
}

void Print()
{
    int k, i;
    for(k = 0; k<=buffSize-1; k++)
    {
        for (i=0; i < maxElmt; i++)
        {
            printf("frameBuf[%d].data[%d]='%c'\n",
                   k,i, frameBuf[k].data[i]);
        }
        printf("\n");
    }
}

int main(int argc, char **argv)
{
    Init();
    Print();
}
like image 125
Cheeso Avatar answered Apr 01 '26 09:04

Cheeso



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!