1

Here is my code with the print statement followed by its output.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int *myF(int a[],int s, int n);

int *myFunc(int x[],int size,int nnum) {
   int i;
   size += 1;
   int *y = malloc(size);
   
   for(i=0;i<size;i++) {
      if(i==0)
         *y = nnum;
      else {
         *(y+i) = x[i-1];
         printf(" %d",*(y+i)); // <-- ***THIS THING RIGHT HERE***
      }
   }

   return y;
}

int main (int argc, char *argv[]) {
   int i;
   int x[7] = {1,2,3,4,5,6,7};
   
   int *P = myFunc(x,7,12);
   for(i=0;i<8;i++) {
      if(i==0) printf("\n");
      printf(" %d",*(P+i));
   }
return 0;
}

OUTPUT:

1 2 3 4 5 6 7 
12 1 2 3 4 5 6 7  

(The second matrix is what I want the code to output)

Here is my code without the print statement followed by its output.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int *myF(int a[],int s, int n);

int *myFunc(int x[],int size,int nnum) {
   int i;
   size += 1;
   int *y = malloc(size);
   
   for(i=0;i<size;i++) {
      if(i==0)
         *y = nnum;
      else {
         *(y+i) = x[i-1];
         // printf(" %d",*(y+i)); <-- ***THIS THING RIGHT HERE*** commented out
      }
   }

   return y;
}

int main (int argc, char *argv[]) {
   int i;
   int x[7] = {1,2,3,4,5,6,7};
   
   int *P = myFunc(x,7,12);
   for(i=0;i<8;i++) {
      if(i==0) printf("\n");
      printf(" %d",*(P+i));
   }
   return 0;
}

OUTPUT:

12 1 2 3 4 5 83 0 

(This is not what I want...)

Can somebody please explain where this code is pulling 83 and 0 for the last two elements in the array just because I chose to include or exclude a random print statement?

Any help would be greatly appreciated because I can't understand how C is pulling numbers out of thin air like this.

Howlium
  • 1,218
  • 1
  • 8
  • 19
Slick
  • 11
  • 1
  • also how would I free memory allocated by malloc in this without erasing the data there before the function returns y? Or do I even need to use free in this? – Slick Aug 19 '20 at 02:37
  • https://en.wikipedia.org/wiki/Heisenbug – jamesdlin Aug 19 '20 at 02:50
  • What's the "this" you mentioned? Is it `myFunc` or `main`? Most common way is create buffer outside and pass the pointer inside to fill in the value. So a maintainer may see malloc and free in the same block. – Louis Go Aug 19 '20 at 03:03
  • 1
    `*(y+i)` should be written as `y[i]`. **Both `x` and `y` are pointers(!)** so there is absolutely no reason to use different syntax for them.. – Antti Haapala -- Слава Україні Aug 19 '20 at 05:45

1 Answers1

2

Your malloc size is not correct and causes undefined behavior. The actual memory size is the length of array * size of array type.

The printf in main is actually printing something random in the memory.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int *myF(int a[],int s, int n);

int *myFunc(int x[],int size,int nnum) {
    int i;
    size += 1;
    //int *y = malloc(size  * sizeof(int) );
    int *y = malloc(size );
    for(i=0;i<size;i++) {
    if(i==0)
        *y = nnum;
    else {
        *(y+i) = x[i-1];
        printf(" %d",*(y+i)); // <-- ***THIS THING RIGHT HERE***
    }
}

return y;
}

int main (int argc, char *argv[]) {
   int i;
   int x[7] = {1,2,3,4,5,6,7};
   
   int *P = myFunc(x,7,12);
   for(i=0;i<8;i++) {
       if(i==0) printf("\n");
       printf(" %d",*(P+i));
   }
   free(P);
return 0;
}

If you're interesting in why printf changed the behavior, this post has in-depth explanation.

Also don't forget to free P in the end.

Louis Go
  • 2,213
  • 2
  • 16
  • 29