How to pass vector to pointer array winapi?
I want to stop using manual allocated memory to work with all winapi. for testing i try to change my code. but i get
no suitable conversion from vector DWORD to DWORD * exists.
std::vector<DWORD> aProcesses;
DWORD cbNeeded;
if (!EnumProcesses(aProcesses, aProcesses.capacity(), &cbNeeded))
{
return 1;
}
for (auto& p : aProcesses)
{
std::cout << p << std::endl;
}
Manual Allocated memory below
DWORD aProcesses[1024], cbNeeded, cProcesses;
unsigned int i;
if (!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded))
{
return 1;
}
// Calculate how many process identifiers were returned.
cProcesses = cbNeeded / sizeof(DWORD);
// Print the name and process identifier for each process.
for (i = 0; i < cProcesses; i++)
{
if (aProcesses[i] != 0)
{
std::cout << aProcesses[i] << std::endl;
}
c++ windows visual-studio winapi
|
show 3 more comments
I want to stop using manual allocated memory to work with all winapi. for testing i try to change my code. but i get
no suitable conversion from vector DWORD to DWORD * exists.
std::vector<DWORD> aProcesses;
DWORD cbNeeded;
if (!EnumProcesses(aProcesses, aProcesses.capacity(), &cbNeeded))
{
return 1;
}
for (auto& p : aProcesses)
{
std::cout << p << std::endl;
}
Manual Allocated memory below
DWORD aProcesses[1024], cbNeeded, cProcesses;
unsigned int i;
if (!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded))
{
return 1;
}
// Calculate how many process identifiers were returned.
cProcesses = cbNeeded / sizeof(DWORD);
// Print the name and process identifier for each process.
for (i = 0; i < cProcesses; i++)
{
if (aProcesses[i] != 0)
{
std::cout << aProcesses[i] << std::endl;
}
c++ windows visual-studio winapi
2
instead of aProcesses` useaProcesses.data()
or alternately&aProcesses[0]
see: en.cppreference.com/w/cpp/container/vector/data
– doug
Nov 15 '18 at 4:48
1
Note that you need to callresize()
on the vector yourself before passing a pointer to the data buffer to an API function, since Windows doesn't know anything about c++.
– Jonathan Potter
Nov 15 '18 at 4:51
It would be helpful to include the code you anticipate replacing to provide more specific advice.
– doug
Nov 15 '18 at 4:58
@doug i include manual allocate memory ways
– Dwerson busch
Nov 15 '18 at 5:04
Just declare your vector like so:std::vector<DWORD> aProcesses(1024);
and useaProcesses.data()
which is a pointer to the first vector element.
– doug
Nov 15 '18 at 5:08
|
show 3 more comments
I want to stop using manual allocated memory to work with all winapi. for testing i try to change my code. but i get
no suitable conversion from vector DWORD to DWORD * exists.
std::vector<DWORD> aProcesses;
DWORD cbNeeded;
if (!EnumProcesses(aProcesses, aProcesses.capacity(), &cbNeeded))
{
return 1;
}
for (auto& p : aProcesses)
{
std::cout << p << std::endl;
}
Manual Allocated memory below
DWORD aProcesses[1024], cbNeeded, cProcesses;
unsigned int i;
if (!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded))
{
return 1;
}
// Calculate how many process identifiers were returned.
cProcesses = cbNeeded / sizeof(DWORD);
// Print the name and process identifier for each process.
for (i = 0; i < cProcesses; i++)
{
if (aProcesses[i] != 0)
{
std::cout << aProcesses[i] << std::endl;
}
c++ windows visual-studio winapi
I want to stop using manual allocated memory to work with all winapi. for testing i try to change my code. but i get
no suitable conversion from vector DWORD to DWORD * exists.
std::vector<DWORD> aProcesses;
DWORD cbNeeded;
if (!EnumProcesses(aProcesses, aProcesses.capacity(), &cbNeeded))
{
return 1;
}
for (auto& p : aProcesses)
{
std::cout << p << std::endl;
}
Manual Allocated memory below
DWORD aProcesses[1024], cbNeeded, cProcesses;
unsigned int i;
if (!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded))
{
return 1;
}
// Calculate how many process identifiers were returned.
cProcesses = cbNeeded / sizeof(DWORD);
// Print the name and process identifier for each process.
for (i = 0; i < cProcesses; i++)
{
if (aProcesses[i] != 0)
{
std::cout << aProcesses[i] << std::endl;
}
c++ windows visual-studio winapi
c++ windows visual-studio winapi
edited Nov 15 '18 at 5:02
Dwerson busch
asked Nov 15 '18 at 4:43
Dwerson buschDwerson busch
64
64
2
instead of aProcesses` useaProcesses.data()
or alternately&aProcesses[0]
see: en.cppreference.com/w/cpp/container/vector/data
– doug
Nov 15 '18 at 4:48
1
Note that you need to callresize()
on the vector yourself before passing a pointer to the data buffer to an API function, since Windows doesn't know anything about c++.
– Jonathan Potter
Nov 15 '18 at 4:51
It would be helpful to include the code you anticipate replacing to provide more specific advice.
– doug
Nov 15 '18 at 4:58
@doug i include manual allocate memory ways
– Dwerson busch
Nov 15 '18 at 5:04
Just declare your vector like so:std::vector<DWORD> aProcesses(1024);
and useaProcesses.data()
which is a pointer to the first vector element.
– doug
Nov 15 '18 at 5:08
|
show 3 more comments
2
instead of aProcesses` useaProcesses.data()
or alternately&aProcesses[0]
see: en.cppreference.com/w/cpp/container/vector/data
– doug
Nov 15 '18 at 4:48
1
Note that you need to callresize()
on the vector yourself before passing a pointer to the data buffer to an API function, since Windows doesn't know anything about c++.
– Jonathan Potter
Nov 15 '18 at 4:51
It would be helpful to include the code you anticipate replacing to provide more specific advice.
– doug
Nov 15 '18 at 4:58
@doug i include manual allocate memory ways
– Dwerson busch
Nov 15 '18 at 5:04
Just declare your vector like so:std::vector<DWORD> aProcesses(1024);
and useaProcesses.data()
which is a pointer to the first vector element.
– doug
Nov 15 '18 at 5:08
2
2
instead of aProcesses` use
aProcesses.data()
or alternately &aProcesses[0]
see: en.cppreference.com/w/cpp/container/vector/data– doug
Nov 15 '18 at 4:48
instead of aProcesses` use
aProcesses.data()
or alternately &aProcesses[0]
see: en.cppreference.com/w/cpp/container/vector/data– doug
Nov 15 '18 at 4:48
1
1
Note that you need to call
resize()
on the vector yourself before passing a pointer to the data buffer to an API function, since Windows doesn't know anything about c++.– Jonathan Potter
Nov 15 '18 at 4:51
Note that you need to call
resize()
on the vector yourself before passing a pointer to the data buffer to an API function, since Windows doesn't know anything about c++.– Jonathan Potter
Nov 15 '18 at 4:51
It would be helpful to include the code you anticipate replacing to provide more specific advice.
– doug
Nov 15 '18 at 4:58
It would be helpful to include the code you anticipate replacing to provide more specific advice.
– doug
Nov 15 '18 at 4:58
@doug i include manual allocate memory ways
– Dwerson busch
Nov 15 '18 at 5:04
@doug i include manual allocate memory ways
– Dwerson busch
Nov 15 '18 at 5:04
Just declare your vector like so:
std::vector<DWORD> aProcesses(1024);
and use aProcesses.data()
which is a pointer to the first vector element.– doug
Nov 15 '18 at 5:08
Just declare your vector like so:
std::vector<DWORD> aProcesses(1024);
and use aProcesses.data()
which is a pointer to the first vector element.– doug
Nov 15 '18 at 5:08
|
show 3 more comments
2 Answers
2
active
oldest
votes
if (!EnumProcesses(aProcesses, aProcesses.capacity(), &cbNeeded))
won't work well. First thing is, you need to pass aProcesses.data()
as the first parameter and the number of *bytes* aProcess
can hold as the 2nd: aProcesses.size() * sizeof(DOWD)
.
If cbNeeded
>= aProcess.size() * sizeof(DWORD)
after the call, you have to resize the vector
to cbNeeded / sizeof(DWORD)
and call EnumProcesses()
again to make sure you got all processes.
if like that. is it call EnumProcesses twice. it make execution slow?
– Dwerson busch
Nov 15 '18 at 5:20
You have no other choice if you don't pass an array that is big enough the first time you callEnumProcesses()
.
– Swordfish
Nov 15 '18 at 5:44
add a comment |
A vector
isn't a pointer to DWORD
; however, this vector
happens to contain one so you can
if (!EnumProcesses(aProcesses.data(), aProcesses.capacity(), &cbNeeded))
if your compiler is compiling for the C++11 Standard revision or more recent or
if (!EnumProcesses(&aProcesses[0], aProcesses.capacity(), &cbNeeded))
if it is not.
But that just gets things compiling
There are logic problems yet to be dealt with.
const int MAX_PROCESSES = 1024; // very few Windows systems will exceed 1024 processes
std::vector<DWORD> aProcesses(MAX_PROCESSES); // allocate space
DWORD cbNeeded;
if (!EnumProcesses(aProcesses.data(),
aProcesses.capacity() * sizeof(DWORD), // size in bytes, not elements
&cbNeeded))
{
return 1;
}
//for (auto& p : aProcesses) Not all processes may have been filled. The unused portions
//are zeroed, so this is safe, but wasteful
for (int i = 0; i < cbNeeded / sizeof(DWORD); ++i)
{
std::cout << aProcesses[i] << std::endl;
}
is not MAX_NUM_DWORDS macro manually allocated memory? i should knows max possible the value?
– Dwerson busch
Nov 15 '18 at 5:06
@Dwersonbusch No, that was a stub. There is pretty much no way to know how many processes are running, but this rarely gets above a few hundred on all but the busiest of servers. Will update question.
– user4581301
Nov 15 '18 at 5:12
EnumProcesses api it just example. i knows if max processes will not run above 1k. but there's other winapi it will return above 1k value.
– Dwerson busch
Nov 15 '18 at 5:18
Why do you say that&aProcesses[0]
isn't legal?aProcesses[0]
returns a reference,T&
, and taking the address of a reference is just a pointer to the referenced object. A DWORD in this case.
– doug
Nov 15 '18 at 5:58
@doug I can't imagine how you could dovector
and theoverload so that
&vec[0]
won't work, but it's not guaranteed. It's only an issue with C++ 98 and 03, so I did some old school standard diving and in 2005 drafts the wording looks like obeys the identity &v[n] == &v[0] + n for all 0 <= n < v.size(). Looks pretty conclusive that it ought to work and I'm gonna have to stop. It's too damn late and I work in the morning. And I'm having trouble typing work. Always a bad sign.
– user4581301
Nov 15 '18 at 6: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%2f53312553%2fhow-to-pass-vector-to-pointer-array-winapi%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
if (!EnumProcesses(aProcesses, aProcesses.capacity(), &cbNeeded))
won't work well. First thing is, you need to pass aProcesses.data()
as the first parameter and the number of *bytes* aProcess
can hold as the 2nd: aProcesses.size() * sizeof(DOWD)
.
If cbNeeded
>= aProcess.size() * sizeof(DWORD)
after the call, you have to resize the vector
to cbNeeded / sizeof(DWORD)
and call EnumProcesses()
again to make sure you got all processes.
if like that. is it call EnumProcesses twice. it make execution slow?
– Dwerson busch
Nov 15 '18 at 5:20
You have no other choice if you don't pass an array that is big enough the first time you callEnumProcesses()
.
– Swordfish
Nov 15 '18 at 5:44
add a comment |
if (!EnumProcesses(aProcesses, aProcesses.capacity(), &cbNeeded))
won't work well. First thing is, you need to pass aProcesses.data()
as the first parameter and the number of *bytes* aProcess
can hold as the 2nd: aProcesses.size() * sizeof(DOWD)
.
If cbNeeded
>= aProcess.size() * sizeof(DWORD)
after the call, you have to resize the vector
to cbNeeded / sizeof(DWORD)
and call EnumProcesses()
again to make sure you got all processes.
if like that. is it call EnumProcesses twice. it make execution slow?
– Dwerson busch
Nov 15 '18 at 5:20
You have no other choice if you don't pass an array that is big enough the first time you callEnumProcesses()
.
– Swordfish
Nov 15 '18 at 5:44
add a comment |
if (!EnumProcesses(aProcesses, aProcesses.capacity(), &cbNeeded))
won't work well. First thing is, you need to pass aProcesses.data()
as the first parameter and the number of *bytes* aProcess
can hold as the 2nd: aProcesses.size() * sizeof(DOWD)
.
If cbNeeded
>= aProcess.size() * sizeof(DWORD)
after the call, you have to resize the vector
to cbNeeded / sizeof(DWORD)
and call EnumProcesses()
again to make sure you got all processes.
if (!EnumProcesses(aProcesses, aProcesses.capacity(), &cbNeeded))
won't work well. First thing is, you need to pass aProcesses.data()
as the first parameter and the number of *bytes* aProcess
can hold as the 2nd: aProcesses.size() * sizeof(DOWD)
.
If cbNeeded
>= aProcess.size() * sizeof(DWORD)
after the call, you have to resize the vector
to cbNeeded / sizeof(DWORD)
and call EnumProcesses()
again to make sure you got all processes.
answered Nov 15 '18 at 5:06
SwordfishSwordfish
10.2k11436
10.2k11436
if like that. is it call EnumProcesses twice. it make execution slow?
– Dwerson busch
Nov 15 '18 at 5:20
You have no other choice if you don't pass an array that is big enough the first time you callEnumProcesses()
.
– Swordfish
Nov 15 '18 at 5:44
add a comment |
if like that. is it call EnumProcesses twice. it make execution slow?
– Dwerson busch
Nov 15 '18 at 5:20
You have no other choice if you don't pass an array that is big enough the first time you callEnumProcesses()
.
– Swordfish
Nov 15 '18 at 5:44
if like that. is it call EnumProcesses twice. it make execution slow?
– Dwerson busch
Nov 15 '18 at 5:20
if like that. is it call EnumProcesses twice. it make execution slow?
– Dwerson busch
Nov 15 '18 at 5:20
You have no other choice if you don't pass an array that is big enough the first time you call
EnumProcesses()
.– Swordfish
Nov 15 '18 at 5:44
You have no other choice if you don't pass an array that is big enough the first time you call
EnumProcesses()
.– Swordfish
Nov 15 '18 at 5:44
add a comment |
A vector
isn't a pointer to DWORD
; however, this vector
happens to contain one so you can
if (!EnumProcesses(aProcesses.data(), aProcesses.capacity(), &cbNeeded))
if your compiler is compiling for the C++11 Standard revision or more recent or
if (!EnumProcesses(&aProcesses[0], aProcesses.capacity(), &cbNeeded))
if it is not.
But that just gets things compiling
There are logic problems yet to be dealt with.
const int MAX_PROCESSES = 1024; // very few Windows systems will exceed 1024 processes
std::vector<DWORD> aProcesses(MAX_PROCESSES); // allocate space
DWORD cbNeeded;
if (!EnumProcesses(aProcesses.data(),
aProcesses.capacity() * sizeof(DWORD), // size in bytes, not elements
&cbNeeded))
{
return 1;
}
//for (auto& p : aProcesses) Not all processes may have been filled. The unused portions
//are zeroed, so this is safe, but wasteful
for (int i = 0; i < cbNeeded / sizeof(DWORD); ++i)
{
std::cout << aProcesses[i] << std::endl;
}
is not MAX_NUM_DWORDS macro manually allocated memory? i should knows max possible the value?
– Dwerson busch
Nov 15 '18 at 5:06
@Dwersonbusch No, that was a stub. There is pretty much no way to know how many processes are running, but this rarely gets above a few hundred on all but the busiest of servers. Will update question.
– user4581301
Nov 15 '18 at 5:12
EnumProcesses api it just example. i knows if max processes will not run above 1k. but there's other winapi it will return above 1k value.
– Dwerson busch
Nov 15 '18 at 5:18
Why do you say that&aProcesses[0]
isn't legal?aProcesses[0]
returns a reference,T&
, and taking the address of a reference is just a pointer to the referenced object. A DWORD in this case.
– doug
Nov 15 '18 at 5:58
@doug I can't imagine how you could dovector
and theoverload so that
&vec[0]
won't work, but it's not guaranteed. It's only an issue with C++ 98 and 03, so I did some old school standard diving and in 2005 drafts the wording looks like obeys the identity &v[n] == &v[0] + n for all 0 <= n < v.size(). Looks pretty conclusive that it ought to work and I'm gonna have to stop. It's too damn late and I work in the morning. And I'm having trouble typing work. Always a bad sign.
– user4581301
Nov 15 '18 at 6:59
add a comment |
A vector
isn't a pointer to DWORD
; however, this vector
happens to contain one so you can
if (!EnumProcesses(aProcesses.data(), aProcesses.capacity(), &cbNeeded))
if your compiler is compiling for the C++11 Standard revision or more recent or
if (!EnumProcesses(&aProcesses[0], aProcesses.capacity(), &cbNeeded))
if it is not.
But that just gets things compiling
There are logic problems yet to be dealt with.
const int MAX_PROCESSES = 1024; // very few Windows systems will exceed 1024 processes
std::vector<DWORD> aProcesses(MAX_PROCESSES); // allocate space
DWORD cbNeeded;
if (!EnumProcesses(aProcesses.data(),
aProcesses.capacity() * sizeof(DWORD), // size in bytes, not elements
&cbNeeded))
{
return 1;
}
//for (auto& p : aProcesses) Not all processes may have been filled. The unused portions
//are zeroed, so this is safe, but wasteful
for (int i = 0; i < cbNeeded / sizeof(DWORD); ++i)
{
std::cout << aProcesses[i] << std::endl;
}
is not MAX_NUM_DWORDS macro manually allocated memory? i should knows max possible the value?
– Dwerson busch
Nov 15 '18 at 5:06
@Dwersonbusch No, that was a stub. There is pretty much no way to know how many processes are running, but this rarely gets above a few hundred on all but the busiest of servers. Will update question.
– user4581301
Nov 15 '18 at 5:12
EnumProcesses api it just example. i knows if max processes will not run above 1k. but there's other winapi it will return above 1k value.
– Dwerson busch
Nov 15 '18 at 5:18
Why do you say that&aProcesses[0]
isn't legal?aProcesses[0]
returns a reference,T&
, and taking the address of a reference is just a pointer to the referenced object. A DWORD in this case.
– doug
Nov 15 '18 at 5:58
@doug I can't imagine how you could dovector
and theoverload so that
&vec[0]
won't work, but it's not guaranteed. It's only an issue with C++ 98 and 03, so I did some old school standard diving and in 2005 drafts the wording looks like obeys the identity &v[n] == &v[0] + n for all 0 <= n < v.size(). Looks pretty conclusive that it ought to work and I'm gonna have to stop. It's too damn late and I work in the morning. And I'm having trouble typing work. Always a bad sign.
– user4581301
Nov 15 '18 at 6:59
add a comment |
A vector
isn't a pointer to DWORD
; however, this vector
happens to contain one so you can
if (!EnumProcesses(aProcesses.data(), aProcesses.capacity(), &cbNeeded))
if your compiler is compiling for the C++11 Standard revision or more recent or
if (!EnumProcesses(&aProcesses[0], aProcesses.capacity(), &cbNeeded))
if it is not.
But that just gets things compiling
There are logic problems yet to be dealt with.
const int MAX_PROCESSES = 1024; // very few Windows systems will exceed 1024 processes
std::vector<DWORD> aProcesses(MAX_PROCESSES); // allocate space
DWORD cbNeeded;
if (!EnumProcesses(aProcesses.data(),
aProcesses.capacity() * sizeof(DWORD), // size in bytes, not elements
&cbNeeded))
{
return 1;
}
//for (auto& p : aProcesses) Not all processes may have been filled. The unused portions
//are zeroed, so this is safe, but wasteful
for (int i = 0; i < cbNeeded / sizeof(DWORD); ++i)
{
std::cout << aProcesses[i] << std::endl;
}
A vector
isn't a pointer to DWORD
; however, this vector
happens to contain one so you can
if (!EnumProcesses(aProcesses.data(), aProcesses.capacity(), &cbNeeded))
if your compiler is compiling for the C++11 Standard revision or more recent or
if (!EnumProcesses(&aProcesses[0], aProcesses.capacity(), &cbNeeded))
if it is not.
But that just gets things compiling
There are logic problems yet to be dealt with.
const int MAX_PROCESSES = 1024; // very few Windows systems will exceed 1024 processes
std::vector<DWORD> aProcesses(MAX_PROCESSES); // allocate space
DWORD cbNeeded;
if (!EnumProcesses(aProcesses.data(),
aProcesses.capacity() * sizeof(DWORD), // size in bytes, not elements
&cbNeeded))
{
return 1;
}
//for (auto& p : aProcesses) Not all processes may have been filled. The unused portions
//are zeroed, so this is safe, but wasteful
for (int i = 0; i < cbNeeded / sizeof(DWORD); ++i)
{
std::cout << aProcesses[i] << std::endl;
}
edited Nov 15 '18 at 7:00
answered Nov 15 '18 at 4:51
user4581301user4581301
20.5k51931
20.5k51931
is not MAX_NUM_DWORDS macro manually allocated memory? i should knows max possible the value?
– Dwerson busch
Nov 15 '18 at 5:06
@Dwersonbusch No, that was a stub. There is pretty much no way to know how many processes are running, but this rarely gets above a few hundred on all but the busiest of servers. Will update question.
– user4581301
Nov 15 '18 at 5:12
EnumProcesses api it just example. i knows if max processes will not run above 1k. but there's other winapi it will return above 1k value.
– Dwerson busch
Nov 15 '18 at 5:18
Why do you say that&aProcesses[0]
isn't legal?aProcesses[0]
returns a reference,T&
, and taking the address of a reference is just a pointer to the referenced object. A DWORD in this case.
– doug
Nov 15 '18 at 5:58
@doug I can't imagine how you could dovector
and theoverload so that
&vec[0]
won't work, but it's not guaranteed. It's only an issue with C++ 98 and 03, so I did some old school standard diving and in 2005 drafts the wording looks like obeys the identity &v[n] == &v[0] + n for all 0 <= n < v.size(). Looks pretty conclusive that it ought to work and I'm gonna have to stop. It's too damn late and I work in the morning. And I'm having trouble typing work. Always a bad sign.
– user4581301
Nov 15 '18 at 6:59
add a comment |
is not MAX_NUM_DWORDS macro manually allocated memory? i should knows max possible the value?
– Dwerson busch
Nov 15 '18 at 5:06
@Dwersonbusch No, that was a stub. There is pretty much no way to know how many processes are running, but this rarely gets above a few hundred on all but the busiest of servers. Will update question.
– user4581301
Nov 15 '18 at 5:12
EnumProcesses api it just example. i knows if max processes will not run above 1k. but there's other winapi it will return above 1k value.
– Dwerson busch
Nov 15 '18 at 5:18
Why do you say that&aProcesses[0]
isn't legal?aProcesses[0]
returns a reference,T&
, and taking the address of a reference is just a pointer to the referenced object. A DWORD in this case.
– doug
Nov 15 '18 at 5:58
@doug I can't imagine how you could dovector
and theoverload so that
&vec[0]
won't work, but it's not guaranteed. It's only an issue with C++ 98 and 03, so I did some old school standard diving and in 2005 drafts the wording looks like obeys the identity &v[n] == &v[0] + n for all 0 <= n < v.size(). Looks pretty conclusive that it ought to work and I'm gonna have to stop. It's too damn late and I work in the morning. And I'm having trouble typing work. Always a bad sign.
– user4581301
Nov 15 '18 at 6:59
is not MAX_NUM_DWORDS macro manually allocated memory? i should knows max possible the value?
– Dwerson busch
Nov 15 '18 at 5:06
is not MAX_NUM_DWORDS macro manually allocated memory? i should knows max possible the value?
– Dwerson busch
Nov 15 '18 at 5:06
@Dwersonbusch No, that was a stub. There is pretty much no way to know how many processes are running, but this rarely gets above a few hundred on all but the busiest of servers. Will update question.
– user4581301
Nov 15 '18 at 5:12
@Dwersonbusch No, that was a stub. There is pretty much no way to know how many processes are running, but this rarely gets above a few hundred on all but the busiest of servers. Will update question.
– user4581301
Nov 15 '18 at 5:12
EnumProcesses api it just example. i knows if max processes will not run above 1k. but there's other winapi it will return above 1k value.
– Dwerson busch
Nov 15 '18 at 5:18
EnumProcesses api it just example. i knows if max processes will not run above 1k. but there's other winapi it will return above 1k value.
– Dwerson busch
Nov 15 '18 at 5:18
Why do you say that
&aProcesses[0]
isn't legal? aProcesses[0]
returns a reference, T&
, and taking the address of a reference is just a pointer to the referenced object. A DWORD in this case.– doug
Nov 15 '18 at 5:58
Why do you say that
&aProcesses[0]
isn't legal? aProcesses[0]
returns a reference, T&
, and taking the address of a reference is just a pointer to the referenced object. A DWORD in this case.– doug
Nov 15 '18 at 5:58
@doug I can't imagine how you could do
vector
and the
overload so that &vec[0]
won't work, but it's not guaranteed. It's only an issue with C++ 98 and 03, so I did some old school standard diving and in 2005 drafts the wording looks like obeys the identity &v[n] == &v[0] + n for all 0 <= n < v.size(). Looks pretty conclusive that it ought to work and I'm gonna have to stop. It's too damn late and I work in the morning. And I'm having trouble typing work. Always a bad sign.– user4581301
Nov 15 '18 at 6:59
@doug I can't imagine how you could do
vector
and the
overload so that &vec[0]
won't work, but it's not guaranteed. It's only an issue with C++ 98 and 03, so I did some old school standard diving and in 2005 drafts the wording looks like obeys the identity &v[n] == &v[0] + n for all 0 <= n < v.size(). Looks pretty conclusive that it ought to work and I'm gonna have to stop. It's too damn late and I work in the morning. And I'm having trouble typing work. Always a bad sign.– user4581301
Nov 15 '18 at 6: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%2f53312553%2fhow-to-pass-vector-to-pointer-array-winapi%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
2
instead of aProcesses` use
aProcesses.data()
or alternately&aProcesses[0]
see: en.cppreference.com/w/cpp/container/vector/data– doug
Nov 15 '18 at 4:48
1
Note that you need to call
resize()
on the vector yourself before passing a pointer to the data buffer to an API function, since Windows doesn't know anything about c++.– Jonathan Potter
Nov 15 '18 at 4:51
It would be helpful to include the code you anticipate replacing to provide more specific advice.
– doug
Nov 15 '18 at 4:58
@doug i include manual allocate memory ways
– Dwerson busch
Nov 15 '18 at 5:04
Just declare your vector like so:
std::vector<DWORD> aProcesses(1024);
and useaProcesses.data()
which is a pointer to the first vector element.– doug
Nov 15 '18 at 5:08