Author Topic: C-code; array of struct ? (Atmel Studio 6)  (Read 47673 times)

0 Members and 1 Guest are viewing this topic.

Offline David_AVD

  • Super Contributor
  • ***
  • Posts: 2607
  • Country: au
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #125 on: August 14, 2013, 01:56:32 am »
Remember you're talking to a C n00b here.

I am putting those lines in the same global.h file as the structure declaration.  Right or wrong?
 

Offline David_AVD

  • Super Contributor
  • ***
  • Posts: 2607
  • Country: au
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #126 on: August 14, 2013, 01:58:59 am »
Putting them in main:

Code: [Select]
statement with no effect [-Wunused-value]
Ian's commands do compile, but where is the output supposed to show up?
« Last Edit: August 14, 2013, 02:00:31 am by David_AVD »
 

Offline c4757p

  • Super Contributor
  • ***
  • Posts: 7805
  • Country: us
  • adieu
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #127 on: August 14, 2013, 02:00:41 am »
I expected the error as it needs to be assigned to something?

It's perfectly valid C to mention a value without using it.

Code: [Select]
2;

That's a perfectly valid statement. "Hey compiler, the number 2 exists!"

Useless, of course. Either dump it out to the LCD or over serial, or use it as the initializer for a global variable and then use a tool like objdump to read it. I'm not aware of a way to make the compiler output it.
No longer active here - try the IRC channel if you just can't be without me :)
 

Offline c4757p

  • Super Contributor
  • ***
  • Posts: 7805
  • Country: us
  • adieu
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #128 on: August 14, 2013, 02:01:18 am »
Ian's commands do compile, but where is the output supposed to show up?

sizeof just returns a number, it's up to you how you look at it. Dump it to the LCD?

Putting them in main:

Ahhhhh.... I see what you were doing! I should have been clearer about what they do... :)
« Last Edit: August 14, 2013, 02:08:04 am by c4757p »
No longer active here - try the IRC channel if you just can't be without me :)
 

Offline David_AVD

  • Super Contributor
  • ***
  • Posts: 2607
  • Country: au
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #129 on: August 14, 2013, 02:09:22 am »
Ah, there you go.  Assume nothing my friends!  LOL

OK, a quick bodge outputs 17 to the screen for the size of the structure without any caption field which is correct.

Adding a string field (array of 6 char) changes it to 23 as expected.

The error only seems to occur when I use strcpy and the structure is over 20 bytes?
 

Offline David_AVD

  • Super Contributor
  • ***
  • Posts: 2607
  • Country: au
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #130 on: August 14, 2013, 02:11:28 am »
I did try a separate array for the captions:

Code: [Select]
char Captions[32][6];
I don't get the "spill errror" until I try to use strcpy as well.
 

Offline c4757p

  • Super Contributor
  • ***
  • Posts: 7805
  • Country: us
  • adieu
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #131 on: August 14, 2013, 02:12:44 am »
How exactly are you calling strcpy, and how exactly are the copy destinations defined?

