Question:
What is wrong with this C code?
David N
2012-12-27 19:31:35 UTC
This is a code snippet. The code compiles and runs, but I cannot show everything in this limited space.At any rate, the offset from the base address of dBase is computed incorrectly, i.e., when i is non-zero, the base address of "points" is not correct. I have analyzed the assembly code extensively & an incorrect size of "element" is used when multiplying by i to get the offset to the ith element. In the failing statement (below), am I referencing the ith member of dBase and the jth member of points correctly? If the reference is made correctly, can anyone offer some insightful comments. A bug in the compiler comes to mind but I've found that is not usually the case. A useful answer is not one that suggests a work-around.

An array of structures thus:

struct element {
char team[30];
int teamClass;
int teamIndex;
int opponentGameIndex[NO_GAMES];
int points[NO_GAMES];
int gameNumberIndex;
};
struct element dBase[NO_ELEMENTS];

This is the C code statement that fails:

dBase[i].points[j] = -1;
Four answers:
John H
2012-12-27 19:58:20 UTC
I'd be very surprised if you found a compiler bug.



For a compiler with 32-bit ints, I'd expect the points array to be offset from the beginning of the struct by 40 + (NO_GAMES * 4) bytes (two bytes of padding after team). sizeof(struct element) should be 44 + (8 * NO_ELEMENTS).
clueless_nerd
2012-12-27 19:53:31 UTC
It looks correct. Double check that i and j are > or = 0 and that i < NO_ELEMENTS and j < NO_GAMES. Don't forget your first index always starts at 0, not 1. Just for fun you could try *((dBase + i).points) + j) and see if the same problem occurs (remember that the compiler will replace a[n] with *(a+n). )



EDIT: I read the other response and I'm not sure i understand your question now. To clarify to both of you: while your assignment statement should certainly work you CANNOT assume that a structure's size is the sum of the size of its members OR that each member will start on a word boundary. THAT is machine and compiler dependent! For example:



struct x { int a; char b[2]; int c};



x's size could be 10 bytes, 12 bytes, or something else entirely! The compiler may or may not word align the members(if it does it will have a "hole" after b on a 32 bit machine.
pinkerman
2016-10-16 12:44:04 UTC
void WriteDay(char d[]); That line pronounces a function prototype. It we could the compiler recognize that there is a function talked approximately as WriteDay and what arguments could desire to be exceeded to it. It would not tell the compiler how the WriteDay function is carried out. you are able to desire to have the easily function someplace so as that the compiler is common with of what it rather is meant to do.
2012-12-27 21:29:07 UTC
Maybe i > NO_ELEMENTS or j > NO_GAMES.

You need handle it.

if (i > NO_ELEMENTS) return;

if (j > NO_GAMES) return;


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...