1

Declaring simple struct:

struct s {
    char* addr;
};

s *ips;

Now allocating that struct array memory

num = 5
ips = (r *) malloc(num * sizeof(r));

I know malloc just allocates memory, and don't initialize, there could be garbage values.

Now I wonder if I don't initialize one, and try to access what would happen?

//Init for 4 of them
for(int i = 0; i < num-1; i++)
    ips[i].addr = strdup("123");

//Accessing un-initialize one:
if(ips[4].addr) {
    printf("Accessing uninitialize one and lets say freeing!!!");
    free(ips[4].addr);
}

Ideal should not be going into this for loop. But then I think because of garbage value it may be. I'm not sure!

RichieHindle
  • 272,464
  • 47
  • 358
  • 399
code muncher
  • 1,592
  • 2
  • 27
  • 46

2 Answers2

1

What will happen will be unpredictable as you can not know what the memory contained. You should either use calloc instead of malloc, or memset the memory after calling malloc.

Personally I prefer to use calloc as it saves a line of code and makes it a little easier to read your code later.

Geoffrey
  • 10,843
  • 3
  • 33
  • 46
  • Neither `calloc` nor `memset` is guaranteed to set pointers to null, or floating-point objects to `0.0`. – Keith Thompson Jul 16 '13 at 21:47
  • @Keith Thompson Your assertion that `calloc()` does not guarantee NULL or 0.0, is that although memory is zero filled, NULL and 0.0 _may_ not be made up of just zero bits? Or is there something else? – chux - Reinstate Monica Jul 16 '13 at 22:16
  • @chux: That's it. On most common implementations, both null pointers and floating-point `0.0` *are* represented as all-bits-zero, but the C standard doesn't guarantee it. If you're going to write code that depends on that assumption, I suggest at least making the assumption explicit. (I prefer not to depend on it; for example, `const foo_t foo_zero = { 0 };` gives you an object of type `foo_t` with all members set to zero (integer 0, floating 0.0, null pointer, ...). – Keith Thompson Jul 16 '13 at 22:42
  • @KeithThompson - Thanks, I was not aware of that, but doesn't the IEEE-754 standard cover the storage format? AFAIK this covers the binary representation, so if the compiler supports IEEE-754 (which most do these days) this should not be an issue. – Geoffrey Jul 16 '13 at 23:45
  • 1
    @Geoffrey: I think so, but C doesn't require IEEE-754 compliance. The fewer assumptions you make, the fewer things you have to think about when tracking down an inevitable error. – Keith Thompson Jul 16 '13 at 23:50
1

Initialize your variables.

Without initialization - all bets are off.
ips[4].addr, as you know, is uninitialized. So using:

// Various code
...
if(ips[4].addr) {

is a convoluted way of simple asking what does the following do?

int i;
if (i) {

The value of i could be the same every time you run the program. If could be different. There is no ideal of what should happen. It is simple undefined behavior (UB).

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256