Remember that each character array must have one extra spot to hold the string terminator, and if you overstep the array length you are screwed (and strcpy couldn't care less).

Also note that char Captions[32][6] is thirty-two strings of length six, not six strings of length thirty-two. Not sure if that's intentional - I don't think it is. That may be your problem :-+ or not... Lengths of six doesn't seem too useful to me, but you did say "about 30 buttons"...
« Last Edit: August 14, 2013, 02:17:21 am by c4757p »
No longer active here - try the IRC channel if you just can't be without me :)
 

Offline IanB

  • Super Contributor
  • ***
  • Posts: 9693
  • Country: us
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #132 on: August 14, 2013, 02:30:33 am »
Also note that char Captions[32][6] is thirty-two strings of length six

Remembering the null terminator, it is actually 32 strings of length 5--trap for the unwary...
I'm not an EE--what am I doing here?
 

Offline David_AVD

  • Super Contributor
  • ***
  • Posts: 2607
  • Country: au
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #133 on: August 14, 2013, 02:33:55 am »
I have 32 buttons, each which fit strings of up to 5 characters.  The longest strings I'm using are 5 characters.

In the interim, I changed the Draw_Button routine to take the button number as a parameter, instead of the address of the structure.  This in conjunction with the separate caption array has fixed the original "wrong text" issue.  The "spill" error is also absent now.
 

Offline c4757p

  • Super Contributor
  • ***
  • Posts: 7805
  • Country: us
  • adieu
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #134 on: August 14, 2013, 02:38:02 am »
I'm out of suggestions for now! :P

One thing you can do when you have weird memory-related errors, though it's a bit much for now: if you can pull out the relevant sections into something that can compile for PC, run it under Valgrind's Memcheck tool. It's surprisingly good at finding memory oopses. Linux-only IIRC, as it uses some pretty advanced OS features.
No longer active here - try the IRC channel if you just can't be without me :)
 

Offline David_AVD

  • Super Contributor
  • ***
  • Posts: 2607
  • Country: au
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #135 on: August 14, 2013, 02:51:25 am »
Everything is currently behaving as it should, so I guess we'll call this issue sorted for now.   :phew:

Having the captions in another array is not a big deal, but it would be nice to find the real cause behind that compiler error message.

It *may* have been to do with the Draw_Button procedure (with the struct as the parameter) as that seem to solve it all.   :-//
 

Offline IanB

  • Super Contributor
  • ***
  • Posts: 9693
  • Country: us
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #136 on: August 14, 2013, 03:20:54 am »
Well a microcontroller is a tiny computer, so you need to be sympathetic to the limitations of the architecture. You can't necessarily copy big objects around the same as you can on a big computer.
I'm not an EE--what am I doing here?
 

Offline David_AVD

  • Super Contributor
  • ***
  • Posts: 2607
  • Country: au
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #137 on: August 14, 2013, 04:25:16 am »
I want to thank you guys and say I appreciate your persistence with helping me with this.   :-+

I did learn a lot out of it and hope some of the onlookers did as well.
 

Offline mrflibble

  • Super Contributor
  • ***
  • Posts: 2018
  • Country: nl
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #138 on: August 14, 2013, 10:59:45 am »
Did you manage to find out what the root cause was? I notice you said that mucking about with Draw_Button helped... Since you mention that it seems to be strcpy related, either a buffer overflow as suggested or maybe a stack overflow (because the amount of sram on that atmel might not be all that much)? Sometimes looking at the linker output is helpful, since you can see which bits go where. Might give you that a-hah moment.
 

Offline David_AVD

  • Super Contributor
  • ***
  • Posts: 2607
  • Country: au
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #139 on: August 14, 2013, 10:15:13 pm »
I know very little about the linker, etc.  I just write the code and hit F5 to program!   ;D

I would like to try adding the caption field back into the structure soon to see if it keels over again.

RAM shouldn't be an issue.  This chip has 8K of it.   :)
 

Offline Kremmen

  • Super Contributor
  • ***
  • Posts: 1283
  • Country: fi
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #140 on: August 15, 2013, 06:50:52 am »
In one message you noted that you are C n00b. Is C++ totally out of the question, do you think? Because reading this thread is like listening to someone drawing their nails across a blackboard. An object (sic) like a button just begs - no, screams - to be implemented in the OO paradigm. I may have stated that already. Implementing it would be an absolute breeze, at the same time cleaning the code which has the risk of becoming a bit messy soon.
Maybe something like this (just something so you get the idea):


class button {

private:
  uint8_t x,y,w,h;    //coordinates and dimensions
  bool visible;
  //other stuff you need
  char caption[11]; 
  show();
  hide();

public:
  button();
  button(uint8_t xpos, ypos, width, height, char *cap); // either this or the next one, depends how dynamic you want (dare) to be
  init(uint8_t xpos, ypos, width, height, char *cap);      // you want this for statically constructed buttons
  move(uint8_t dx, dy);
  moveto(uint8_t nx, ny);
  rename(char *newcap);
}

button::button() {  // not really needed because this happens anyway
  x=y=h=w=0;
  visible = false;
  caption[0] = 0;
}

button::button(uint8_t xpos, ypos, width, height, char *cap) {
  x=xpos;
  y=ypos;
  w=width;
  h=height;
  visible = false;
  strlcpy(&caption, cap, sizeof(caption));
  show();
}

button::init(uint8_t xpos, ypos, width, height, char *cap) {
  x=xpos;
  y=ypos;
  w=width;
  h=height;
  visible = false;
  strlcpy(&caption, cap, sizeof(caption));
  show();
}
button::show() {
  // do what needs to be done to draw the button
  visible = true;
}

button::hide{
  // overwrite the button with the background
  visible = false;

button::move(uint8_t dx, dy) {
  hide();
  x+=dx;
  y+=dy;
  show();
}

button::moveto(uint8_t dx, dy) {
  hide();
  x=nx;
  y=ny;
  show();
}

button::rename(char *newcap) {
  hide();
  strlcpy(&caption, newcap, sizeof(caption));
  show();
}

- - - - - - -
#define NUM_BUTTONS 32;
button my_buttons[NUM_BUTTONS];
...

for (i=0; i < NUM_BUTTONS; i++) {
  my_buttons.init(some_x, some_y, some_h, some_w, some_cap_p);
}

my_buttons[random_index].rename("newcaption");

//done

Nothing sings like a kilovolt.
Dr W. Bishop
 

Offline David_AVD

  • Super Contributor
  • ***
  • Posts: 2607
  • Country: au
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #141 on: August 15, 2013, 09:42:25 am »
I been using Delphi for PC programming for many years so know the OO concept well.

The reason I stayed with C for Atmel was that I'm also coding other projects in XC8 for PIC processors.  All my previous PIC programming (100's of projects) has been in assembler.

Maybe my reasoning is flawed, but I'd like to learn a common HLL for both types for now.  Time will tell and there's no reason I can't use C++ in the future once I get better at C.
 

Offline Kremmen

  • Super Contributor
  • ***
  • Posts: 1283
  • Country: fi
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #142 on: August 15, 2013, 09:57:05 am »
OK, you pays your money and you takes your choice, as they say. For me C and C++ are essentially the same language - you can easily write C using C++ if you really want to. There are a few things to look out for, but those are easily handled.
Nothing sings like a kilovolt.
Dr W. Bishop
 

Offline David_AVD

  • Super Contributor
  • ***
  • Posts: 2607
  • Country: au
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #143 on: August 23, 2013, 01:37:12 am »
A quick update; I moved the caption arrays back into the (array of) structure and it now compiles without error.  So it looks like that internal error I was getting was caused by trying to pass the structure as a parameter.  Changing to only passing the index (to the array) was the real fix.  Anyway, I hope that helps someone else.   :)
 

