Memory address and vlaue store in loop

Status
Not open for further replies.

Thread Starter

Parth786

Joined Jun 19, 2017
642
I am trying to figured out what happen when loop run on hardware. how it works on hardware. I wrote c program for "for " to store values in variable
C:
#include<stdio.h>

int main(void)
{
    int i = 0;
    int *pointer;
    pointer = &i;
  
    printf("value of variable: %d \n", i);
    printf("address of variable : %p \n",&pointer);  

    for(i = 0; i < 4; i++)
    {
       printf("value of variable: %d \n", i);
       printf("address of variable : %p \n",&pointer);
    }
  
      printf("value of variable: %d \n", i);
      printf("address of variable : %p\n",&pointer);
     
   return 0;
}
at the start 0 store in variable i and pointer hold the address of variable i so the value store in variable is 0 and address of variable i is 0061FF28. When loop start i set to 0 the address of variable is same had previous address then we compare value of i with 4 if it's less the 4 then print its value and address if its false then just stop loop

value of variable: 0
address of variable : 0061FF28

value of variable: 0
address of variable : 0061FF28
value of variable: 1
address of variable : 0061FF28
value of variable: 2
address of variable : 0061FF28
value of variable: 3
address of variable : 0061FF28

value of variable: 4
address of variable : 0061FF28

Program show that the value of variable change but the address of that variable will be same.

I don't understand why the value of variables changes when loop exit. What happen when this program run on hardware.
 

LesJones

Joined Jan 8, 2017
4,511
I have almost no knowledge of "C" programming but to me it looks like you only have one variable (pointer) Each iteration of the loop you increment i and store is in that variable. As you only have one variable so it only has one address. Depending on the word size of the microproccesor the variable probably occupy a number of memory addresses. I think what you are trying to do is address elements in an array.

Les.
 

miniwinwm

Joined Feb 2, 2018
68
> I don't understand why the value of variables changes when loop exit.
It doesn't, it's the other way round. Because i has changed to a value that is no longer less than 4 the loop exits.

> What happen when this program run on hardware.
The same as you have seen. Why don't you download a free compiler and IDE, run the code in the debugger, and watch what's happening?
 

AlbertHall

Joined Jun 4, 2014
12,625
I don't understand why the value of variables changes when loop exit. What happen when this program run on hardware.
At the end of each time round the loop, the increment part (i++) is executed and the the value is compared to the limit value. If the check fails and the loop exits, the value has already been incremented so i will be 4.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
I think what you are trying to do is address elements in an array.
Les.
> watch what's happening?
What the part of hardware make loop , Do we use the counter to make the loop or Do we use the register to make the loop. Generally register is used to hold the data and counter is use to increment or decrements data value
.
What happen when this program run on hardware
 
Last edited:

AlbertHall

Joined Jun 4, 2014
12,625
What happen when this program run on hardware
It's not down to the hardware, it's down to the specification of the C language.
That program would be compiled to whatever language the hardware needs. The program would produce the same result whether you compile a very small PIC microcontroller or a modern PC processor. What happens on the hardware would be very different but the result would be the same.
 

joeyd999

Joined Jun 6, 2011
6,279
Regardless of anything, why would you expect the pointer variable "pointer" to ever change? You only assign its value once:

C:
pointer = &i;
Edit: In fact, I don't think you are displaying the address of i, but the address of pointer (which is only declared once and never changed).
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Regardless of anything, why would you expect the pointer variable "pointer" to ever change? You only assign its value once:
Edit: In fact, I don't think you are displaying the address of i, but the address of pointer (which is only declared once and never changed).
Please look at this program I am printing four values. I understand difference between both address
C:
#include<stdio.h>

int main(void)
{
    int i = 0;
    int *pointer;
    pointer = &i;
 
    printf("value of variable: %d \n", i);
    printf("value of pointer: %p \n", pointer);
    printf("address of variable : %p \n",&i); 
    printf("address of pointer : %p\n",&pointer);

    return 0;
}
Program output
value of variable: 0
value of pointer: 0061FF2C
address of variable : 0061FF2C
address of pointer : 0061FF28
 

joeyd999

Joined Jun 6, 2011
6,279
Please look at this program I am printing four values. I understand difference between both address
C:
#include<stdio.h>

int main(void)
{
    int i = 0;
    int *pointer;
    pointer = &i;

    printf("value of variable: %d \n", i);
    printf("value of pointer: %p \n", pointer);
    printf("address of variable : %p \n",&i);
    printf("address of pointer : %p\n",&pointer);

    return 0;
}
Program output
value of variable: 0
value of pointer: 0061FF2C
address of variable : 0061FF2C
address of pointer : 0061FF28
Thanks for the confirmation of my edit.
 

WBahn

Joined Mar 31, 2012
32,824
If you want to understand what is happening on the hardware when you run high level code, then work through the Nand2Tetris project that I've suggested to you many times already.
 

philba

Joined Aug 17, 2017
959
This is a weird thread. At first I was confused by "how does loop make on hardware" but realized you were asking how the loop is implemented by the compiler though the questions about the variables still seems stunningly misdirected. Do you know that you can get the actual assembler code the compiler generates? Not sure which compiler you are using but they all allow it. So, instead of asking questions, why don't you print the assembler listing, get an assembler manual and figure it out for yourself. Ask questions about op codes and such but at least put in some effort to understand how the machine works. Any good software engineer has a strong understand of the underlying machine and what machine code the compiler is going to generate.

By the way, if you can, turn off any optimizations - otherwise, the generated code will be even more confusing.

A note for the purists - most compilers don't use assembly code as an intermediate step but generate the actual machine codes directly. The assembly code listing is usually generated as the compiler emits the machine code if the option is set.
 

WBahn

Joined Mar 31, 2012
32,824
Any good software engineer has a strong understand of the underlying machine and what machine code the compiler is going to generate.
Sadly, that is becoming increasingly rare. Or perhaps it is more accurate to say that "good" software engineers are becoming increasingly rare specifically because they lack such understanding.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
I just thought about this and asked question I do not want to go into more detail

I already have a lot to study. I Just wanted to know when we write loop in c programming, how that loop works on the hardware.

Does register work or counter work for loop or something else work. I think my question was wrong
 

AlbertHall

Joined Jun 4, 2014
12,625
I just thought about this and asked question I do not want to go into more detail

I already have a lot to study. I Just wanted to know when we write loop in c programming, how that loop works on the hardware.

Does register work or counter work for loop or something else work. I think my question was wrong
There can only be an answer to that question for a specific compiler and specific hardware.

A particular compiler may generate different code on different hardware.
Different compilers may make different code for the same hardware.
 

philba

Joined Aug 17, 2017
959
Sadly, that is becoming increasingly rare. Or perhaps it is more accurate to say that "good" software engineers are becoming increasingly rare specifically because they lack such understanding.
I guess it's easier to ask someone else to think for them.

I had an AHA! moment early on when I realized how the compiler generated code for the machine I was using (PDP-11). Within a short period I was writing significantly better code and debugging a lot faster. There seems to be a fear of looking at the actual machine code, like it will hurt their little brains or something. Or perhaps detract from all the high level thinking going on in there.

yeah - I'm in a cranky mood right now...
 

philba

Joined Aug 17, 2017
959
I just thought about this and asked question I do not want to go into more detail

I already have a lot to study. I Just wanted to know when we write loop in c programming, how that loop works on the hardware.

Does register work or counter work for loop or something else work. I think my question was wrong
So, you just ask questions you don't really want answered? Do you waste people's time in your real life too?
 
Status
Not open for further replies.
Top