Pointer to Pointer as function parameter for a getter
Given some struct:
struct Foo {
// ...
};
I want to use a function from a lib with the following signature:
void getAllFoos(Foo** foos);
Basically, the function should return a vector of all Foos, but someone decided to use the pointer-to-pointer for the purpose of returning something. I have to give some sort of empty Foo** to the function, so it can be filled.
How do I initialize foos
when I want to call that function? Will a "null-pointer" (Foo**) be sufficient, or do I have to get the amount of foos first and malloc enough memory myself (how do I do that for a pointer-to-pointer?)? And who is responsible for freeing the memory?
c++ pointers
|
show 5 more comments
Given some struct:
struct Foo {
// ...
};
I want to use a function from a lib with the following signature:
void getAllFoos(Foo** foos);
Basically, the function should return a vector of all Foos, but someone decided to use the pointer-to-pointer for the purpose of returning something. I have to give some sort of empty Foo** to the function, so it can be filled.
How do I initialize foos
when I want to call that function? Will a "null-pointer" (Foo**) be sufficient, or do I have to get the amount of foos first and malloc enough memory myself (how do I do that for a pointer-to-pointer?)? And who is responsible for freeing the memory?
c++ pointers
A vector and pointer to pointer are not compatible, do you mean an array ofFoos
?
– Rishikesh Raje
Nov 21 '18 at 8:35
How do you know how many Foos return? How about C++ list/vector and shared pointers ?
– tunglt
Nov 21 '18 at 8:36
The signaturevoid getAllFoos(Foo** foos);
suggests, that the function wants the address of aFoo*
so it can allocate memory for the user:Foo *foos; getAllFoos(&foos);
– Swordfish
Nov 21 '18 at 8:37
3
Please limit your question to *one* language. C *or* C++. In C the function declaration as shown is a syntax error.
– Swordfish
Nov 21 '18 at 8:39
1
WhatgetAllFoos
is doing exactly? Returns already existing array of pointers or creates it or copy to it? Why the array length is not specified as a second parameter?
– serge
Nov 21 '18 at 10:45
|
show 5 more comments
Given some struct:
struct Foo {
// ...
};
I want to use a function from a lib with the following signature:
void getAllFoos(Foo** foos);
Basically, the function should return a vector of all Foos, but someone decided to use the pointer-to-pointer for the purpose of returning something. I have to give some sort of empty Foo** to the function, so it can be filled.
How do I initialize foos
when I want to call that function? Will a "null-pointer" (Foo**) be sufficient, or do I have to get the amount of foos first and malloc enough memory myself (how do I do that for a pointer-to-pointer?)? And who is responsible for freeing the memory?
c++ pointers
Given some struct:
struct Foo {
// ...
};
I want to use a function from a lib with the following signature:
void getAllFoos(Foo** foos);
Basically, the function should return a vector of all Foos, but someone decided to use the pointer-to-pointer for the purpose of returning something. I have to give some sort of empty Foo** to the function, so it can be filled.
How do I initialize foos
when I want to call that function? Will a "null-pointer" (Foo**) be sufficient, or do I have to get the amount of foos first and malloc enough memory myself (how do I do that for a pointer-to-pointer?)? And who is responsible for freeing the memory?
c++ pointers
c++ pointers
edited Nov 21 '18 at 9:23
Kackao
asked Nov 21 '18 at 8:31
KackaoKackao
3361414
3361414
A vector and pointer to pointer are not compatible, do you mean an array ofFoos
?
– Rishikesh Raje
Nov 21 '18 at 8:35
How do you know how many Foos return? How about C++ list/vector and shared pointers ?
– tunglt
Nov 21 '18 at 8:36
The signaturevoid getAllFoos(Foo** foos);
suggests, that the function wants the address of aFoo*
so it can allocate memory for the user:Foo *foos; getAllFoos(&foos);
– Swordfish
Nov 21 '18 at 8:37
3
Please limit your question to *one* language. C *or* C++. In C the function declaration as shown is a syntax error.
– Swordfish
Nov 21 '18 at 8:39
1
WhatgetAllFoos
is doing exactly? Returns already existing array of pointers or creates it or copy to it? Why the array length is not specified as a second parameter?
– serge
Nov 21 '18 at 10:45
|
show 5 more comments
A vector and pointer to pointer are not compatible, do you mean an array ofFoos
?
– Rishikesh Raje
Nov 21 '18 at 8:35
How do you know how many Foos return? How about C++ list/vector and shared pointers ?
– tunglt
Nov 21 '18 at 8:36
The signaturevoid getAllFoos(Foo** foos);
suggests, that the function wants the address of aFoo*
so it can allocate memory for the user:Foo *foos; getAllFoos(&foos);
– Swordfish
Nov 21 '18 at 8:37
3
Please limit your question to *one* language. C *or* C++. In C the function declaration as shown is a syntax error.
– Swordfish
Nov 21 '18 at 8:39
1
WhatgetAllFoos
is doing exactly? Returns already existing array of pointers or creates it or copy to it? Why the array length is not specified as a second parameter?
– serge
Nov 21 '18 at 10:45
A vector and pointer to pointer are not compatible, do you mean an array of
Foos
?– Rishikesh Raje
Nov 21 '18 at 8:35
A vector and pointer to pointer are not compatible, do you mean an array of
Foos
?– Rishikesh Raje
Nov 21 '18 at 8:35
How do you know how many Foos return? How about C++ list/vector and shared pointers ?
– tunglt
Nov 21 '18 at 8:36
How do you know how many Foos return? How about C++ list/vector and shared pointers ?
– tunglt
Nov 21 '18 at 8:36
The signature
void getAllFoos(Foo** foos);
suggests, that the function wants the address of a Foo*
so it can allocate memory for the user: Foo *foos; getAllFoos(&foos);
– Swordfish
Nov 21 '18 at 8:37
The signature
void getAllFoos(Foo** foos);
suggests, that the function wants the address of a Foo*
so it can allocate memory for the user: Foo *foos; getAllFoos(&foos);
– Swordfish
Nov 21 '18 at 8:37
3
3
Please limit your question to *one* language. C *or* C++. In C the function declaration as shown is a syntax error.
– Swordfish
Nov 21 '18 at 8:39
Please limit your question to *one* language. C *or* C++. In C the function declaration as shown is a syntax error.
– Swordfish
Nov 21 '18 at 8:39
1
1
What
getAllFoos
is doing exactly? Returns already existing array of pointers or creates it or copy to it? Why the array length is not specified as a second parameter?– serge
Nov 21 '18 at 10:45
What
getAllFoos
is doing exactly? Returns already existing array of pointers or creates it or copy to it? Why the array length is not specified as a second parameter?– serge
Nov 21 '18 at 10:45
|
show 5 more comments
3 Answers
3
active
oldest
votes
foos
does not need to be initialized if getAllFoos
can be assumed to work something like this -- it will initialize foos
it for you:
void getAllFoos(Foo** pfoos) {
Foo* the_pointer_to_all_the_foos;
// (1) maybe the_pointer_to_all_the_foos = malloc(10 * sizeof(Foo));
// (2) maybe the_pointer_to_all_the_foos = new Foo[10];
// (3) maybe the_pointer_to_all_the_foos is a static Foo[10]
*pfoos = the_pointer_to_all_the_foos;
}
With the API above, there is no reason for getallFoos
to read the value of pfoos
.
To use it:
// in C, you should write: struct Foo* foos;
Foo* foos;
getAllFoos(&foos); // note: foos is Foo*, pfoos is Foo**
In general, the API documentation for getAllFoos
must document what you need to do with foos
in the end. Depending on what the code does [(1)
, (2)
, (3)
from above or something else], you might need to free(foos)
, delete foos
, do nothing, or do something else. We cannot answer that with the information given in the question.
add a comment |
You need to know in advance how many Foo
's you're going to get. There is no getting around this if you get all the Foo
s at once, and this is hard-wired in the interface. What you can do, though, if you're responsible for allocating memory, is to use a std::vector
as a recipient, and give its data()
member as an argument:
std::vector<Foo> flexible_buffer(Foo_nb); // or reserve later
getAllFoos(&(flexible_buffer.data());
if the function allocates memory somehow and tries to modify the pointer you pass it all hell breaks loose
– Swordfish
Nov 21 '18 at 8:45
You need to know in advance how many Foo's you're going to get. There is no getting around this Yes, there is. But without knowing which function of which library that is, its all guesswork.
– Swordfish
Nov 21 '18 at 8:53
add a comment |
You should call that function by passing reference of pointer of type struct Foo
.
struct Foo *foo = NULL;
getAllFoos(&foo);
You can free foo
as below
free(foo); //Assuming getAllFoos allocated the memory and filled.
Will a "null-pointer" (Foo**) be sufficient
No FOO *
should point to valid memory.
struct Foo **foo = NULL;
getAllFoos(foo);
If getAllFoos
tries to dereference foo
You will have undefined behavior.
1
@DavidC.Rankin I believe what kiranBiradar tried to say with FOO * should point to valid memory. is that the parameterfoo
derecerenced once shouldn't beNULL
.
– Swordfish
Nov 21 '18 at 8:47
Maybe you can reformulate your answer so it is clearer. Also, passing reference is a little misleading.&foo
takes the address offoo
and passes it as a value togetAllFoos()
. No references involved.
– Swordfish
Nov 21 '18 at 8:50
So the answer was to simply say that "If it was NULL and the function dreferenced it -- then that would cause problems? I'll give it the benefit of the doubt, but I read the answer so say "NO you can't pass*foos
as aNULL
pointer. So I guess at best it was unclear either way. (I didn't ding for it -- so no harm there)
– David C. Rankin
Nov 21 '18 at 8:51
2
I see it the other way now, thanks @Swordfish, above the horizontal he is talking aboutstruct Foo *foo = NULL;
, below he is taking about an initial setting ofstruct Foo **foo = NULL;
(and that is presumably in the caller) and then yes, I agree in part, NOT that "FOO * should point to valid memory." but that FOO * should have a valid address." (i.e. that it is a valid declared pointer itself)
– David C. Rankin
Nov 21 '18 at 8:55
All we came up with is speculation about OPs function at best. For all we know the function could set the passed pointer to the first node of a linked list.
– Swordfish
Nov 21 '18 at 8:59
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%2f53407957%2fpointer-to-pointer-as-function-parameter-for-a-getter%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
foos
does not need to be initialized if getAllFoos
can be assumed to work something like this -- it will initialize foos
it for you:
void getAllFoos(Foo** pfoos) {
Foo* the_pointer_to_all_the_foos;
// (1) maybe the_pointer_to_all_the_foos = malloc(10 * sizeof(Foo));
// (2) maybe the_pointer_to_all_the_foos = new Foo[10];
// (3) maybe the_pointer_to_all_the_foos is a static Foo[10]
*pfoos = the_pointer_to_all_the_foos;
}
With the API above, there is no reason for getallFoos
to read the value of pfoos
.
To use it:
// in C, you should write: struct Foo* foos;
Foo* foos;
getAllFoos(&foos); // note: foos is Foo*, pfoos is Foo**
In general, the API documentation for getAllFoos
must document what you need to do with foos
in the end. Depending on what the code does [(1)
, (2)
, (3)
from above or something else], you might need to free(foos)
, delete foos
, do nothing, or do something else. We cannot answer that with the information given in the question.
add a comment |
foos
does not need to be initialized if getAllFoos
can be assumed to work something like this -- it will initialize foos
it for you:
void getAllFoos(Foo** pfoos) {
Foo* the_pointer_to_all_the_foos;
// (1) maybe the_pointer_to_all_the_foos = malloc(10 * sizeof(Foo));
// (2) maybe the_pointer_to_all_the_foos = new Foo[10];
// (3) maybe the_pointer_to_all_the_foos is a static Foo[10]
*pfoos = the_pointer_to_all_the_foos;
}
With the API above, there is no reason for getallFoos
to read the value of pfoos
.
To use it:
// in C, you should write: struct Foo* foos;
Foo* foos;
getAllFoos(&foos); // note: foos is Foo*, pfoos is Foo**
In general, the API documentation for getAllFoos
must document what you need to do with foos
in the end. Depending on what the code does [(1)
, (2)
, (3)
from above or something else], you might need to free(foos)
, delete foos
, do nothing, or do something else. We cannot answer that with the information given in the question.
add a comment |
foos
does not need to be initialized if getAllFoos
can be assumed to work something like this -- it will initialize foos
it for you:
void getAllFoos(Foo** pfoos) {
Foo* the_pointer_to_all_the_foos;
// (1) maybe the_pointer_to_all_the_foos = malloc(10 * sizeof(Foo));
// (2) maybe the_pointer_to_all_the_foos = new Foo[10];
// (3) maybe the_pointer_to_all_the_foos is a static Foo[10]
*pfoos = the_pointer_to_all_the_foos;
}
With the API above, there is no reason for getallFoos
to read the value of pfoos
.
To use it:
// in C, you should write: struct Foo* foos;
Foo* foos;
getAllFoos(&foos); // note: foos is Foo*, pfoos is Foo**
In general, the API documentation for getAllFoos
must document what you need to do with foos
in the end. Depending on what the code does [(1)
, (2)
, (3)
from above or something else], you might need to free(foos)
, delete foos
, do nothing, or do something else. We cannot answer that with the information given in the question.
foos
does not need to be initialized if getAllFoos
can be assumed to work something like this -- it will initialize foos
it for you:
void getAllFoos(Foo** pfoos) {
Foo* the_pointer_to_all_the_foos;
// (1) maybe the_pointer_to_all_the_foos = malloc(10 * sizeof(Foo));
// (2) maybe the_pointer_to_all_the_foos = new Foo[10];
// (3) maybe the_pointer_to_all_the_foos is a static Foo[10]
*pfoos = the_pointer_to_all_the_foos;
}
With the API above, there is no reason for getallFoos
to read the value of pfoos
.
To use it:
// in C, you should write: struct Foo* foos;
Foo* foos;
getAllFoos(&foos); // note: foos is Foo*, pfoos is Foo**
In general, the API documentation for getAllFoos
must document what you need to do with foos
in the end. Depending on what the code does [(1)
, (2)
, (3)
from above or something else], you might need to free(foos)
, delete foos
, do nothing, or do something else. We cannot answer that with the information given in the question.
edited Nov 21 '18 at 10:25
answered Nov 21 '18 at 9:00
palotasbpalotasb
2,37111420
2,37111420
add a comment |
add a comment |
You need to know in advance how many Foo
's you're going to get. There is no getting around this if you get all the Foo
s at once, and this is hard-wired in the interface. What you can do, though, if you're responsible for allocating memory, is to use a std::vector
as a recipient, and give its data()
member as an argument:
std::vector<Foo> flexible_buffer(Foo_nb); // or reserve later
getAllFoos(&(flexible_buffer.data());
if the function allocates memory somehow and tries to modify the pointer you pass it all hell breaks loose
– Swordfish
Nov 21 '18 at 8:45
You need to know in advance how many Foo's you're going to get. There is no getting around this Yes, there is. But without knowing which function of which library that is, its all guesswork.
– Swordfish
Nov 21 '18 at 8:53
add a comment |
You need to know in advance how many Foo
's you're going to get. There is no getting around this if you get all the Foo
s at once, and this is hard-wired in the interface. What you can do, though, if you're responsible for allocating memory, is to use a std::vector
as a recipient, and give its data()
member as an argument:
std::vector<Foo> flexible_buffer(Foo_nb); // or reserve later
getAllFoos(&(flexible_buffer.data());
if the function allocates memory somehow and tries to modify the pointer you pass it all hell breaks loose
– Swordfish
Nov 21 '18 at 8:45
You need to know in advance how many Foo's you're going to get. There is no getting around this Yes, there is. But without knowing which function of which library that is, its all guesswork.
– Swordfish
Nov 21 '18 at 8:53
add a comment |
You need to know in advance how many Foo
's you're going to get. There is no getting around this if you get all the Foo
s at once, and this is hard-wired in the interface. What you can do, though, if you're responsible for allocating memory, is to use a std::vector
as a recipient, and give its data()
member as an argument:
std::vector<Foo> flexible_buffer(Foo_nb); // or reserve later
getAllFoos(&(flexible_buffer.data());
You need to know in advance how many Foo
's you're going to get. There is no getting around this if you get all the Foo
s at once, and this is hard-wired in the interface. What you can do, though, if you're responsible for allocating memory, is to use a std::vector
as a recipient, and give its data()
member as an argument:
std::vector<Foo> flexible_buffer(Foo_nb); // or reserve later
getAllFoos(&(flexible_buffer.data());
answered Nov 21 '18 at 8:38
papagagapapagaga
507311
507311
if the function allocates memory somehow and tries to modify the pointer you pass it all hell breaks loose
– Swordfish
Nov 21 '18 at 8:45
You need to know in advance how many Foo's you're going to get. There is no getting around this Yes, there is. But without knowing which function of which library that is, its all guesswork.
– Swordfish
Nov 21 '18 at 8:53
add a comment |
if the function allocates memory somehow and tries to modify the pointer you pass it all hell breaks loose
– Swordfish
Nov 21 '18 at 8:45
You need to know in advance how many Foo's you're going to get. There is no getting around this Yes, there is. But without knowing which function of which library that is, its all guesswork.
– Swordfish
Nov 21 '18 at 8:53
if the function allocates memory somehow and tries to modify the pointer you pass it all hell breaks loose
– Swordfish
Nov 21 '18 at 8:45
if the function allocates memory somehow and tries to modify the pointer you pass it all hell breaks loose
– Swordfish
Nov 21 '18 at 8:45
You need to know in advance how many Foo's you're going to get. There is no getting around this Yes, there is. But without knowing which function of which library that is, its all guesswork.
– Swordfish
Nov 21 '18 at 8:53
You need to know in advance how many Foo's you're going to get. There is no getting around this Yes, there is. But without knowing which function of which library that is, its all guesswork.
– Swordfish
Nov 21 '18 at 8:53
add a comment |
You should call that function by passing reference of pointer of type struct Foo
.
struct Foo *foo = NULL;
getAllFoos(&foo);
You can free foo
as below
free(foo); //Assuming getAllFoos allocated the memory and filled.
Will a "null-pointer" (Foo**) be sufficient
No FOO *
should point to valid memory.
struct Foo **foo = NULL;
getAllFoos(foo);
If getAllFoos
tries to dereference foo
You will have undefined behavior.
1
@DavidC.Rankin I believe what kiranBiradar tried to say with FOO * should point to valid memory. is that the parameterfoo
derecerenced once shouldn't beNULL
.
– Swordfish
Nov 21 '18 at 8:47
Maybe you can reformulate your answer so it is clearer. Also, passing reference is a little misleading.&foo
takes the address offoo
and passes it as a value togetAllFoos()
. No references involved.
– Swordfish
Nov 21 '18 at 8:50
So the answer was to simply say that "If it was NULL and the function dreferenced it -- then that would cause problems? I'll give it the benefit of the doubt, but I read the answer so say "NO you can't pass*foos
as aNULL
pointer. So I guess at best it was unclear either way. (I didn't ding for it -- so no harm there)
– David C. Rankin
Nov 21 '18 at 8:51
2
I see it the other way now, thanks @Swordfish, above the horizontal he is talking aboutstruct Foo *foo = NULL;
, below he is taking about an initial setting ofstruct Foo **foo = NULL;
(and that is presumably in the caller) and then yes, I agree in part, NOT that "FOO * should point to valid memory." but that FOO * should have a valid address." (i.e. that it is a valid declared pointer itself)
– David C. Rankin
Nov 21 '18 at 8:55
All we came up with is speculation about OPs function at best. For all we know the function could set the passed pointer to the first node of a linked list.
– Swordfish
Nov 21 '18 at 8:59
add a comment |
You should call that function by passing reference of pointer of type struct Foo
.
struct Foo *foo = NULL;
getAllFoos(&foo);
You can free foo
as below
free(foo); //Assuming getAllFoos allocated the memory and filled.
Will a "null-pointer" (Foo**) be sufficient
No FOO *
should point to valid memory.
struct Foo **foo = NULL;
getAllFoos(foo);
If getAllFoos
tries to dereference foo
You will have undefined behavior.
1
@DavidC.Rankin I believe what kiranBiradar tried to say with FOO * should point to valid memory. is that the parameterfoo
derecerenced once shouldn't beNULL
.
– Swordfish
Nov 21 '18 at 8:47
Maybe you can reformulate your answer so it is clearer. Also, passing reference is a little misleading.&foo
takes the address offoo
and passes it as a value togetAllFoos()
. No references involved.
– Swordfish
Nov 21 '18 at 8:50
So the answer was to simply say that "If it was NULL and the function dreferenced it -- then that would cause problems? I'll give it the benefit of the doubt, but I read the answer so say "NO you can't pass*foos
as aNULL
pointer. So I guess at best it was unclear either way. (I didn't ding for it -- so no harm there)
– David C. Rankin
Nov 21 '18 at 8:51
2
I see it the other way now, thanks @Swordfish, above the horizontal he is talking aboutstruct Foo *foo = NULL;
, below he is taking about an initial setting ofstruct Foo **foo = NULL;
(and that is presumably in the caller) and then yes, I agree in part, NOT that "FOO * should point to valid memory." but that FOO * should have a valid address." (i.e. that it is a valid declared pointer itself)
– David C. Rankin
Nov 21 '18 at 8:55
All we came up with is speculation about OPs function at best. For all we know the function could set the passed pointer to the first node of a linked list.
– Swordfish
Nov 21 '18 at 8:59
add a comment |
You should call that function by passing reference of pointer of type struct Foo
.
struct Foo *foo = NULL;
getAllFoos(&foo);
You can free foo
as below
free(foo); //Assuming getAllFoos allocated the memory and filled.
Will a "null-pointer" (Foo**) be sufficient
No FOO *
should point to valid memory.
struct Foo **foo = NULL;
getAllFoos(foo);
If getAllFoos
tries to dereference foo
You will have undefined behavior.
You should call that function by passing reference of pointer of type struct Foo
.
struct Foo *foo = NULL;
getAllFoos(&foo);
You can free foo
as below
free(foo); //Assuming getAllFoos allocated the memory and filled.
Will a "null-pointer" (Foo**) be sufficient
No FOO *
should point to valid memory.
struct Foo **foo = NULL;
getAllFoos(foo);
If getAllFoos
tries to dereference foo
You will have undefined behavior.
edited Nov 21 '18 at 9:00
answered Nov 21 '18 at 8:37
kiran Biradarkiran Biradar
5,5122927
5,5122927
1
@DavidC.Rankin I believe what kiranBiradar tried to say with FOO * should point to valid memory. is that the parameterfoo
derecerenced once shouldn't beNULL
.
– Swordfish
Nov 21 '18 at 8:47
Maybe you can reformulate your answer so it is clearer. Also, passing reference is a little misleading.&foo
takes the address offoo
and passes it as a value togetAllFoos()
. No references involved.
– Swordfish
Nov 21 '18 at 8:50
So the answer was to simply say that "If it was NULL and the function dreferenced it -- then that would cause problems? I'll give it the benefit of the doubt, but I read the answer so say "NO you can't pass*foos
as aNULL
pointer. So I guess at best it was unclear either way. (I didn't ding for it -- so no harm there)
– David C. Rankin
Nov 21 '18 at 8:51
2
I see it the other way now, thanks @Swordfish, above the horizontal he is talking aboutstruct Foo *foo = NULL;
, below he is taking about an initial setting ofstruct Foo **foo = NULL;
(and that is presumably in the caller) and then yes, I agree in part, NOT that "FOO * should point to valid memory." but that FOO * should have a valid address." (i.e. that it is a valid declared pointer itself)
– David C. Rankin
Nov 21 '18 at 8:55
All we came up with is speculation about OPs function at best. For all we know the function could set the passed pointer to the first node of a linked list.
– Swordfish
Nov 21 '18 at 8:59
add a comment |
1
@DavidC.Rankin I believe what kiranBiradar tried to say with FOO * should point to valid memory. is that the parameterfoo
derecerenced once shouldn't beNULL
.
– Swordfish
Nov 21 '18 at 8:47
Maybe you can reformulate your answer so it is clearer. Also, passing reference is a little misleading.&foo
takes the address offoo
and passes it as a value togetAllFoos()
. No references involved.
– Swordfish
Nov 21 '18 at 8:50
So the answer was to simply say that "If it was NULL and the function dreferenced it -- then that would cause problems? I'll give it the benefit of the doubt, but I read the answer so say "NO you can't pass*foos
as aNULL
pointer. So I guess at best it was unclear either way. (I didn't ding for it -- so no harm there)
– David C. Rankin
Nov 21 '18 at 8:51
2
I see it the other way now, thanks @Swordfish, above the horizontal he is talking aboutstruct Foo *foo = NULL;
, below he is taking about an initial setting ofstruct Foo **foo = NULL;
(and that is presumably in the caller) and then yes, I agree in part, NOT that "FOO * should point to valid memory." but that FOO * should have a valid address." (i.e. that it is a valid declared pointer itself)
– David C. Rankin
Nov 21 '18 at 8:55
All we came up with is speculation about OPs function at best. For all we know the function could set the passed pointer to the first node of a linked list.
– Swordfish
Nov 21 '18 at 8:59
1
1
@DavidC.Rankin I believe what kiranBiradar tried to say with FOO * should point to valid memory. is that the parameter
foo
derecerenced once shouldn't be NULL
.– Swordfish
Nov 21 '18 at 8:47
@DavidC.Rankin I believe what kiranBiradar tried to say with FOO * should point to valid memory. is that the parameter
foo
derecerenced once shouldn't be NULL
.– Swordfish
Nov 21 '18 at 8:47
Maybe you can reformulate your answer so it is clearer. Also, passing reference is a little misleading.
&foo
takes the address of foo
and passes it as a value to getAllFoos()
. No references involved.– Swordfish
Nov 21 '18 at 8:50
Maybe you can reformulate your answer so it is clearer. Also, passing reference is a little misleading.
&foo
takes the address of foo
and passes it as a value to getAllFoos()
. No references involved.– Swordfish
Nov 21 '18 at 8:50
So the answer was to simply say that "If it was NULL and the function dreferenced it -- then that would cause problems? I'll give it the benefit of the doubt, but I read the answer so say "NO you can't pass
*foos
as a NULL
pointer. So I guess at best it was unclear either way. (I didn't ding for it -- so no harm there)– David C. Rankin
Nov 21 '18 at 8:51
So the answer was to simply say that "If it was NULL and the function dreferenced it -- then that would cause problems? I'll give it the benefit of the doubt, but I read the answer so say "NO you can't pass
*foos
as a NULL
pointer. So I guess at best it was unclear either way. (I didn't ding for it -- so no harm there)– David C. Rankin
Nov 21 '18 at 8:51
2
2
I see it the other way now, thanks @Swordfish, above the horizontal he is talking about
struct Foo *foo = NULL;
, below he is taking about an initial setting of struct Foo **foo = NULL;
(and that is presumably in the caller) and then yes, I agree in part, NOT that "FOO * should point to valid memory." but that FOO * should have a valid address." (i.e. that it is a valid declared pointer itself)– David C. Rankin
Nov 21 '18 at 8:55
I see it the other way now, thanks @Swordfish, above the horizontal he is talking about
struct Foo *foo = NULL;
, below he is taking about an initial setting of struct Foo **foo = NULL;
(and that is presumably in the caller) and then yes, I agree in part, NOT that "FOO * should point to valid memory." but that FOO * should have a valid address." (i.e. that it is a valid declared pointer itself)– David C. Rankin
Nov 21 '18 at 8:55
All we came up with is speculation about OPs function at best. For all we know the function could set the passed pointer to the first node of a linked list.
– Swordfish
Nov 21 '18 at 8:59
All we came up with is speculation about OPs function at best. For all we know the function could set the passed pointer to the first node of a linked list.
– Swordfish
Nov 21 '18 at 8:59
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%2f53407957%2fpointer-to-pointer-as-function-parameter-for-a-getter%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 vector and pointer to pointer are not compatible, do you mean an array of
Foos
?– Rishikesh Raje
Nov 21 '18 at 8:35
How do you know how many Foos return? How about C++ list/vector and shared pointers ?
– tunglt
Nov 21 '18 at 8:36
The signature
void getAllFoos(Foo** foos);
suggests, that the function wants the address of aFoo*
so it can allocate memory for the user:Foo *foos; getAllFoos(&foos);
– Swordfish
Nov 21 '18 at 8:37
3
Please limit your question to *one* language. C *or* C++. In C the function declaration as shown is a syntax error.
– Swordfish
Nov 21 '18 at 8:39
1
What
getAllFoos
is doing exactly? Returns already existing array of pointers or creates it or copy to it? Why the array length is not specified as a second parameter?– serge
Nov 21 '18 at 10:45