is a not initialized local variable static? [duplicate] - c

This question already has answers here:
Undefined, unspecified and implementation-defined behavior
(9 answers)
Closed 4 years ago.
if the variable is initialized (i = 0), it's still 1 each time the function func is called, BUT
when i is not initialized:
#include <stdio.h>
int funct(void);
int main(void)
{
funct();
funct();
funct();
return 0;
}
int funct(void)
{
int i;
static int j = 0;
i++;
j++;
printf(" i = %d j = %d\n", i, j);
}
the output is
i = 1 j = 1
i = 2 j = 2
i = 3 j = 3
I don't understand why the variable i behaves like a static one!

The value is unspecified, so anything goes. But, likely the same memory is reused for each call to funct and with that, the same memory is reused and the i just pick up the old value left from the previous run.

Related

Array assigned statically does not overflow when assigning values out of range [duplicate]

This question already has answers here:
How dangerous is it to access an array out of bounds?
(12 answers)
Closed 4 years ago.
Why does this code work?
I have an array of 2 elements and it should overflow, but it does not even give me an error on Linux using gcc. Instead, it works and outputs 5.
int doS(int a[2])
{
printf("%d", a[4]);
}
int main()
{
int rows =2;
int a[rows];
a[0] = 1;
a[1] = 2;
a[2]= 3;
a[3] = 4;
a[4] = 5;
doS(a);
}
There is no guarantee that it always works! a[2], a[3] and a[4] are out of memory. Behaviour of the code is called as Undefined Behaviour
For example, try below one. Does it still work?
for (int i = 0; i < 100; ++i) {
a[i] = i + 1;
}

C: uninitialized local variable takes on unexpected value [duplicate]

This question already has answers here:
What happens to a declared, uninitialized variable in C? Does it have a value?
(9 answers)
Closed 6 years ago.
I'm really confused about the result of the following code:
#include <stdio.h>
#include <stdlib.h>
int one(int a, int b) {
int k, t;
k = a - b;
t = a + b + 1;
if (k % 2 == 0) return t;
else return 0;
}
int two(int x, int y) {
int m;
printf("%d\n", m);
return m + x + y;
}
main() {
int result = two(5, one(4, 3));
// printf("%d\n", one(4, 3));
printf("result is %d\n", result);
}
one(4, 3) returns 0, which is not surprising. But I don't understand why two(5, 0) returns 8. In other words, m takes on the value 3 without being initialized. How did this happen?
C does not automatically initialize values to 0 when you define them. Technically, reading that data before you initialize it is undefined behavior. In practice, this normally results in a garbage value containing whatever data was stored in that location previously.

Weird result from a macro while using pre-increment and post-increment operators [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 6 years ago.
I get 12 and 49 from this code
#include <stdio.h>
#define product(a) a*a
int main() {
int i = 3, j, k;
j = product(i++);
k = product(++i);
printf("%d %d\n", j, k);
return 0;
}
If instead of using macro you use a function then you get 9 and 25, which is what I would expect...
Can someone explain why does this happen?
If you expand the macro, the line
j = product(i++);
becomes
j = i++*i++;
and the line
k = product(++i);
becomes
k = ++i*++i;
Both these lines are subject to undefined behavior. You can try to make sense of it for one run of the program but the results are not guaranteed to be same with a different compiler or even the same compiler with different compiler flags.
When you use a function call instead of a macro, the behavior of the code is more straight forward. The line
j = product(i++);
is equivalent to:
int temp = i;
j = product(temp);
i++;
The line
k = product(++i);
is equivalent to:
++i;
k = product(i);
Hence, there are no problems when a function is used instead of a macro.
The output you expect will require you to change the macro to a function
#include <stdio.h>
int product(int a) { return a * a; }
int main() {
int i = 3, j, k;
j = product(i++);
k = product(++i);
printf("%d %d\n", j, k);
return 0;
}
Output
9 25
Since macro unpacks inline, the value of i changes in the main function's callframe and you get an unexpected result.

Pointers to pointer usage giving unexpected result [duplicate]

This question already has answers here:
returning a local variable from function in C [duplicate]
(4 answers)
Closed 7 years ago.
Why is the First Printing Statement in main(), printing 11 ?
#include<stdio.h>
void foo(int ** p){
int j = 11;
*p = &j;
printf("%d ", **p); //Printing 11
}
int main(){
int i = 10;
int *p = &i;
foo(&p);
printf("%d ", *p); //Printing 11
printf("%d ", *p); //Printing Random value
return 0;
}
Inside foo() , you're assigning the address of a automatic local variable j to *p. After foo() has finished execution, j does not exist anymore and thus, using (derererencing) p furthermore in main() invokes undefined behavior.
Now, the output of UB is, well, undefined.

How can I get an array and count with variable size by a function? [duplicate]

This question already has answers here:
Can a local variable's memory be accessed outside its scope?
(20 answers)
Closed 8 years ago.
I want to use a C function to get an array and count with variable size (#runtime). I implement this function as following:
void getList(int **listArray, int *count){
// get the total count
int totalListCount = getTotalListCount();
// Initialize the array
int theList[totalListCount];
memset( theList, 0, sizeof(int)*totalListCount );
// Set the elements in the array
for (int i = 0; i < totalListCount; i++) {
theList[i] = theElementAtIndex(i);
}
// Assign the value to the pointer.
*count = totalListCount;
*listArray = theList;
}
After getting the array and the count, I could print the values:
int *list;
int count;
getList(&list, &count);
for (int i = 0; i < count; i++) {
printf("list[%d]: %d \n", i, list[i]);
}
Is the implementation correct? Do I need to manage the memory for those pointers, and how?
// Initialize the array
int theList[totalListCount];
you should not return the function's local array, you should use malloc like this:
int *theList = malloc(totalListCount);
off course,you should free it when you not use it

Resources