Use the 2D pointer without allocation
In the following C code, I allocate, initialize, and define values for **c
. I want **assign
to take **c
's values. But I only declare a 2D pointer for **assign
, no memory allocation, and the code runs successfully and print out the same results as **c
. I don't know why?
int main(){
int i,j;
float **c=NULL, **assign=NULL;
c = (float **)calloc(2,sizeof(float *));
for (i=0;i<2;i++){
c[i] = (float *)calloc(3,sizeof(float));
}
for (i=0;i<2;i++){
for (j=0;j<3;j++){
c[i][j] = i+j;
printf("c[%d][%d]=%fn",i,j,c[i][j]);
}
}
assign = c;
for (i=0;i<2;i++){
for (j=0;j<3;j++){
printf("assign[%d][%d]=%fn",i,j,assign[i][j]);
}
}
return 0;
}
c pointers dynamic-memory-allocation
add a comment |
In the following C code, I allocate, initialize, and define values for **c
. I want **assign
to take **c
's values. But I only declare a 2D pointer for **assign
, no memory allocation, and the code runs successfully and print out the same results as **c
. I don't know why?
int main(){
int i,j;
float **c=NULL, **assign=NULL;
c = (float **)calloc(2,sizeof(float *));
for (i=0;i<2;i++){
c[i] = (float *)calloc(3,sizeof(float));
}
for (i=0;i<2;i++){
for (j=0;j<3;j++){
c[i][j] = i+j;
printf("c[%d][%d]=%fn",i,j,c[i][j]);
}
}
assign = c;
for (i=0;i<2;i++){
for (j=0;j<3;j++){
printf("assign[%d][%d]=%fn",i,j,assign[i][j]);
}
}
return 0;
}
c pointers dynamic-memory-allocation
c
andassign
are pointers, not arrays. Code is simply assigning a pointer withassign = c;
.
– chux
Jan 19 at 3:18
1
Two pointers can point to the same memory. As long as the memory being pointed to has been properly allocated this is perfectly safe. You don't have to allocate separately for each pointer. Having said that, note that in C saying that "the code runs successfully" is not always proof that the code is correct. Undefined behavior often looks like code that "runs successfully" most of the time.
– John Coleman
Jan 19 at 3:20
@JohnColeman So you mean the code is correct, but my words are not reasonable?
– james
Jan 19 at 3:51
@james I don't know if your code is correct. I didn't run it or think about it very deeply. Nothing jumps out at me. I just wanted to sound a note of caution to not read too much into the fact that the code seems to be working.
– John Coleman
Jan 19 at 4:17
add a comment |
In the following C code, I allocate, initialize, and define values for **c
. I want **assign
to take **c
's values. But I only declare a 2D pointer for **assign
, no memory allocation, and the code runs successfully and print out the same results as **c
. I don't know why?
int main(){
int i,j;
float **c=NULL, **assign=NULL;
c = (float **)calloc(2,sizeof(float *));
for (i=0;i<2;i++){
c[i] = (float *)calloc(3,sizeof(float));
}
for (i=0;i<2;i++){
for (j=0;j<3;j++){
c[i][j] = i+j;
printf("c[%d][%d]=%fn",i,j,c[i][j]);
}
}
assign = c;
for (i=0;i<2;i++){
for (j=0;j<3;j++){
printf("assign[%d][%d]=%fn",i,j,assign[i][j]);
}
}
return 0;
}
c pointers dynamic-memory-allocation
In the following C code, I allocate, initialize, and define values for **c
. I want **assign
to take **c
's values. But I only declare a 2D pointer for **assign
, no memory allocation, and the code runs successfully and print out the same results as **c
. I don't know why?
int main(){
int i,j;
float **c=NULL, **assign=NULL;
c = (float **)calloc(2,sizeof(float *));
for (i=0;i<2;i++){
c[i] = (float *)calloc(3,sizeof(float));
}
for (i=0;i<2;i++){
for (j=0;j<3;j++){
c[i][j] = i+j;
printf("c[%d][%d]=%fn",i,j,c[i][j]);
}
}
assign = c;
for (i=0;i<2;i++){
for (j=0;j<3;j++){
printf("assign[%d][%d]=%fn",i,j,assign[i][j]);
}
}
return 0;
}
c pointers dynamic-memory-allocation
c pointers dynamic-memory-allocation
edited Jan 19 at 6:26
Mohammadreza Farahani
2,02531424
2,02531424
asked Jan 19 at 3:11
jamesjames
61
61
c
andassign
are pointers, not arrays. Code is simply assigning a pointer withassign = c;
.
– chux
Jan 19 at 3:18
1
Two pointers can point to the same memory. As long as the memory being pointed to has been properly allocated this is perfectly safe. You don't have to allocate separately for each pointer. Having said that, note that in C saying that "the code runs successfully" is not always proof that the code is correct. Undefined behavior often looks like code that "runs successfully" most of the time.
– John Coleman
Jan 19 at 3:20
@JohnColeman So you mean the code is correct, but my words are not reasonable?
– james
Jan 19 at 3:51
@james I don't know if your code is correct. I didn't run it or think about it very deeply. Nothing jumps out at me. I just wanted to sound a note of caution to not read too much into the fact that the code seems to be working.
– John Coleman
Jan 19 at 4:17
add a comment |
c
andassign
are pointers, not arrays. Code is simply assigning a pointer withassign = c;
.
– chux
Jan 19 at 3:18
1
Two pointers can point to the same memory. As long as the memory being pointed to has been properly allocated this is perfectly safe. You don't have to allocate separately for each pointer. Having said that, note that in C saying that "the code runs successfully" is not always proof that the code is correct. Undefined behavior often looks like code that "runs successfully" most of the time.
– John Coleman
Jan 19 at 3:20
@JohnColeman So you mean the code is correct, but my words are not reasonable?
– james
Jan 19 at 3:51
@james I don't know if your code is correct. I didn't run it or think about it very deeply. Nothing jumps out at me. I just wanted to sound a note of caution to not read too much into the fact that the code seems to be working.
– John Coleman
Jan 19 at 4:17
c
and assign
are pointers, not arrays. Code is simply assigning a pointer with assign = c;
.– chux
Jan 19 at 3:18
c
and assign
are pointers, not arrays. Code is simply assigning a pointer with assign = c;
.– chux
Jan 19 at 3:18
1
1
Two pointers can point to the same memory. As long as the memory being pointed to has been properly allocated this is perfectly safe. You don't have to allocate separately for each pointer. Having said that, note that in C saying that "the code runs successfully" is not always proof that the code is correct. Undefined behavior often looks like code that "runs successfully" most of the time.
– John Coleman
Jan 19 at 3:20
Two pointers can point to the same memory. As long as the memory being pointed to has been properly allocated this is perfectly safe. You don't have to allocate separately for each pointer. Having said that, note that in C saying that "the code runs successfully" is not always proof that the code is correct. Undefined behavior often looks like code that "runs successfully" most of the time.
– John Coleman
Jan 19 at 3:20
@JohnColeman So you mean the code is correct, but my words are not reasonable?
– james
Jan 19 at 3:51
@JohnColeman So you mean the code is correct, but my words are not reasonable?
– james
Jan 19 at 3:51
@james I don't know if your code is correct. I didn't run it or think about it very deeply. Nothing jumps out at me. I just wanted to sound a note of caution to not read too much into the fact that the code seems to be working.
– John Coleman
Jan 19 at 4:17
@james I don't know if your code is correct. I didn't run it or think about it very deeply. Nothing jumps out at me. I just wanted to sound a note of caution to not read too much into the fact that the code seems to be working.
– John Coleman
Jan 19 at 4:17
add a comment |
1 Answer
1
active
oldest
votes
A pointer is simply a normal variable that holds the address of something else as its value. In other words, a pointer points to the address where something else can be found.
When you allocate:
c = calloc(2,sizeof(float *));
You are assigning the starting address for the new block of memory to c
. In other words c
points to the location in memory where the first (of two) pointers you allocated can be found.
When you assign (verb):
assign = c;
You are setting the value held by assign
to the value held by c
. (and what does c
hold? -- the address of the block of memory you allocated with calloc
). So assign
now holds the same address as c
, e.g. assign
now points to the first (of two) pointers you allocated. So assign
and c
now both hold the same address as their value and you can use either one to reference what is stored there.
note: there is no need to cast the return of malloc
, it is unnecessary. See: Do I cast the result of malloc?. Further, if you use the derefernced pointer to set the type-size for the allocation, you eliminate the chance of getting it wrong, e.g.
c = calloc (2, sizeof *c);
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%2f54263754%2fuse-the-2d-pointer-without-allocation%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
A pointer is simply a normal variable that holds the address of something else as its value. In other words, a pointer points to the address where something else can be found.
When you allocate:
c = calloc(2,sizeof(float *));
You are assigning the starting address for the new block of memory to c
. In other words c
points to the location in memory where the first (of two) pointers you allocated can be found.
When you assign (verb):
assign = c;
You are setting the value held by assign
to the value held by c
. (and what does c
hold? -- the address of the block of memory you allocated with calloc
). So assign
now holds the same address as c
, e.g. assign
now points to the first (of two) pointers you allocated. So assign
and c
now both hold the same address as their value and you can use either one to reference what is stored there.
note: there is no need to cast the return of malloc
, it is unnecessary. See: Do I cast the result of malloc?. Further, if you use the derefernced pointer to set the type-size for the allocation, you eliminate the chance of getting it wrong, e.g.
c = calloc (2, sizeof *c);
add a comment |
A pointer is simply a normal variable that holds the address of something else as its value. In other words, a pointer points to the address where something else can be found.
When you allocate:
c = calloc(2,sizeof(float *));
You are assigning the starting address for the new block of memory to c
. In other words c
points to the location in memory where the first (of two) pointers you allocated can be found.
When you assign (verb):
assign = c;
You are setting the value held by assign
to the value held by c
. (and what does c
hold? -- the address of the block of memory you allocated with calloc
). So assign
now holds the same address as c
, e.g. assign
now points to the first (of two) pointers you allocated. So assign
and c
now both hold the same address as their value and you can use either one to reference what is stored there.
note: there is no need to cast the return of malloc
, it is unnecessary. See: Do I cast the result of malloc?. Further, if you use the derefernced pointer to set the type-size for the allocation, you eliminate the chance of getting it wrong, e.g.
c = calloc (2, sizeof *c);
add a comment |
A pointer is simply a normal variable that holds the address of something else as its value. In other words, a pointer points to the address where something else can be found.
When you allocate:
c = calloc(2,sizeof(float *));
You are assigning the starting address for the new block of memory to c
. In other words c
points to the location in memory where the first (of two) pointers you allocated can be found.
When you assign (verb):
assign = c;
You are setting the value held by assign
to the value held by c
. (and what does c
hold? -- the address of the block of memory you allocated with calloc
). So assign
now holds the same address as c
, e.g. assign
now points to the first (of two) pointers you allocated. So assign
and c
now both hold the same address as their value and you can use either one to reference what is stored there.
note: there is no need to cast the return of malloc
, it is unnecessary. See: Do I cast the result of malloc?. Further, if you use the derefernced pointer to set the type-size for the allocation, you eliminate the chance of getting it wrong, e.g.
c = calloc (2, sizeof *c);
A pointer is simply a normal variable that holds the address of something else as its value. In other words, a pointer points to the address where something else can be found.
When you allocate:
c = calloc(2,sizeof(float *));
You are assigning the starting address for the new block of memory to c
. In other words c
points to the location in memory where the first (of two) pointers you allocated can be found.
When you assign (verb):
assign = c;
You are setting the value held by assign
to the value held by c
. (and what does c
hold? -- the address of the block of memory you allocated with calloc
). So assign
now holds the same address as c
, e.g. assign
now points to the first (of two) pointers you allocated. So assign
and c
now both hold the same address as their value and you can use either one to reference what is stored there.
note: there is no need to cast the return of malloc
, it is unnecessary. See: Do I cast the result of malloc?. Further, if you use the derefernced pointer to set the type-size for the allocation, you eliminate the chance of getting it wrong, e.g.
c = calloc (2, sizeof *c);
answered Jan 19 at 4:51
David C. RankinDavid C. Rankin
41.3k32748
41.3k32748
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%2f54263754%2fuse-the-2d-pointer-without-allocation%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
c
andassign
are pointers, not arrays. Code is simply assigning a pointer withassign = c;
.– chux
Jan 19 at 3:18
1
Two pointers can point to the same memory. As long as the memory being pointed to has been properly allocated this is perfectly safe. You don't have to allocate separately for each pointer. Having said that, note that in C saying that "the code runs successfully" is not always proof that the code is correct. Undefined behavior often looks like code that "runs successfully" most of the time.
– John Coleman
Jan 19 at 3:20
@JohnColeman So you mean the code is correct, but my words are not reasonable?
– james
Jan 19 at 3:51
@james I don't know if your code is correct. I didn't run it or think about it very deeply. Nothing jumps out at me. I just wanted to sound a note of caution to not read too much into the fact that the code seems to be working.
– John Coleman
Jan 19 at 4:17