Writing declarations in a loop
up vote
1
down vote
favorite
The following question came to my mind:
Is there any difference between these two ways of running code?
Perhaps in memory management?
int main()
int counter = 1;
while(1)
int arr_one[3] = 0, 1, 2 ;
int arr_two[3] = 3, 4, 5 ;
int arr_three[3] = 6, 7, 8 ;
if(counter == 1)
for(int i = 0; i < 2; i++) printf("%dn", arr_one[i]);
if(counter == 2)
for(int i = 0; i < 2; i++) printf("%dn", arr_two[i]);
if(counter == 3)
for(int i = 0; i < 2; i++) printf("%dn", arr_three[i]);
if(counter >= 4) counter = 1; else counter++;
return 0;
//
int main()
int counter = 1;
while(1)
if(counter == 1)
for(int i = 0; i < 2; i++)
int arr_one[3] = 0, 1, 2 ;
printf("%dn", arr_one[i]);
if(counter == 2)
for(int i = 0; i < 2; i++)
int arr_two[3] = 3, 4, 5 ;
printf("%dn", arr_two[i]);
if(counter == 3)
for(int i = 0; i < 2; i++)
int arr_three[3] = 6, 7, 8 ;
printf("%dn", arr_three[i]);
if(counter >= 4) counter = 1; else counter++;
return 0;
//
This is a simplified version of code, which I am using.
Because I'm running the code on an Arduino, as well as with larger arrays, memory is pretty tight. Currently I have the code as shown in the first example, because I haven't read a lot about how C works at its heart.
I hope you could help me out!
Thanks in advance!
c
add a comment |
up vote
1
down vote
favorite
The following question came to my mind:
Is there any difference between these two ways of running code?
Perhaps in memory management?
int main()
int counter = 1;
while(1)
int arr_one[3] = 0, 1, 2 ;
int arr_two[3] = 3, 4, 5 ;
int arr_three[3] = 6, 7, 8 ;
if(counter == 1)
for(int i = 0; i < 2; i++) printf("%dn", arr_one[i]);
if(counter == 2)
for(int i = 0; i < 2; i++) printf("%dn", arr_two[i]);
if(counter == 3)
for(int i = 0; i < 2; i++) printf("%dn", arr_three[i]);
if(counter >= 4) counter = 1; else counter++;
return 0;
//
int main()
int counter = 1;
while(1)
if(counter == 1)
for(int i = 0; i < 2; i++)
int arr_one[3] = 0, 1, 2 ;
printf("%dn", arr_one[i]);
if(counter == 2)
for(int i = 0; i < 2; i++)
int arr_two[3] = 3, 4, 5 ;
printf("%dn", arr_two[i]);
if(counter == 3)
for(int i = 0; i < 2; i++)
int arr_three[3] = 6, 7, 8 ;
printf("%dn", arr_three[i]);
if(counter >= 4) counter = 1; else counter++;
return 0;
//
This is a simplified version of code, which I am using.
Because I'm running the code on an Arduino, as well as with larger arrays, memory is pretty tight. Currently I have the code as shown in the first example, because I haven't read a lot about how C works at its heart.
I hope you could help me out!
Thanks in advance!
c
I think we need areal-world-ctag that indicates when people are writing code on an actual computer (where it's ok to mention a "stack" and a "heap") and not just coding against the C standard.
– Sean Bright
Nov 8 at 17:16
@SeanBright Most computers these days are small embedded ones - billions per year - where C is quite popular. A large number of those platforms use a memory model lacking a heap and stack as typically understood.
– chux
Nov 8 at 20:05
The vast majority of people asking questions about C on SO are not writing it on embedded systems (I have no evidence to support this assertion, but I think we both know I am right). I appreciate the desire for technical correctness, but it can be a bit exhausting.
– Sean Bright
Nov 8 at 20:08
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
The following question came to my mind:
Is there any difference between these two ways of running code?
Perhaps in memory management?
int main()
int counter = 1;
while(1)
int arr_one[3] = 0, 1, 2 ;
int arr_two[3] = 3, 4, 5 ;
int arr_three[3] = 6, 7, 8 ;
if(counter == 1)
for(int i = 0; i < 2; i++) printf("%dn", arr_one[i]);
if(counter == 2)
for(int i = 0; i < 2; i++) printf("%dn", arr_two[i]);
if(counter == 3)
for(int i = 0; i < 2; i++) printf("%dn", arr_three[i]);
if(counter >= 4) counter = 1; else counter++;
return 0;
//
int main()
int counter = 1;
while(1)
if(counter == 1)
for(int i = 0; i < 2; i++)
int arr_one[3] = 0, 1, 2 ;
printf("%dn", arr_one[i]);
if(counter == 2)
for(int i = 0; i < 2; i++)
int arr_two[3] = 3, 4, 5 ;
printf("%dn", arr_two[i]);
if(counter == 3)
for(int i = 0; i < 2; i++)
int arr_three[3] = 6, 7, 8 ;
printf("%dn", arr_three[i]);
if(counter >= 4) counter = 1; else counter++;
return 0;
//
This is a simplified version of code, which I am using.
Because I'm running the code on an Arduino, as well as with larger arrays, memory is pretty tight. Currently I have the code as shown in the first example, because I haven't read a lot about how C works at its heart.
I hope you could help me out!
Thanks in advance!
c
The following question came to my mind:
Is there any difference between these two ways of running code?
Perhaps in memory management?
int main()
int counter = 1;
while(1)
int arr_one[3] = 0, 1, 2 ;
int arr_two[3] = 3, 4, 5 ;
int arr_three[3] = 6, 7, 8 ;
if(counter == 1)
for(int i = 0; i < 2; i++) printf("%dn", arr_one[i]);
if(counter == 2)
for(int i = 0; i < 2; i++) printf("%dn", arr_two[i]);
if(counter == 3)
for(int i = 0; i < 2; i++) printf("%dn", arr_three[i]);
if(counter >= 4) counter = 1; else counter++;
return 0;
//
int main()
int counter = 1;
while(1)
if(counter == 1)
for(int i = 0; i < 2; i++)
int arr_one[3] = 0, 1, 2 ;
printf("%dn", arr_one[i]);
if(counter == 2)
for(int i = 0; i < 2; i++)
int arr_two[3] = 3, 4, 5 ;
printf("%dn", arr_two[i]);
if(counter == 3)
for(int i = 0; i < 2; i++)
int arr_three[3] = 6, 7, 8 ;
printf("%dn", arr_three[i]);
if(counter >= 4) counter = 1; else counter++;
return 0;
//
This is a simplified version of code, which I am using.
Because I'm running the code on an Arduino, as well as with larger arrays, memory is pretty tight. Currently I have the code as shown in the first example, because I haven't read a lot about how C works at its heart.
I hope you could help me out!
Thanks in advance!
c
c
edited Nov 8 at 17:04
asked Nov 8 at 16:50
itzFlubby
10515
10515
I think we need areal-world-ctag that indicates when people are writing code on an actual computer (where it's ok to mention a "stack" and a "heap") and not just coding against the C standard.
– Sean Bright
Nov 8 at 17:16
@SeanBright Most computers these days are small embedded ones - billions per year - where C is quite popular. A large number of those platforms use a memory model lacking a heap and stack as typically understood.
– chux
Nov 8 at 20:05
The vast majority of people asking questions about C on SO are not writing it on embedded systems (I have no evidence to support this assertion, but I think we both know I am right). I appreciate the desire for technical correctness, but it can be a bit exhausting.
– Sean Bright
Nov 8 at 20:08
add a comment |
I think we need areal-world-ctag that indicates when people are writing code on an actual computer (where it's ok to mention a "stack" and a "heap") and not just coding against the C standard.
– Sean Bright
Nov 8 at 17:16
@SeanBright Most computers these days are small embedded ones - billions per year - where C is quite popular. A large number of those platforms use a memory model lacking a heap and stack as typically understood.
– chux
Nov 8 at 20:05
The vast majority of people asking questions about C on SO are not writing it on embedded systems (I have no evidence to support this assertion, but I think we both know I am right). I appreciate the desire for technical correctness, but it can be a bit exhausting.
– Sean Bright
Nov 8 at 20:08
I think we need a
real-world-c tag that indicates when people are writing code on an actual computer (where it's ok to mention a "stack" and a "heap") and not just coding against the C standard.– Sean Bright
Nov 8 at 17:16
I think we need a
real-world-c tag that indicates when people are writing code on an actual computer (where it's ok to mention a "stack" and a "heap") and not just coding against the C standard.– Sean Bright
Nov 8 at 17:16
@SeanBright Most computers these days are small embedded ones - billions per year - where C is quite popular. A large number of those platforms use a memory model lacking a heap and stack as typically understood.
– chux
Nov 8 at 20:05
@SeanBright Most computers these days are small embedded ones - billions per year - where C is quite popular. A large number of those platforms use a memory model lacking a heap and stack as typically understood.
– chux
Nov 8 at 20:05
The vast majority of people asking questions about C on SO are not writing it on embedded systems (I have no evidence to support this assertion, but I think we both know I am right). I appreciate the desire for technical correctness, but it can be a bit exhausting.
– Sean Bright
Nov 8 at 20:08
The vast majority of people asking questions about C on SO are not writing it on embedded systems (I have no evidence to support this assertion, but I think we both know I am right). I appreciate the desire for technical correctness, but it can be a bit exhausting.
– Sean Bright
Nov 8 at 20:08
add a comment |
3 Answers
3
active
oldest
votes
up vote
4
down vote
accepted
Strictly speaking, C doesn't even have a stack, however most implementation use one, and yours likely does.
That being said, it's best to restrict variables to the innermost scope needed to use them. That way, they don't consume stack space longer than needed and they aren't visible to scopes that don't need them.
So your second approach is preferable.
add a comment |
up vote
1
down vote
The C standard does not contain such information. The standard sets a number of rules that determines the output of the program. The standard doesn't care how that output is obtained, how much memory is used, how long time it takes and so on...
So your question can't be answered in general. Instead it depends on the platform used and compiler used.
Modern compilers are very good at optimizing the code so it's nearly impossible to predict whether a specific way of writing C code will improve the resulting program.
The best advice is to write your code in an easy to understand way and let the compiler do the optimization. If performance turns out to be bad, profile the code to find bottle necks.
add a comment |
up vote
0
down vote
On arduino, better get some very optimized code. It's not an Atari 2600, okay, but still.
Here is a little code that could save you much space (note that this code will resolve what your post says. It might not work for you).
int main()
int counter = 0;
int arr[3] = 0, 1, 2;
if (counter < 3)
for (int i = 0; i < 2; i++)
printf("%dn", arr[i] + 3 * counter);//better replace 3 with arr lenth.
counter++;
else
counter = 1;
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
Strictly speaking, C doesn't even have a stack, however most implementation use one, and yours likely does.
That being said, it's best to restrict variables to the innermost scope needed to use them. That way, they don't consume stack space longer than needed and they aren't visible to scopes that don't need them.
So your second approach is preferable.
add a comment |
up vote
4
down vote
accepted
Strictly speaking, C doesn't even have a stack, however most implementation use one, and yours likely does.
That being said, it's best to restrict variables to the innermost scope needed to use them. That way, they don't consume stack space longer than needed and they aren't visible to scopes that don't need them.
So your second approach is preferable.
add a comment |
up vote
4
down vote
accepted
up vote
4
down vote
accepted
Strictly speaking, C doesn't even have a stack, however most implementation use one, and yours likely does.
That being said, it's best to restrict variables to the innermost scope needed to use them. That way, they don't consume stack space longer than needed and they aren't visible to scopes that don't need them.
So your second approach is preferable.
Strictly speaking, C doesn't even have a stack, however most implementation use one, and yours likely does.
That being said, it's best to restrict variables to the innermost scope needed to use them. That way, they don't consume stack space longer than needed and they aren't visible to scopes that don't need them.
So your second approach is preferable.
answered Nov 8 at 16:54
dbush
89.4k1298131
89.4k1298131
add a comment |
add a comment |
up vote
1
down vote
The C standard does not contain such information. The standard sets a number of rules that determines the output of the program. The standard doesn't care how that output is obtained, how much memory is used, how long time it takes and so on...
So your question can't be answered in general. Instead it depends on the platform used and compiler used.
Modern compilers are very good at optimizing the code so it's nearly impossible to predict whether a specific way of writing C code will improve the resulting program.
The best advice is to write your code in an easy to understand way and let the compiler do the optimization. If performance turns out to be bad, profile the code to find bottle necks.
add a comment |
up vote
1
down vote
The C standard does not contain such information. The standard sets a number of rules that determines the output of the program. The standard doesn't care how that output is obtained, how much memory is used, how long time it takes and so on...
So your question can't be answered in general. Instead it depends on the platform used and compiler used.
Modern compilers are very good at optimizing the code so it's nearly impossible to predict whether a specific way of writing C code will improve the resulting program.
The best advice is to write your code in an easy to understand way and let the compiler do the optimization. If performance turns out to be bad, profile the code to find bottle necks.
add a comment |
up vote
1
down vote
up vote
1
down vote
The C standard does not contain such information. The standard sets a number of rules that determines the output of the program. The standard doesn't care how that output is obtained, how much memory is used, how long time it takes and so on...
So your question can't be answered in general. Instead it depends on the platform used and compiler used.
Modern compilers are very good at optimizing the code so it's nearly impossible to predict whether a specific way of writing C code will improve the resulting program.
The best advice is to write your code in an easy to understand way and let the compiler do the optimization. If performance turns out to be bad, profile the code to find bottle necks.
The C standard does not contain such information. The standard sets a number of rules that determines the output of the program. The standard doesn't care how that output is obtained, how much memory is used, how long time it takes and so on...
So your question can't be answered in general. Instead it depends on the platform used and compiler used.
Modern compilers are very good at optimizing the code so it's nearly impossible to predict whether a specific way of writing C code will improve the resulting program.
The best advice is to write your code in an easy to understand way and let the compiler do the optimization. If performance turns out to be bad, profile the code to find bottle necks.
answered Nov 8 at 17:08
4386427
19.6k21745
19.6k21745
add a comment |
add a comment |
up vote
0
down vote
On arduino, better get some very optimized code. It's not an Atari 2600, okay, but still.
Here is a little code that could save you much space (note that this code will resolve what your post says. It might not work for you).
int main()
int counter = 0;
int arr[3] = 0, 1, 2;
if (counter < 3)
for (int i = 0; i < 2; i++)
printf("%dn", arr[i] + 3 * counter);//better replace 3 with arr lenth.
counter++;
else
counter = 1;
add a comment |
up vote
0
down vote
On arduino, better get some very optimized code. It's not an Atari 2600, okay, but still.
Here is a little code that could save you much space (note that this code will resolve what your post says. It might not work for you).
int main()
int counter = 0;
int arr[3] = 0, 1, 2;
if (counter < 3)
for (int i = 0; i < 2; i++)
printf("%dn", arr[i] + 3 * counter);//better replace 3 with arr lenth.
counter++;
else
counter = 1;
add a comment |
up vote
0
down vote
up vote
0
down vote
On arduino, better get some very optimized code. It's not an Atari 2600, okay, but still.
Here is a little code that could save you much space (note that this code will resolve what your post says. It might not work for you).
int main()
int counter = 0;
int arr[3] = 0, 1, 2;
if (counter < 3)
for (int i = 0; i < 2; i++)
printf("%dn", arr[i] + 3 * counter);//better replace 3 with arr lenth.
counter++;
else
counter = 1;
On arduino, better get some very optimized code. It's not an Atari 2600, okay, but still.
Here is a little code that could save you much space (note that this code will resolve what your post says. It might not work for you).
int main()
int counter = 0;
int arr[3] = 0, 1, 2;
if (counter < 3)
for (int i = 0; i < 2; i++)
printf("%dn", arr[i] + 3 * counter);//better replace 3 with arr lenth.
counter++;
else
counter = 1;
answered Nov 8 at 17:00
Jean-Marc Zimmer
32514
32514
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53212458%2fwriting-declarations-in-a-loop%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I think we need a
real-world-ctag that indicates when people are writing code on an actual computer (where it's ok to mention a "stack" and a "heap") and not just coding against the C standard.– Sean Bright
Nov 8 at 17:16
@SeanBright Most computers these days are small embedded ones - billions per year - where C is quite popular. A large number of those platforms use a memory model lacking a heap and stack as typically understood.
– chux
Nov 8 at 20:05
The vast majority of people asking questions about C on SO are not writing it on embedded systems (I have no evidence to support this assertion, but I think we both know I am right). I appreciate the desire for technical correctness, but it can be a bit exhausting.
– Sean Bright
Nov 8 at 20:08