arrays - What occurrs in a 'for loop' at the hardware level? Is memory automatically assigned? (C++) -


i have code here reverses array of characters:

#include <iostream> #include <string>  using namespace std;  char s[50];  void reversechar(char * s) {     (int i=0; i<strlen(s)/2; ++i)     {         char temp = s[i];         s[i] = s[strlen(s)-i-1];         s[strlen(s)-i-1] = temp;     } }  int main() {     cout << "hello, program reverses words." << endl << endl;     cout << "enter in word, no spaces please:" << endl;     cin.getline(s, 50);     cout << "this word, has been reversed:" << endl;     reversechar(s);     cout << s;     return 0; } 

in loop can explain going on @ hardware level. understand 'temp' allocated byte, assigned value of s[i].

is memory allocated everything?

the equals sign, s[i], etc?

after byte assigned value in s[i], s[i] assigned other value in array s. other value assigned temp.

i'm having trouble understanding these bytes going, , how being manipulated c++.

i understand in line:

s[i] = s[strlen(s)-i-1]; 

the placeholder values being swapped?

in line:

s[strlen(s)-i-1] = temp; 

the 'copied' value sent 'temp'. happens temp value afterwards, become new 'temp' once loop reiterates?

in loop can explain going on @ hardware level. understand 'temp' allocated byte, assigned value of s[i].

is memory allocated everything?

yes, allocated, on stack. need learn difference between stack , heap.

yes, variables represented in form of memory. again, based on context, either stack or heap.

the equals sign, s[i], etc?

equals operator, inform compiler assignment operation taking place, no memory required that. s[i], on other hand, array object, represented in stack memory.

the 'mirrored' value sent 'temp'. happens temp value afterwards, become new 'temp' once loop reiterates?

since, in loop have declared temp as,

char temp; 

a new variable temp, created in stack every iteration.

i recommend read memory organization in typical operating system. can start basics here.

edit: note based on scope of variable, compiler automatically de-allocates memory stack variable based on scope. temp variable above, scope ends @ end of loop, , hence destroyed @ point. hence compiler not end allocating character space of memory every iteration.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -