Same memory location assignment for two static variables
![Multi tool use Multi tool use](http://sgv.ssvwv.com/sg/ssvwvcomimagb.png)
Multi tool use
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have the below code and I see that the two variables have been assigned with same address. Both the variables are completely different type . Is there anyway I can void this ? And under what circumstances does the same memory gets allocated to both the variables .
static int Sw_Type ;
static BOOL Sw_Update;
void main()
{
int i;
int bytes = 3;
if (Sw_Update!= TRUE)
{
for(i = 0; i< bytes ;i++)
{
Sw_Type [i] = *Ver_Value;
Ver_Value++;
}
Sw_Update= TRUE;
}
}
This is a snippet of my code and "Ver_Value" is a structure which gets assigned in different function.
So the problem I am seeing is , when Sw_Update gets updated, Sw_Type [1] is getting updated and I see these two have same memory address.
c arrays static memory-address
|
show 4 more comments
I have the below code and I see that the two variables have been assigned with same address. Both the variables are completely different type . Is there anyway I can void this ? And under what circumstances does the same memory gets allocated to both the variables .
static int Sw_Type ;
static BOOL Sw_Update;
void main()
{
int i;
int bytes = 3;
if (Sw_Update!= TRUE)
{
for(i = 0; i< bytes ;i++)
{
Sw_Type [i] = *Ver_Value;
Ver_Value++;
}
Sw_Update= TRUE;
}
}
This is a snippet of my code and "Ver_Value" is a structure which gets assigned in different function.
So the problem I am seeing is , when Sw_Update gets updated, Sw_Type [1] is getting updated and I see these two have same memory address.
c arrays static memory-address
1
how much memory do you think gets allocated with:static int Sw_Type ;
?
– bruceg
Jan 3 at 21:49
5
Empty arrays are illegal in C.
– Eugene Sh.
Jan 3 at 21:49
@EugeneSh.: There is no empty array in the code shown. There is an array of incomplete type. (It could have been completed.)
– Eric Postpischil
Jan 3 at 21:59
The limit on yourfor
loop is3
. Therefore, it is using array index values in the range 0-2. But, your array only defines a single element. You want:static int Sw_Type [3];
But, if you increasebytes
, you'll need to adjust the array accordingly. This is not a great way to do this. (e.g.) You'd be better off with#define bytes 3
above the array and then:static int Sw_Type [bytes];
– Craig Estey
Jan 3 at 22:33
@chux: Clang is okay with completing it later. I am not sure whether 6.9.2 3 is intended to be applied to each declaration as it appears or to the resolved composite type at the end of the translation unit.
– Eric Postpischil
Jan 3 at 23:10
|
show 4 more comments
I have the below code and I see that the two variables have been assigned with same address. Both the variables are completely different type . Is there anyway I can void this ? And under what circumstances does the same memory gets allocated to both the variables .
static int Sw_Type ;
static BOOL Sw_Update;
void main()
{
int i;
int bytes = 3;
if (Sw_Update!= TRUE)
{
for(i = 0; i< bytes ;i++)
{
Sw_Type [i] = *Ver_Value;
Ver_Value++;
}
Sw_Update= TRUE;
}
}
This is a snippet of my code and "Ver_Value" is a structure which gets assigned in different function.
So the problem I am seeing is , when Sw_Update gets updated, Sw_Type [1] is getting updated and I see these two have same memory address.
c arrays static memory-address
I have the below code and I see that the two variables have been assigned with same address. Both the variables are completely different type . Is there anyway I can void this ? And under what circumstances does the same memory gets allocated to both the variables .
static int Sw_Type ;
static BOOL Sw_Update;
void main()
{
int i;
int bytes = 3;
if (Sw_Update!= TRUE)
{
for(i = 0; i< bytes ;i++)
{
Sw_Type [i] = *Ver_Value;
Ver_Value++;
}
Sw_Update= TRUE;
}
}
This is a snippet of my code and "Ver_Value" is a structure which gets assigned in different function.
So the problem I am seeing is , when Sw_Update gets updated, Sw_Type [1] is getting updated and I see these two have same memory address.
c arrays static memory-address
c arrays static memory-address
edited Jan 3 at 22:09
![](https://i.stack.imgur.com/8hkBI.png?s=32&g=1)
![](https://i.stack.imgur.com/8hkBI.png?s=32&g=1)
Andy J
65131430
65131430
asked Jan 3 at 21:47
pranathipranathi
1022514
1022514
1
how much memory do you think gets allocated with:static int Sw_Type ;
?
– bruceg
Jan 3 at 21:49
5
Empty arrays are illegal in C.
– Eugene Sh.
Jan 3 at 21:49
@EugeneSh.: There is no empty array in the code shown. There is an array of incomplete type. (It could have been completed.)
– Eric Postpischil
Jan 3 at 21:59
The limit on yourfor
loop is3
. Therefore, it is using array index values in the range 0-2. But, your array only defines a single element. You want:static int Sw_Type [3];
But, if you increasebytes
, you'll need to adjust the array accordingly. This is not a great way to do this. (e.g.) You'd be better off with#define bytes 3
above the array and then:static int Sw_Type [bytes];
– Craig Estey
Jan 3 at 22:33
@chux: Clang is okay with completing it later. I am not sure whether 6.9.2 3 is intended to be applied to each declaration as it appears or to the resolved composite type at the end of the translation unit.
– Eric Postpischil
Jan 3 at 23:10
|
show 4 more comments
1
how much memory do you think gets allocated with:static int Sw_Type ;
?
– bruceg
Jan 3 at 21:49
5
Empty arrays are illegal in C.
– Eugene Sh.
Jan 3 at 21:49
@EugeneSh.: There is no empty array in the code shown. There is an array of incomplete type. (It could have been completed.)
– Eric Postpischil
Jan 3 at 21:59
The limit on yourfor
loop is3
. Therefore, it is using array index values in the range 0-2. But, your array only defines a single element. You want:static int Sw_Type [3];
But, if you increasebytes
, you'll need to adjust the array accordingly. This is not a great way to do this. (e.g.) You'd be better off with#define bytes 3
above the array and then:static int Sw_Type [bytes];
– Craig Estey
Jan 3 at 22:33
@chux: Clang is okay with completing it later. I am not sure whether 6.9.2 3 is intended to be applied to each declaration as it appears or to the resolved composite type at the end of the translation unit.
– Eric Postpischil
Jan 3 at 23:10
1
1
how much memory do you think gets allocated with:
static int Sw_Type ;
?– bruceg
Jan 3 at 21:49
how much memory do you think gets allocated with:
static int Sw_Type ;
?– bruceg
Jan 3 at 21:49
5
5
Empty arrays are illegal in C.
– Eugene Sh.
Jan 3 at 21:49
Empty arrays are illegal in C.
– Eugene Sh.
Jan 3 at 21:49
@EugeneSh.: There is no empty array in the code shown. There is an array of incomplete type. (It could have been completed.)
– Eric Postpischil
Jan 3 at 21:59
@EugeneSh.: There is no empty array in the code shown. There is an array of incomplete type. (It could have been completed.)
– Eric Postpischil
Jan 3 at 21:59
The limit on your
for
loop is 3
. Therefore, it is using array index values in the range 0-2. But, your array only defines a single element. You want: static int Sw_Type [3];
But, if you increase bytes
, you'll need to adjust the array accordingly. This is not a great way to do this. (e.g.) You'd be better off with #define bytes 3
above the array and then: static int Sw_Type [bytes];
– Craig Estey
Jan 3 at 22:33
The limit on your
for
loop is 3
. Therefore, it is using array index values in the range 0-2. But, your array only defines a single element. You want: static int Sw_Type [3];
But, if you increase bytes
, you'll need to adjust the array accordingly. This is not a great way to do this. (e.g.) You'd be better off with #define bytes 3
above the array and then: static int Sw_Type [bytes];
– Craig Estey
Jan 3 at 22:33
@chux: Clang is okay with completing it later. I am not sure whether 6.9.2 3 is intended to be applied to each declaration as it appears or to the resolved composite type at the end of the translation unit.
– Eric Postpischil
Jan 3 at 23:10
@chux: Clang is okay with completing it later. I am not sure whether 6.9.2 3 is intended to be applied to each declaration as it appears or to the resolved composite type at the end of the translation unit.
– Eric Postpischil
Jan 3 at 23:10
|
show 4 more comments
2 Answers
2
active
oldest
votes
static int Sw_Type ;
constitutes a tentative definition, per C 2018 6.9.2 2:
A declaration of an identifier for an object that has file scope without an initializer, and without a storage-class specifier or with the storage-class specifier static, constitutes a tentative definition. If a translation unit contains one or more tentative definitions for an identifier, and the translation unit contains no external definition for that identifier, then the behavior is exactly as if the translation unit contains a file scope declaration of that identifier, with the composite type as of the end of the translation unit, with an initializer equal to 0.
Since your program provides no non-tentative definition, it is as if it ended with static int Sw_Type = { 0 };
. (In case it is not clear from the text quoted above that the result is indeed an array of one element, it is made clear by Example 2 in paragraph 5 of the same clause.)
Thus, Sw_Type
is an array of one int
. It contains only the element Sw_Type[0]
. The behavior of accessing Sw_Type[1]
is not defined by the C standard. From the observations you report, it appears as if Sw_Update
follows Sw_Type
in memory, and accessing Sw_Type[1]
results in modifying Sw_Update
. This behavior is of course not reliable.
To make Sw_Type
larger, you must declare a size for it, as with static int Sw_Type[4];
.
Note: 6.9.2 3 says “If the declaration of an identifier for an object is a tentative definition and has internal linkage, the declared type shall not be an incomplete type.” While this might be read as applying to the declared type in each declaration that is a tentative definition, I think it might be intended to apply to the declared type of the object once its composite type is fully resolved at the end of the translation unit. Experimentally, Clang is okay with accepting an incomplete type at first and completing it later.
Thank you Eric! Declaring a size solved my problem . I have a question on dynamic size . When the size is dynamic which is the best data structure to be used?
– pranathi
Jan 4 at 14:50
@pranathi: For dynamic sizes, you do not define an array. You define a pointer to the desired type. Then, when the program is running, you callmalloc
to allocate space for as many objects as desired, and you set the pointer to point to that space.
– Eric Postpischil
Jan 4 at 16:16
Thanks for the response. I am doing Embedded programming , I cannot use malloc since it would cost me time and freeing is not hassle free. Is there any other way I can do this ? Thanks!
– pranathi
Jan 7 at 0:42
@pranathi: You either reserve memory at compile time, which requires a size at compile time, or you reserve memory at execution time, which requires some means of allocating memory at execution time. If you have a maximum amount of memory that may be required, you can reserve all of that at compile time, even if less is used at execution time. Otherwise, you must provide memory at execution time. Perhaps it might come from the stack in your system, but, one way or another, you must provide memory.
– Eric Postpischil
Jan 7 at 1:24
Thank you ! That helps!
– pranathi
Jan 14 at 17:21
add a comment |
So the problem I am seeing is , when Sw_Update gets updated, Sw_Type [1] is getting updated and I see these two have same memory address.
There is no Sw_Type [1]
. Only an array with two or more elements has a second entry, and Sw_Type
is not an array with two or more elements. Accessing an array out of bounds can certainly stomp on other objects.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2f54030258%2fsame-memory-location-assignment-for-two-static-variables%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
static int Sw_Type ;
constitutes a tentative definition, per C 2018 6.9.2 2:
A declaration of an identifier for an object that has file scope without an initializer, and without a storage-class specifier or with the storage-class specifier static, constitutes a tentative definition. If a translation unit contains one or more tentative definitions for an identifier, and the translation unit contains no external definition for that identifier, then the behavior is exactly as if the translation unit contains a file scope declaration of that identifier, with the composite type as of the end of the translation unit, with an initializer equal to 0.
Since your program provides no non-tentative definition, it is as if it ended with static int Sw_Type = { 0 };
. (In case it is not clear from the text quoted above that the result is indeed an array of one element, it is made clear by Example 2 in paragraph 5 of the same clause.)
Thus, Sw_Type
is an array of one int
. It contains only the element Sw_Type[0]
. The behavior of accessing Sw_Type[1]
is not defined by the C standard. From the observations you report, it appears as if Sw_Update
follows Sw_Type
in memory, and accessing Sw_Type[1]
results in modifying Sw_Update
. This behavior is of course not reliable.
To make Sw_Type
larger, you must declare a size for it, as with static int Sw_Type[4];
.
Note: 6.9.2 3 says “If the declaration of an identifier for an object is a tentative definition and has internal linkage, the declared type shall not be an incomplete type.” While this might be read as applying to the declared type in each declaration that is a tentative definition, I think it might be intended to apply to the declared type of the object once its composite type is fully resolved at the end of the translation unit. Experimentally, Clang is okay with accepting an incomplete type at first and completing it later.
Thank you Eric! Declaring a size solved my problem . I have a question on dynamic size . When the size is dynamic which is the best data structure to be used?
– pranathi
Jan 4 at 14:50
@pranathi: For dynamic sizes, you do not define an array. You define a pointer to the desired type. Then, when the program is running, you callmalloc
to allocate space for as many objects as desired, and you set the pointer to point to that space.
– Eric Postpischil
Jan 4 at 16:16
Thanks for the response. I am doing Embedded programming , I cannot use malloc since it would cost me time and freeing is not hassle free. Is there any other way I can do this ? Thanks!
– pranathi
Jan 7 at 0:42
@pranathi: You either reserve memory at compile time, which requires a size at compile time, or you reserve memory at execution time, which requires some means of allocating memory at execution time. If you have a maximum amount of memory that may be required, you can reserve all of that at compile time, even if less is used at execution time. Otherwise, you must provide memory at execution time. Perhaps it might come from the stack in your system, but, one way or another, you must provide memory.
– Eric Postpischil
Jan 7 at 1:24
Thank you ! That helps!
– pranathi
Jan 14 at 17:21
add a comment |
static int Sw_Type ;
constitutes a tentative definition, per C 2018 6.9.2 2:
A declaration of an identifier for an object that has file scope without an initializer, and without a storage-class specifier or with the storage-class specifier static, constitutes a tentative definition. If a translation unit contains one or more tentative definitions for an identifier, and the translation unit contains no external definition for that identifier, then the behavior is exactly as if the translation unit contains a file scope declaration of that identifier, with the composite type as of the end of the translation unit, with an initializer equal to 0.
Since your program provides no non-tentative definition, it is as if it ended with static int Sw_Type = { 0 };
. (In case it is not clear from the text quoted above that the result is indeed an array of one element, it is made clear by Example 2 in paragraph 5 of the same clause.)
Thus, Sw_Type
is an array of one int
. It contains only the element Sw_Type[0]
. The behavior of accessing Sw_Type[1]
is not defined by the C standard. From the observations you report, it appears as if Sw_Update
follows Sw_Type
in memory, and accessing Sw_Type[1]
results in modifying Sw_Update
. This behavior is of course not reliable.
To make Sw_Type
larger, you must declare a size for it, as with static int Sw_Type[4];
.
Note: 6.9.2 3 says “If the declaration of an identifier for an object is a tentative definition and has internal linkage, the declared type shall not be an incomplete type.” While this might be read as applying to the declared type in each declaration that is a tentative definition, I think it might be intended to apply to the declared type of the object once its composite type is fully resolved at the end of the translation unit. Experimentally, Clang is okay with accepting an incomplete type at first and completing it later.
Thank you Eric! Declaring a size solved my problem . I have a question on dynamic size . When the size is dynamic which is the best data structure to be used?
– pranathi
Jan 4 at 14:50
@pranathi: For dynamic sizes, you do not define an array. You define a pointer to the desired type. Then, when the program is running, you callmalloc
to allocate space for as many objects as desired, and you set the pointer to point to that space.
– Eric Postpischil
Jan 4 at 16:16
Thanks for the response. I am doing Embedded programming , I cannot use malloc since it would cost me time and freeing is not hassle free. Is there any other way I can do this ? Thanks!
– pranathi
Jan 7 at 0:42
@pranathi: You either reserve memory at compile time, which requires a size at compile time, or you reserve memory at execution time, which requires some means of allocating memory at execution time. If you have a maximum amount of memory that may be required, you can reserve all of that at compile time, even if less is used at execution time. Otherwise, you must provide memory at execution time. Perhaps it might come from the stack in your system, but, one way or another, you must provide memory.
– Eric Postpischil
Jan 7 at 1:24
Thank you ! That helps!
– pranathi
Jan 14 at 17:21
add a comment |
static int Sw_Type ;
constitutes a tentative definition, per C 2018 6.9.2 2:
A declaration of an identifier for an object that has file scope without an initializer, and without a storage-class specifier or with the storage-class specifier static, constitutes a tentative definition. If a translation unit contains one or more tentative definitions for an identifier, and the translation unit contains no external definition for that identifier, then the behavior is exactly as if the translation unit contains a file scope declaration of that identifier, with the composite type as of the end of the translation unit, with an initializer equal to 0.
Since your program provides no non-tentative definition, it is as if it ended with static int Sw_Type = { 0 };
. (In case it is not clear from the text quoted above that the result is indeed an array of one element, it is made clear by Example 2 in paragraph 5 of the same clause.)
Thus, Sw_Type
is an array of one int
. It contains only the element Sw_Type[0]
. The behavior of accessing Sw_Type[1]
is not defined by the C standard. From the observations you report, it appears as if Sw_Update
follows Sw_Type
in memory, and accessing Sw_Type[1]
results in modifying Sw_Update
. This behavior is of course not reliable.
To make Sw_Type
larger, you must declare a size for it, as with static int Sw_Type[4];
.
Note: 6.9.2 3 says “If the declaration of an identifier for an object is a tentative definition and has internal linkage, the declared type shall not be an incomplete type.” While this might be read as applying to the declared type in each declaration that is a tentative definition, I think it might be intended to apply to the declared type of the object once its composite type is fully resolved at the end of the translation unit. Experimentally, Clang is okay with accepting an incomplete type at first and completing it later.
static int Sw_Type ;
constitutes a tentative definition, per C 2018 6.9.2 2:
A declaration of an identifier for an object that has file scope without an initializer, and without a storage-class specifier or with the storage-class specifier static, constitutes a tentative definition. If a translation unit contains one or more tentative definitions for an identifier, and the translation unit contains no external definition for that identifier, then the behavior is exactly as if the translation unit contains a file scope declaration of that identifier, with the composite type as of the end of the translation unit, with an initializer equal to 0.
Since your program provides no non-tentative definition, it is as if it ended with static int Sw_Type = { 0 };
. (In case it is not clear from the text quoted above that the result is indeed an array of one element, it is made clear by Example 2 in paragraph 5 of the same clause.)
Thus, Sw_Type
is an array of one int
. It contains only the element Sw_Type[0]
. The behavior of accessing Sw_Type[1]
is not defined by the C standard. From the observations you report, it appears as if Sw_Update
follows Sw_Type
in memory, and accessing Sw_Type[1]
results in modifying Sw_Update
. This behavior is of course not reliable.
To make Sw_Type
larger, you must declare a size for it, as with static int Sw_Type[4];
.
Note: 6.9.2 3 says “If the declaration of an identifier for an object is a tentative definition and has internal linkage, the declared type shall not be an incomplete type.” While this might be read as applying to the declared type in each declaration that is a tentative definition, I think it might be intended to apply to the declared type of the object once its composite type is fully resolved at the end of the translation unit. Experimentally, Clang is okay with accepting an incomplete type at first and completing it later.
edited Jan 3 at 23:13
answered Jan 3 at 23:04
Eric PostpischilEric Postpischil
80.3k890169
80.3k890169
Thank you Eric! Declaring a size solved my problem . I have a question on dynamic size . When the size is dynamic which is the best data structure to be used?
– pranathi
Jan 4 at 14:50
@pranathi: For dynamic sizes, you do not define an array. You define a pointer to the desired type. Then, when the program is running, you callmalloc
to allocate space for as many objects as desired, and you set the pointer to point to that space.
– Eric Postpischil
Jan 4 at 16:16
Thanks for the response. I am doing Embedded programming , I cannot use malloc since it would cost me time and freeing is not hassle free. Is there any other way I can do this ? Thanks!
– pranathi
Jan 7 at 0:42
@pranathi: You either reserve memory at compile time, which requires a size at compile time, or you reserve memory at execution time, which requires some means of allocating memory at execution time. If you have a maximum amount of memory that may be required, you can reserve all of that at compile time, even if less is used at execution time. Otherwise, you must provide memory at execution time. Perhaps it might come from the stack in your system, but, one way or another, you must provide memory.
– Eric Postpischil
Jan 7 at 1:24
Thank you ! That helps!
– pranathi
Jan 14 at 17:21
add a comment |
Thank you Eric! Declaring a size solved my problem . I have a question on dynamic size . When the size is dynamic which is the best data structure to be used?
– pranathi
Jan 4 at 14:50
@pranathi: For dynamic sizes, you do not define an array. You define a pointer to the desired type. Then, when the program is running, you callmalloc
to allocate space for as many objects as desired, and you set the pointer to point to that space.
– Eric Postpischil
Jan 4 at 16:16
Thanks for the response. I am doing Embedded programming , I cannot use malloc since it would cost me time and freeing is not hassle free. Is there any other way I can do this ? Thanks!
– pranathi
Jan 7 at 0:42
@pranathi: You either reserve memory at compile time, which requires a size at compile time, or you reserve memory at execution time, which requires some means of allocating memory at execution time. If you have a maximum amount of memory that may be required, you can reserve all of that at compile time, even if less is used at execution time. Otherwise, you must provide memory at execution time. Perhaps it might come from the stack in your system, but, one way or another, you must provide memory.
– Eric Postpischil
Jan 7 at 1:24
Thank you ! That helps!
– pranathi
Jan 14 at 17:21
Thank you Eric! Declaring a size solved my problem . I have a question on dynamic size . When the size is dynamic which is the best data structure to be used?
– pranathi
Jan 4 at 14:50
Thank you Eric! Declaring a size solved my problem . I have a question on dynamic size . When the size is dynamic which is the best data structure to be used?
– pranathi
Jan 4 at 14:50
@pranathi: For dynamic sizes, you do not define an array. You define a pointer to the desired type. Then, when the program is running, you call
malloc
to allocate space for as many objects as desired, and you set the pointer to point to that space.– Eric Postpischil
Jan 4 at 16:16
@pranathi: For dynamic sizes, you do not define an array. You define a pointer to the desired type. Then, when the program is running, you call
malloc
to allocate space for as many objects as desired, and you set the pointer to point to that space.– Eric Postpischil
Jan 4 at 16:16
Thanks for the response. I am doing Embedded programming , I cannot use malloc since it would cost me time and freeing is not hassle free. Is there any other way I can do this ? Thanks!
– pranathi
Jan 7 at 0:42
Thanks for the response. I am doing Embedded programming , I cannot use malloc since it would cost me time and freeing is not hassle free. Is there any other way I can do this ? Thanks!
– pranathi
Jan 7 at 0:42
@pranathi: You either reserve memory at compile time, which requires a size at compile time, or you reserve memory at execution time, which requires some means of allocating memory at execution time. If you have a maximum amount of memory that may be required, you can reserve all of that at compile time, even if less is used at execution time. Otherwise, you must provide memory at execution time. Perhaps it might come from the stack in your system, but, one way or another, you must provide memory.
– Eric Postpischil
Jan 7 at 1:24
@pranathi: You either reserve memory at compile time, which requires a size at compile time, or you reserve memory at execution time, which requires some means of allocating memory at execution time. If you have a maximum amount of memory that may be required, you can reserve all of that at compile time, even if less is used at execution time. Otherwise, you must provide memory at execution time. Perhaps it might come from the stack in your system, but, one way or another, you must provide memory.
– Eric Postpischil
Jan 7 at 1:24
Thank you ! That helps!
– pranathi
Jan 14 at 17:21
Thank you ! That helps!
– pranathi
Jan 14 at 17:21
add a comment |
So the problem I am seeing is , when Sw_Update gets updated, Sw_Type [1] is getting updated and I see these two have same memory address.
There is no Sw_Type [1]
. Only an array with two or more elements has a second entry, and Sw_Type
is not an array with two or more elements. Accessing an array out of bounds can certainly stomp on other objects.
add a comment |
So the problem I am seeing is , when Sw_Update gets updated, Sw_Type [1] is getting updated and I see these two have same memory address.
There is no Sw_Type [1]
. Only an array with two or more elements has a second entry, and Sw_Type
is not an array with two or more elements. Accessing an array out of bounds can certainly stomp on other objects.
add a comment |
So the problem I am seeing is , when Sw_Update gets updated, Sw_Type [1] is getting updated and I see these two have same memory address.
There is no Sw_Type [1]
. Only an array with two or more elements has a second entry, and Sw_Type
is not an array with two or more elements. Accessing an array out of bounds can certainly stomp on other objects.
So the problem I am seeing is , when Sw_Update gets updated, Sw_Type [1] is getting updated and I see these two have same memory address.
There is no Sw_Type [1]
. Only an array with two or more elements has a second entry, and Sw_Type
is not an array with two or more elements. Accessing an array out of bounds can certainly stomp on other objects.
answered Jan 3 at 22:15
David SchwartzDavid Schwartz
139k14145230
139k14145230
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f54030258%2fsame-memory-location-assignment-for-two-static-variables%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
A,7U8v Jb3mlo,3l0PwqDLyN tW6XR6ZMUjbq17FtgR6951mn5EW9HIs,F8m45CC7Ayi,Ifc8XoM
1
how much memory do you think gets allocated with:
static int Sw_Type ;
?– bruceg
Jan 3 at 21:49
5
Empty arrays are illegal in C.
– Eugene Sh.
Jan 3 at 21:49
@EugeneSh.: There is no empty array in the code shown. There is an array of incomplete type. (It could have been completed.)
– Eric Postpischil
Jan 3 at 21:59
The limit on your
for
loop is3
. Therefore, it is using array index values in the range 0-2. But, your array only defines a single element. You want:static int Sw_Type [3];
But, if you increasebytes
, you'll need to adjust the array accordingly. This is not a great way to do this. (e.g.) You'd be better off with#define bytes 3
above the array and then:static int Sw_Type [bytes];
– Craig Estey
Jan 3 at 22:33
@chux: Clang is okay with completing it later. I am not sure whether 6.9.2 3 is intended to be applied to each declaration as it appears or to the resolved composite type at the end of the translation unit.
– Eric Postpischil
Jan 3 at 23:10