2

I was writing some low level c code on fedora 14, but going nuts over this piece of code. The first array is not initialized to '0', the second is. Gone through gdb several times, but it's like magic. What is happening?

const int maxsize=100000;
char num[7];
char tnum[6];
int pos=0;
while(pos<(maxsize+1)) {
  int c=0;
  int j=0;

  int myindex;
  int tindex;
  for(myindex=0;myindex<7;myindex++) num[myindex]='0';
  for(tindex=0;tindex<6;tindex++) tnum[tindex]='0';
//....
}

I printed the array values inside gdb, both as p num , p tnum and as p num[0] and p tnum[0]. I also tried to initialize as plain 0, same thing also happens.

Here is the debugger output

Temporary breakpoint 1, main () at inversionscount.c:3
3   int main() {
Missing separate debuginfos, use: debuginfo-install glibc-2.13-1.i686
(gdb) s
5   const int maxsize=100000;
(gdb) s
6   int startarray[maxsize];
(gdb) s
14  int pos=0;
(gdb) s
15  while(pos<(maxsize+1)) {
(gdb) s
19      int c=0;
(gdb) s
20      int j=0;
(gdb) s
24      for(myindex=0;myindex<7;myindex++) num[myindex]='0';
(gdb) s
25      for(tindex=0;tindex<6;tindex++) tnum[tindex]='0';
(gdb) s
27      while( c=getchar()!="\n") {
(gdb) p num
$1 = "\370\377\277\270\367\377"
(gdb) p tnum
$2 = "000000"
(gdb) 
5
  • What is the value of maxsize? Commented Jun 18, 2012 at 18:15
  • why is there an un-closed while loop here? Commented Jun 18, 2012 at 18:15
  • What makes you think they aren't initialized correctly? It looks correct to me. Commented Jun 18, 2012 at 18:18
  • @skjaidev bzero is not a Standard C function and is deprecated in POSIX Commented Jun 18, 2012 at 18:22
  • OOOoh POSIX police! My comment stands. Use memset then! Commented Jun 18, 2012 at 19:59

3 Answers 3

1

What is maxsize? Make sure the code follows the execution path you think it does, by single-stepping through with a debugger.

Also, you shouldn't repeat magical constants, the for loops are better written as:

size_t i;

for(i = 0; i < sizeof num; ++i)
  num[i] = '0';
for(i = 0; i < sizeof tnum; ++i)
  tnum[i] = '0';

or, since these are char arrays, just use memset():

#include <string.h>

memset(num, '0', sizeof num);
memset(tnum, '0', sizeof tnum);
Sign up to request clarification or add additional context in comments.

3 Comments

using sizeof as the exit condition of the for loop will cause the computation of the size each time. Guess it doesn't matter too much for so small arrays...
maxsize is const int with value 100000
@Saphrosit: No. In all but the most convoluted cases, sizeof is evaluated at compile-time.
0

How do you check you array are initialized with the '0' character literal? If you print them as strings, remember string are null terminated and your arrays are not.

Add this:

 num[sizeof num - 1] = '\0';
 tnum[sizeof tnum - 1] = '\0';

before printing them:

printf("%s\n", num);
printf("%s\n", tnum);

Also notice '\0' is the int value 0 while '0' is the printable character 0.

3 Comments

Yes your solution works. Print f prints both as (gdb) s 29 printf("%s\n", num); (gdb) s 000000 30 printf("%s\n", tnum); (gdb) s 00000 Was that a string terminating issue?Thank you.
@user1464460 By definition a string in C is a null terminated array of characters. The null character '\0' marks the end of the string.
Initally the problem happened when i gave input 0, mistakenly taking it for int, while was char. C needs much attention at these things :-)
0

You should use memset for both of your arrays:

#include <string.h>
char num[7];
memset(num, '\0', sizeof(num));

2 Comments

This probably isn't the solution he is looking for.
Why? This is the standard idiom to initialize arrays in c.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.