1

My C has a behavior I do not understand.

I have defined an array as following

long gainT1[21];

I do some work with that variable (so, no need for an initialization), then, later, I would like to display the values that it contains. And here comes the problem: The first value showed by the second for is not the expected one!

printf("CHECKING: gainT1[0]=%ld\n", gainT1[0]);
{
    ptrdiff_t k;
    for(k = 0; k < 16; k++) printf("[%td]=%ld   ", k, gainT1[k]); printf("\n");
    for(k = 0; k < 17; k++) printf("[%td]=%ld   ", k, gainT1[k]); printf("\n"); // Here the for is going up to 16 instead of 15 previously
}

this code returns:

CHECKING: gainT1[0]=4207440

[0]=4207440 [1]=4207440 [2]=4294967295 [3]=139846275105333 [4]=16351504 [5]=0 [6]=139846268659528 [7]=139846277253568 [8]=16351504 [9]=3 [10]=128 [11]=139846277252304 [12]=4294967295 [13]=139846272645590 [14]=0 [15]=0

[0]=2356216002 [1]=4207440 [2]=4294967295 [3]=139846275105333 [4]=16351504 [5]=0 [6]=139846268659528 [7]=139846277253568 [8]=16351504 [9]=3 [10]=128 [11]=139846277252304 [12]=4294967295 [13]=139846272645590 [14]=0 [15]=0 [16]=6312008

I can't figure out what is wrong with my code.

simon.denel
  • 780
  • 6
  • 23
  • 2
    Please add the declaration and assignment of `gainT1`. – Zeta Dec 03 '12 at 09:20
  • See http://stackoverflow.com/questions/7954439/c-which-character-should-be-used-for-ptrdiff-t-in-printf – kmkaplan Dec 03 '12 at 09:31
  • ... and you're sure that your compiler doesn't give you a nasty warning, something like: warning: format '%d' expects type 'int', but argument 2 has type 'ptrdiff_t'? – carlosdc Dec 03 '12 at 09:37
  • 1
    you don't provide enough information, please add code so people won't have to guess. – iabdalkader Dec 03 '12 at 09:41
  • 2
    As I said in my previous comment, there is nothing wrong per-say with the code you show (see this: http://ideone.com/ixdekv)... – Nim Dec 03 '12 at 09:48
  • @Nim u forget typing return 0; that's why the link give the runtime error. – akp Dec 03 '12 at 09:56
  • @akp, I just wanted to show that the two loops could not be responsible for this problem... – Nim Dec 03 '12 at 10:03
  • Since you are not being particularly forthcoming, let's try another question, *where* is your array declared relative to the above printing code.. – Nim Dec 03 '12 at 10:06

3 Answers3

1

"I do some work with that variable" is rather undefined. If, in the for-loop, you're accessing an element that's actually still undefined, anything can happen, including odd values showing up.

So, easiest is simply to do:

long gainT1[21] = { 0 };

Any non-set value will then at least be zero.

(Note: I know this array initialization works for c99; not sure about eg c89 or c90.)

0

If you show the whole code then it will be easy to get the exact error but as you have provided less code so I think you should try this.

You can use printf() to display a ptrdiff_t. According to the 1999 C Standard, the format string should contain the t length modifier with the d or i conversion specifier, as in:

ptrdiff_t d;
...
printf("%td", d);

Although the %d specifier has been around as long as C, the %t modifier is fairly new. Few C libraries support it yet. If your compiler doesn't support %td, then you should try %ld, as in:

printf("%ld", (long)d);

Displaying a ptrdiff_t using the Standard C++ iostream library is a simple as displaying any other numeric type:

std::cout << d;
unwind
  • 391,730
  • 64
  • 469
  • 606
akp
  • 1,753
  • 3
  • 18
  • 26
  • Hello akp, although you are right, It does not solve the problem. [%td]=%ld gives the same result. – simon.denel Dec 03 '12 at 09:34
  • @sdenel can u show the actual whole code??? – akp Dec 03 '12 at 09:35
  • 2
    Is there a specific reason why **(yo)u use only bold text**? Also, this question is cleary tagged C, not C++. Don't provide solutions from other languages. – Zeta Dec 03 '12 at 09:39
  • @Zeta its only for focusing something important.. – akp Dec 03 '12 at 09:41
  • @akp: I am not reluctant to give you the entire, quite long, code between the initialization and the displaying but I do not think it will give you more informations, as nothing is done between the two loops, and gainT1 is linked to no peripheral. – simon.denel Dec 03 '12 at 09:46
  • @sdenel have u compiled it many times & checked it again and again ...& in every case are u getting same result(i mean the first value in the array becomes different)..? – akp Dec 03 '12 at 09:50
  • @akp: I do, and you are right saying I did not give you enough informations. Please give me 15 minutes as I am preparing you a short, not working code. – simon.denel Dec 03 '12 at 10:04
0

As you helped me understanding that the problem must be somewhere else, I created a new project deleting as much lines as possible and it appears that it is an initialization problem: as the array is not initialized properly, the value is not stable. The problem appears or not depending of compiling options, so let's keep in mind to always initialize variables properly even when it seems useless.

simon.denel
  • 780
  • 6
  • 23