Jump to content
teejaydub

Structure Members Passed Incorrectly By Reference

Recommended Posts

This code fails with SourceBoost 6.84, BoostC 6.84, BoostLink 6.84, target PIC16F688.

 

#include <system.h>


typedef struct {
char a;
char b;
} msg_t;

msg_t msg;


void setTo12(char& c)
{
c = 12;
}


// Two different ways of defining an inline function:

// This one works, but bypasses type checking.
#define GetValueMacro(c)  setTo12(c)

// This one includes type checking, but produces incorrect code.
inline void GetValueInline(char& c)  { setTo12(c); }


main()
{
while (1) {
	msg.a = 100;
	msg.b = 0;

	// This works.
	//GetValueMacro(msg.b);
	// Result:
	// msg.a = 100
	// msg.b = 12

	// This fails - passes the address of a instead of b.
	GetValueInline(msg.b);
	// Result:
	// msg.a = 12
	// msg.b = 0
}
}

Share this post


Link to post
Share on other sites
This code fails with SourceBoost 6.84, BoostC 6.84, BoostLink 6.84, target PIC16F688.

 

#include <system.h>


typedef struct {
char a;
char b;
} msg_t;

msg_t msg;


void setTo12(char& c)
{
c = 12;
}


// Two different ways of defining an inline function:

// This one works, but bypasses type checking.
#define GetValueMacro(c)  setTo12(c)

// This one includes type checking, but produces incorrect code.
inline void GetValueInline(char& c)  { setTo12(c); }


main()
{
while (1) {
	msg.a = 100;
	msg.b = 0;

	// This works.
	//GetValueMacro(msg.b);
	// Result:
	// msg.a = 100
	// msg.b = 12

	// This fails - passes the address of a instead of b.
	GetValueInline(msg.b);
	// Result:
	// msg.a = 12
	// msg.b = 0
}
}

 

Works fine if you remove the inline from "inline void GetValueInline(char& c) { setTo12©; }" so the problem is passing a reference to a structure to an inline function.

 

Note that neither the address of a or the address of b are passed in any case. Call by reference is implemented by copying the variable to the function parameter, then copying the function parameter back to the variable when the function returns. Indeed, in the 'inline' case, it is msg.a that is copied/restored rather than msg.b.

 

Orin.

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...