Offline David_AVD

  • Super Contributor
  • ***
  • Posts: 2607
  • Country: au
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #144 on: September 13, 2013, 01:34:57 am »
Back on the code for this project.  Now I'm wanting to find the size of a structure.

I'm trying SizeOf(structure_name) but getting an error that it's an undefined function.  What unit does it live in?
 

Offline c4757p

  • Super Contributor
  • ***
  • Posts: 7805
  • Country: us
  • adieu
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #145 on: September 13, 2013, 01:42:44 am »
It's just sizeof, all lowercase.
No longer active here - try the IRC channel if you just can't be without me :)
 

Offline David_AVD

  • Super Contributor
  • ***
  • Posts: 2607
  • Country: au
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #146 on: September 13, 2013, 02:08:19 am »
Thanks for that.  Gawd I hate case sensitivity some days!   |O

 

Offline c4757p

  • Super Contributor
  • ***
  • Posts: 7805
  • Country: us
  • adieu
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #147 on: September 13, 2013, 02:15:17 am »
Rule of thumb: if it's C, it's lowercase.
No longer active here - try the IRC channel if you just can't be without me :)
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8229
  • Country: 00
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #148 on: September 13, 2013, 10:26:25 am »
Quote
pass the structure as a parameter.

Pass it as a pointer to a structure. And use typedef.
================================
https://dannyelectronics.wordpress.com/
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8229
  • Country: 00
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #149 on: September 13, 2013, 11:17:43 am »
Quote
The 'const' keyword is an instruction for the compiler to treat the declaration as constant in the sense that its value is not supposed to change, but it will _not_ cause the storage to be allocated from flash memory.

The behavior is compiler specific.

This is where I think embedded C took a wrong turn. "const" should really mean what it is: it's read only. If you want to move it to flash, use some other modifiers. Taking "const" to mean "flash" is simply wrong in my view.
================================
https://dannyelectronics.wordpress.com/
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf