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 ==============
[[email protected] programs]$ gcc -ansi -pedantic -Wall -Wextra test.c
[[email protected] programs]$ ./a.out
arg =
arg =
[[email protected] 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 =============
[[email protected] programs]$ gcc -ansi -pedantic -Wall -Wextra test.c
[[email protected] programs]$ ./a.out
arg is NULL
arg is NULL
[[email protected] 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 ?
 
Ad

Advertisements

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 =============
[[email protected] programs]$ gcc -ansi -pedantic -Wall -Wextra test.c
[[email protected] programs]$ ./a.out
arg is NULL
arg is NULL
[[email protected] 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 =============
[[email protected] programs]$ gcc -ansi -pedantic -Wall -Wextra test.c
[[email protected] 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 ==============
[[email protected] programs]$ gcc -ansi -pedantic -Wall -Wextra test.c
[[email protected] programs]$ ./a.out
arg =
arg =
[[email protected] 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...
 
Ad

Advertisements

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 =============
[[email protected] programs]$ gcc -ansi -pedantic -Wall -Wextra test.c
[[email protected] programs]$ ./a.out
arg is NULL
arg is NULL
[[email protected] 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.
 
Ad

Advertisements


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

K&R2, exercise 5-4, strend(s,t) 15
Const Issue 2
const problem 1
Parsing a string 44
Passing an array of struct to function 3
sequence points and printf() 17
time_t: initializer element is not constant 10
pointer to pointer 7

Top