Difference between an initialized and uninitialized Array

A

arnuld

#include <stdio.h>


void check_arg(char* p);

int main(void)
{
char arr1[25];
char arr2[25] = {0};

check_arg(arr1);
check_arg(arr2);

return 0;
}


void check_arg(char* p)
{
if( p ) printf("arg = %s\n", p);
else printf("arg is NULL\n");
}

============= OUTPUT ==============
[arnuld@dune programs]$ gcc -ansi -pedantic -Wall -Wextra test.c
[arnuld@dune programs]$ ./a.out
arg =
arg =
[arnuld@dune programs]$



First array was not initialized, hence it contained garbage. 2nd array
was initialized with NULs but both of them are same when check_array()
checks them. See the 2nd version of the same program:



#include <stdio.h>


void check_arg(char* p);

int main(void)
{
char arr1[25];
char arr2[25] = {0};

check_arg(arr1);
check_arg(arr2);

return 0;
}


void check_arg(char* p)
{
if( p ) printf("arg = %s\n", p);
else printf("arg is NULL\n");
}

================== OUTPUT =============
[arnuld@dune programs]$ gcc -ansi -pedantic -Wall -Wextra test.c
[arnuld@dune programs]$ ./a.out
arg is NULL
arg is NULL
[arnuld@dune programs]$


The only difference between these 2 programs is the if condition of
check_array(), which checks for a pointer in 2nd case. *p accesses the
first element which is NUL in 2nd array (but not in first because
first was uninitialized, hence contains contains garbage ) but still
it says both are NUL. Why so ?
 
A

arnuld

2nd version (corrected)

#include <stdio.h>

void check_arg(char* p);

int main(void)
{
char arr1[25];
char arr2[25] = {0};

check_arg(arr1);
check_arg(arr2);

return 0;

}

void check_arg(char* p)
{
if( *p ) printf("arg = %s\n", p);
else printf("arg is NULL\n");

}

================== OUTPUT =============
[arnuld@dune programs]$ gcc -ansi -pedantic -Wall -Wextra test.c
[arnuld@dune programs]$ ./a.out
arg is NULL
arg is NULL
[arnuld@dune programs]$
 
E

Eric Sosman

arnuld said:
[... print two char[] with "%s" ...]

First array was not initialized, hence it contained garbage. 2nd array
was initialized with NULs but both of them are same when check_array()
checks them.

The garbage (more formally, the "indeterminate values")
in the first array may have contained a '\0' in the first
position, or perhaps after a few white-space characters that
were hard to notice in the output. That's the thing about
garbage: You never know what's rotting in it. Also, the reek
of today may be different from the stench of tomorrow.
See the 2nd version of the same program:

Same as the first, as far as I can see.
 
B

Ben Bacarisse

arnuld said:
2nd version (corrected)

#include <stdio.h>

void check_arg(char* p);

int main(void)
{
char arr1[25];
char arr2[25] = {0};

check_arg(arr1);
check_arg(arr2);

return 0;

}

void check_arg(char* p)
{
if( *p ) printf("arg = %s\n", p);
else printf("arg is NULL\n");

}

================== OUTPUT =============
[arnuld@dune programs]$ gcc -ansi -pedantic -Wall -Wextra test.c
[arnuld@dune programs]$ ./a.out
arg is NULL
arg is NULL

The message is not strictly accurate because NULL refers to a pointer
whereas the thing you are testing (*p) is a character but the output
is consistent with what one should expect. Were you surprised? Did
you have a question?
 
M

Mark Bluemel

#include <stdio.h>

void check_arg(char* p);

int main(void)
{
  char arr1[25];
  char arr2[25] = {0};

  check_arg(arr1);
  check_arg(arr2);

  return 0;

}

void check_arg(char* p)
{
  if( p )     printf("arg = %s\n", p);
  else        printf("arg is NULL\n");

}

============= OUTPUT ==============
[arnuld@dune programs]$ gcc -ansi -pedantic -Wall -Wextra test.c
[arnuld@dune programs]$ ./a.out
arg =
arg =
[arnuld@dune programs]$

First array was not initialized, hence it contained garbage.

Not a good way to characterise the situation. The first array was not
initialized, hence you have no way of knowing what it contained.

Some platforms may choose, for example for security purposes, to fill
this with zeroes, even though it's not static and wasn't explicitly
initialized.

Either way, it's not relevant to this example, as your check_arg
function is only checking that you've passed it a non-NULL pointer,
which in both cases you have.

In your (corrected, after a fashion) second example :-
void check_arg(char* p)
{
if( *p ) printf("arg = %s\n", p);
else printf("arg is NULL\n");

}

You check to see whether the first character pointed to is non-zero.
There is no guarantee that this will be the case for your so-called
"garbage".

You cannot, in general, test for memory being uninitialized...
 
B

Barry Schwarz

2nd version (corrected)

#include <stdio.h>

void check_arg(char* p);

int main(void)
{
char arr1[25];
char arr2[25] = {0};

check_arg(arr1);
check_arg(arr2);

return 0;

}

void check_arg(char* p)
{
if( *p ) printf("arg = %s\n", p);
else printf("arg is NULL\n");

}

================== OUTPUT =============
[arnuld@dune programs]$ gcc -ansi -pedantic -Wall -Wextra test.c
[arnuld@dune programs]$ ./a.out
arg is NULL
arg is NULL
[arnuld@dune programs]$

Some compilers will initialize automatic arrays, especially in debug
mode. It is also possible that yours doesn't but the memory occupied
by arr1 just happened to contain zeros.

The standard says that the value of an uninitialized automatic object
is indeterminate and evaluating such a value invokes undefined
behavior.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top