Behy Behy -4 years ago 62
C Question

wrong content during passing of the pointer

In the following project, the necessary parameters to initialize

test.c
are arranged in the structure
DataStructure
(Here I have only a pointer
U8* Buffer
). During
init
, the pointer
Buffer
is initialized (is pointed to an array) and the function
test_init
passes the
initData
to
test.c
. In
test.c
the content of the array is printed.

main.c

#include <stdio.h>
#include <stdlib.h>
#include "test.h"

void init(DataStructure *pSL);
void main(void);

void main(void) {
DataStructure initData[COUNT];
init(&initData[0]);
test_init(&initData[0]);

test_run();

}
void init(DataStructure *pSL)
{
U8 Buffer[8] = {0xF1 , 0xF2 , 0xF3 , 0xF4 , 0xF5 , 0xF6 , 0xF7 , 0xF8};

DataStructure SL;
U8* pC = &Buffer[0];
SL.Buffer = pC;

*pSL = SL;

}


where
test.c
and
test.h
are:

#ifndef TEST_H_
#define TEST_H_

#ifndef U8
typedef unsigned char U8;
#endif

typedef struct{
U8 *Buffer;
} DataStructure;

#define COUNT 1

void test_init (DataStructure *List);
void test_run ();
#endif /* TEST_H_ */


and

#include <stdio.h>
#include <stdlib.h>
#include "test.h"

static DataStructure *pD;

void test_init (DataStructure *_pD)
{
pD = _pD;
printf("\n test_init: ");
for(int i=0;i<8;i++)
printf("0x%0x, ",*(pD->Buffer+i));
}
void test_run (void)
{
printf("\n test_run: ");
for(int i=0;i<8;i++)
printf("0x%0x, ",*(pD->Buffer+i));
}


The output of this code is:

test_init: 0x0, 0x0, 0x0, 0x0, 0xf5, 0xf6, 0xf7, 0xf8,
test_run: 0x0, 0x0, 0x0, 0x0, 0xf5, 0xf6, 0xf7, 0xf8,


which is wrong. But if I print the elements of
SL.Buffer
within
init
function as well, i.e if I change the
init
function as follows:

void init(DataStructure *pSL)
{
U8 Buffer[8] = {0xF1 , 0xF2 , 0xF3 , 0xF4 , 0xF5 , 0xF6 , 0xF7 , 0xF8};

DataStructure SL;
U8* pC = &Buffer[0];
SL.Buffer = pC;

*pSL = SL;


printf("\n main: ");
for(int i=0;i<8;i++)
printf("0x%0x, ",*(SL.Buffer+i));

}


, the array is passed correctly:

main: 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
test_init: 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
test_run: 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,


Could anybody explain me whats wrong whit this code and why the problem is fixed just by printing the array in
init
function?

And is it the right way to initialize and pass a structure containing a pointer?

The code is a simple version of my project. Originaly the
DataStructure
contains more parameters and the parameter
COUNT
(size of
initData
is more than one.

Answer Source

The problem, as I see it is with the usage of Buffer array.

It is local to the init() function, yet you return the address of the first element of the array to be used outside the function, where the array does not exist anymore. Hence, the address is essentially invalid and any attempt to access that memory will cause undefined behavior.

In the second case, it works fine, as you're printing the array before it ceases it's lifetime, so you're getting expected result.

Solution: You need to allocate static storage to the Buffer array, or use memory allocator function so as, the lifetime of the memory supersede the function scope.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download