Understanding garbage collection in C [closed] - c

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I decided to rewrite what I was doing going backwards to basics:
#include <stdio.h>
int main () {
int a;
int b;
int c;
int d;
printf("ta-dah: %i %i %i %i\n", a, b, c, d);
return 0;
}
I call it me.c and I compile it with gcc me.c and run it with ./a.out.
I get this as a result:
hc$ ./a.out
ta-dah: 32767 1477090280 0 0
This is a very simple program, and I am not sure where the error is. Any suggestions?

You dont initialize the values, so it prints garbage values (whatever happens to be on the stack memory at that point in (memory)spacetime.
You should initialize them first, if you want to be able to read from them.
int a = 3;
int b = 42;
int c = 1337;
int d = 0;
Example

Non-static variables (local variables) are indeterminate. Reading them prior to assigning a value results in undefined behavior.
Either initialize the variables:
#include <stdio.h>
int main () {
int a = 1;
int b = 2;
int c = 3;
int d = 4;
printf("ta-dah: %i %i %i %i\n", a, b, c, d);
return 0;
}
Output: ta-dah: 1 2 3 4
Or set them to static:
#include <stdio.h>
int main () {
static int a;
static int b;
static int c;
static int d;
printf("ta-dah: %i %i %i %i\n", a, b, c, d);
return 0;
}
Output: ta-dah: 0 0 0 0

Unless you initialize the variables with values you will see whatever value happens to be at the memory address used to store the variable's value.
Change it to:
#include <stdio.h>
int main () {
int a = 1;
int b = 2;
int c = 3;
int d = 4;
printf("ta-dah: %i %i %i %i\n", a, b, c, d);
return 0;
}
And you will get:
ta-dah: 1 2 3 4

Related

Auto VS Static Variable Scope in C programming

Helloo!! I am currently learning about variable scope in C programming, auto, static and extern. I am running this code below and I am not sure why it works. I thought auto variables are only defined in the function it is defined in and does not retain its value? How is it that this code is working? Shouldn't the int a,b be static variables instead?
#include <stdio.h>
void sum(void)
{
auto int a,b;
a = 91; b = 7;
printf("%d + %d = %d\n",a,b,a+b);
}
int main()
{
puts("Calling the sum() function:");
sum();
puts("done");
return(0);
}
Your variables are not retaining anything. They're always the same value because you're explicitly assigning them the same values in sum().
a = 91; b = 7;
If you change your code to accept the variables as parameters instead, you'll get the output you would expect.
#include <stdio.h>
void sum(int a, int b)
{
printf("%d + %d = %d\n",a,b,a+b);
}
int main()
{
puts("Calling the sum() function:");
sum(91, 7);
sum(23, 9);
auto int x = 44;
auto int y = 24;
sum(x, y);
puts("done");
return(0);
}
If you try to access x or y from within sum() now, you'll get an error that they aren't in scope.

What's wrong? Why pointer stays constant? [duplicate]

This question already has answers here:
Assigning a pointer to an integer
(6 answers)
Closed 4 years ago.
I learn C programming for a while and I had to create a program, which contains a function void hello() displays word 'Hello :)' and the number, how many time the function hello() was called. The code bellow displays 'Hello' but the number of function calling stays constant. I just want to know, what's wrong and why it isn't working as it should.
#include <stdio.h>
int main(void) {
void hello(int *p_number);
int number = 1, i;
int* p_number = number;
for (i = 1; i <= 10; i++){
hello(&p_number);
printf("Number in cyclus = %d\n", number);
number++;
}
return 0;
}
void hello(int *p_number){
printf("number of calling = %d, Hello :)\n", *p_number);
}
You need
int* p_number = &number;
and
hello(p_number);
at the calling site.
i.e. set p_number to the address of number. And do turn up the warning level on your compile and read them! There is a fair amount of redundancy in maintaining a pointer in hello; presumably this is for an exercise?
#include <stdio.h>
void hello(int *p_number);
int main(void) {
int number = 1, i;
int *p_number = &number;
for (i = 1; i <= 10; i++) {
hello(p_number);
printf("Number in cyclus = %d\n", number);
number++;
}
return 0;
}
void hello(int *p_number) {
printf("number of calling = %d, Hello :)\n", *p_number);
}
Set the pointer to point at the address of number, pass pointer address to function.

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.

C: Having an issue with scope

so I'm studying for a final and we are given this block of codeL
#include <stdio.h>
int a;
void addOne(void) {
a++;
printf(“W. a = %d\n”, a);
}
int removeOne(int a) {
int b = a – 1;
printf(“R. b = %d\n”, b);
}
void swap(int a, int *b) {
int temp = a;
a = *b;
*b = temp;
}
int main() {
a = 5;
int b = 20;
if (b > 15) {
int a = 53;
removeOne(b);
addOne(a);
printf(“X. a = %d\n”, a);
}
printf(“Y. a = %d, b = %d\n”, a, b);
swap(a, &b);
printf(“Z. a = %d, b = %d\n”, a, b);
return 0;
}
We are instructed to give the outputs of the program. I'm having trouble with the addone(a) where I came up with 54, the correct answer was 6. Is it 6 because when the function is declared it has the void (don't remember the technical term but the information it takes in to the function) rather than something like int a?
My more direct question is why does the function take the a initialized in the main function rather than the a in the if?
The reason that the answer is 6:
Note at the top that a is declared as a global. Later, in main there is a call to addOne(a) inside of a code block. That code block defines a local variable a as well. The a that is passed in that scope is the local a (53). It is passed into a function that accepts an unnamed void variable. In that function, however, there is a reference to a. Due to scoping, this will be the global a (5), so a++ will result in an output of 6.
That is a horrible exam question.

