Question about memory handling

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi.

Could anyone please explain the following to me?
I have struct mapping a command on a certain memory area, and it's declared like this:

unsigned char outbuf[COMMAND_DATA_MAX_LENGTH];
struct odm_command *out_command = (struct odm_command *)outbuf;

The odm_command struct's last element is the array command_data[]. Now, I wanted to use the sprintf() function, and have it return the string to command_data[1]...command_data[n]. I've tested using:

sprintf(out_command->command_data[1], "Test");

That doesn't work. But if I first create a new variable that points to the 2nd slot in the command_data[] array, that works! Like this:

unsigned char *test = &out_command->command_data[1];
sprintf(test, "Test");

I've checked the memory allocation in AVR Studio, and this seems to work fine. But what I don't get is the use of * and &.

When casting the odm_command struct on the allocated memory area, I have to use * on the right side of the =. But when creating a variable that points to a slot in an array inside the struct, I have to use &. Can someone please explain the difference to me?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This is pointer stuff, not memory allocation!

Maybe if you did this:

sprintf(&out_command->command_data[1], "Test");

sprintf wants a pointer ( that is a memory adddress) to the variable. Since you've done the array index, this derefences the pointer ( effectively adding the *) therefore you're referring to the variable itself, not it's memory address.

In a declaration or cast, the * is saying that the variable is a pointer.

So, get yourself a good C book and start reading, pointers are probably the trickiest to get to grips with.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Silly question but you do know that for an N element array the elements are [0] to [N-1] and not [1] to [N] don't you?

It's kind of odd to sprintf() to &[1] which is the SECOND byte of the string, not the first. In fact C then lets you use either:

sprintf(&out_command->command_data[0], "Test");

or perhaps easier:

sprintf(out_command->command_data, "Test");

BTW while stating the obvious I'll observe that "Test" is FIVE bytes not four - so always make sure 'command_data' is dimensioned to take the largest string PLUS ONE to accommodate its terminating NUL.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:
This is pointer stuff, not memory allocation!

Maybe if you did this:

sprintf(&out_command->command_data[1], "Test");

sprintf wants a pointer ( that is a memory adddress) to the variable. Since you've done the array index, this derefences the pointer ( effectively adding the *) therefore you're referring to the variable itself, not it's memory address.

In a declaration or cast, the * is saying that the variable is a pointer.

So, get yourself a good C book and start reading, pointers are probably the trickiest to get to grips with.


Hmm.. That's probably not a bad idea. Thanks for that.

Yeah, I've gotten way better on the pointer theory just during the last couple of weeks, but I still have a way to go. :oops: :)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
Silly question but you do know that for an N element array the elements are [0] to [N-1] and not [1] to [N] don't you?

Yes, very much so, which was the reason for the question to begin with. I needed the string to be inputted from the 2nd byte and up in the buffer. And for code readability, I wanted to have a variable point to the correct slot in the buffer. I was just curious as to what was the reason for the seemingly inconsistent usage of * and & in the two examples I came with.

clawson wrote:
BTW while stating the obvious I'll observe that "Test" is FIVE bytes not four - so always make sure 'command_data' is dimensioned to take the largest string PLUS ONE to accommodate its terminating NUL.

Hmm... Very good point that wasn't obvious to me. :) Thanks.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
I was just curious as to what was the reason for the seemingly inconsistent usage of * and & in the two examples I came with.

The only thing inconsistent was that you forgot the & in the first example.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The inconsistency I was talking about was when you have two cases where one case uses one method and the other case uses another method, which is why the & wasn't present in the first example. But no matter, the whole problem just boils down to my lack of knowledge, really. :)

I'm starting to get the hang of it now, though. Thanks for the help, guys (and gals).