2

I'm new to development in VxWorks (using 6.8) and I've run into a situation where I need to pass the address of a struct to a function. Is it possible to create a struct from the interpreter shell? I have a hard copy of a VxWorks programmers guide that is based on v5.5 and it explicitly says that structs cannot be formed/created from the interpreter shell. Has this changed in later versions of VxWorks and if so, how would I make a struct from the command line.

As a VxWorks newb, I was hoping to run the command

myStruct = {msg1 ="hi", msg2 = "hello" }

and then use the struct in another statement like

printf("%s\n", myStruct.msg1)

If a struct cannot be created in the interpreter like above, what options can I pursue in order to get the printf statement to print "hi" or "hello" by accessing a member within the struct?

1 Answer 1

3

No, as of 6.9 the vxWorks C shell does not support structs. See the C Interpreter Limitations section of the Wind River Workbench Host Shell User's Guide. The shell uses a symbol table that does not track the types of its symbols, and you cannot define types on the command line.

To get the same behavior, you could allocate memory for a struct and then fill it in by address. The size you allocate doesn't have to be exact, but your memory offsets do. Your example is easy because you have only 2 pointers, and your struct wouldn't have any padding. If you are on a 32-bit processor, then the size of a pointer is 4 bytes. So the first pointer is at offset 0, 1, 2, and 3, and then the 2nd pointer starts at offset 4.

E.g.,

-> mystruct = malloc(50);
New symbol "mystruct" added to kernel symbol table.
mystruct = 0x3a320a0: value = 63949520 = 0x3cfcad0
-> mystruct[0] = "hi";
0x3cfcad0: value = 61030776 = 0x3a364a0
-> mystruct[4] = "hello";
0x3cfcae0: value 61040928 = 0x3a36920 = ' '
-> printf("%s\n", mystruct[4]);
hello
value = 6 = 0x6

Things would get tricky as your struct got larger, and if there were padding. E.g., struct { char, int } could have a size of 6 or 8 depending on your processor's alignment requirements, so you'd have to be aware of that when trying to get the offset of the int. If it got too complicated, you could compile helper functions in your code that perhaps look like this pseudo code:

struct st { char a; int b; char *c; };
struct st *make_me_a_struct(char a, int b, char *c)
{
    struct st *newstruct = malloc(sizeof(st));
    newstruct->a = a;
    newstruct->b = b;
    newstruct->c = c;
    return newstruct;
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.