[C]--binary function [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I'm working in a small program using Language C.
I want to create a function which transform a decimal number to binary.
So i Have that:
#include< stdio.h>
int *binaire(int nb,int base)
{
int d,i=0,j;
int *tab= (int*) malloc(sizeof(int));
while(nb>0){
tab[i]=(nb%base);//inverser les bits
nb=nb/base;
i++;
}
return tab;
}
please Help
Thxs
sizeof returns the size in bytes, not in bits. May be you are looking for this:
int *tab= (int*) malloc(sizeof(int) * 8);
EDIT: As pointed in the comments, there are other problems here. Please check them too.
EDIT2: The previous code contains a bug in calculation. I am assuming that you are trying to store the bits in an int array. So the array will contain sizeof(int) * 8 integers, all either zero or one. So the code will be:
int arraySize = sizeof(int) * 8;
int *tab = (int *) malloc(arraySize * sizeof(int));
In fact you don't need to use full int to store a single bit. One byte character is enough to store a bit.
Somethign along those lines:
#include <math.h>
#include <string.h>
#include <stdio.h>
typedef struct value_
{
int* digits;
int nb_digits;
} value;
value* base_convert(int nb,int base) ;
value* make_value(int nb, int base)
{
value* v = malloc(sizeof(value));
if(v)
{
v->nb_digits = 1+round(log(nb)/log(base));
v->digits = malloc(sizeof(int)*v->nb_digits);
if(v->digits) base_convert(v, nb, base);
}
return v;
}
void destroy_value(value* v)
{
if(v->digits) free(v->digits);
free(v);
}
void base_convert(value* v, int nb,int base)
{
int i;
for(i=0;i<v->nb_digits;++i)
{
v->digits[v->nb_digits-i-1]=(nb%base);//inverser les bits
nb=nb/base;
}
return v;
}
int main()
{
int i;
value* v = make_value(137,2);
for(i=0;i<v->nb_digits;++i) printf("%d",v->digits[i]);
destroy_value(v);
return 0;
}
This needs :
a get_digit function to mask the rough access to value::digits
Number of digits is computed using log to be generic. The abse 2 version can be computed in a faster way using bitshifts.

Resources