C++ - passing static 2d array to functions
How am I supposed to pass static 2d array to function in cpp as an argument? I tried something like that:
void foo(int (&tab)[N][N]) {
// function body
}
int main() {
int n;
cin >> n;
int tab[n][n];
foo(tab); // doesn't work
return 0;
}
I get "no matching function error" when I try to call foo.
I need static arrays, because vectors are too slow for my needs. I would like to avoid declaring array with 10000 rows and columns, too. Moreover, I would want to use functions, because it will make my code readable. Is there any solution for this problem which will meet my expectations?
c++
|
show 13 more comments
How am I supposed to pass static 2d array to function in cpp as an argument? I tried something like that:
void foo(int (&tab)[N][N]) {
// function body
}
int main() {
int n;
cin >> n;
int tab[n][n];
foo(tab); // doesn't work
return 0;
}
I get "no matching function error" when I try to call foo.
I need static arrays, because vectors are too slow for my needs. I would like to avoid declaring array with 10000 rows and columns, too. Moreover, I would want to use functions, because it will make my code readable. Is there any solution for this problem which will meet my expectations?
c++
1
This shoudn't even work for a "1D array". C++ doesn't support variable length arrays.
– juanchopanza
Nov 22 at 20:39
1
Why not double pointerint **tab
?
– ventaquil
Nov 22 at 20:47
2
because vectors are too slow for my needs. – too slow for what?
– Swordfish
Nov 22 at 21:03
3
@ventaquil "double pointer" - Why would you want to do that? Keep code simple - more stars usually indicate worse code, not better. Modern C++ has containers, smart pointers and more that you can use to avoid having to use low level stuff and keep things sane (and the abstractions usually optimize away).
– Jesper Juhl
Nov 22 at 21:03
1
@Mentos1105 Duh! Use one vector with sizerows * columns
and calculate the indexes like(row * columns + col)
. If you know you'll stay in the same row for the next n accesses, cache the index and just do the addition. Term to look up: Cache locality.
– Swordfish
Nov 24 at 15:27
|
show 13 more comments
How am I supposed to pass static 2d array to function in cpp as an argument? I tried something like that:
void foo(int (&tab)[N][N]) {
// function body
}
int main() {
int n;
cin >> n;
int tab[n][n];
foo(tab); // doesn't work
return 0;
}
I get "no matching function error" when I try to call foo.
I need static arrays, because vectors are too slow for my needs. I would like to avoid declaring array with 10000 rows and columns, too. Moreover, I would want to use functions, because it will make my code readable. Is there any solution for this problem which will meet my expectations?
c++
How am I supposed to pass static 2d array to function in cpp as an argument? I tried something like that:
void foo(int (&tab)[N][N]) {
// function body
}
int main() {
int n;
cin >> n;
int tab[n][n];
foo(tab); // doesn't work
return 0;
}
I get "no matching function error" when I try to call foo.
I need static arrays, because vectors are too slow for my needs. I would like to avoid declaring array with 10000 rows and columns, too. Moreover, I would want to use functions, because it will make my code readable. Is there any solution for this problem which will meet my expectations?
c++
c++
edited Nov 22 at 20:52
asked Nov 22 at 20:38
Mentos1105
186
186
1
This shoudn't even work for a "1D array". C++ doesn't support variable length arrays.
– juanchopanza
Nov 22 at 20:39
1
Why not double pointerint **tab
?
– ventaquil
Nov 22 at 20:47
2
because vectors are too slow for my needs. – too slow for what?
– Swordfish
Nov 22 at 21:03
3
@ventaquil "double pointer" - Why would you want to do that? Keep code simple - more stars usually indicate worse code, not better. Modern C++ has containers, smart pointers and more that you can use to avoid having to use low level stuff and keep things sane (and the abstractions usually optimize away).
– Jesper Juhl
Nov 22 at 21:03
1
@Mentos1105 Duh! Use one vector with sizerows * columns
and calculate the indexes like(row * columns + col)
. If you know you'll stay in the same row for the next n accesses, cache the index and just do the addition. Term to look up: Cache locality.
– Swordfish
Nov 24 at 15:27
|
show 13 more comments
1
This shoudn't even work for a "1D array". C++ doesn't support variable length arrays.
– juanchopanza
Nov 22 at 20:39
1
Why not double pointerint **tab
?
– ventaquil
Nov 22 at 20:47
2
because vectors are too slow for my needs. – too slow for what?
– Swordfish
Nov 22 at 21:03
3
@ventaquil "double pointer" - Why would you want to do that? Keep code simple - more stars usually indicate worse code, not better. Modern C++ has containers, smart pointers and more that you can use to avoid having to use low level stuff and keep things sane (and the abstractions usually optimize away).
– Jesper Juhl
Nov 22 at 21:03
1
@Mentos1105 Duh! Use one vector with sizerows * columns
and calculate the indexes like(row * columns + col)
. If you know you'll stay in the same row for the next n accesses, cache the index and just do the addition. Term to look up: Cache locality.
– Swordfish
Nov 24 at 15:27
1
1
This shoudn't even work for a "1D array". C++ doesn't support variable length arrays.
– juanchopanza
Nov 22 at 20:39
This shoudn't even work for a "1D array". C++ doesn't support variable length arrays.
– juanchopanza
Nov 22 at 20:39
1
1
Why not double pointer
int **tab
?– ventaquil
Nov 22 at 20:47
Why not double pointer
int **tab
?– ventaquil
Nov 22 at 20:47
2
2
because vectors are too slow for my needs. – too slow for what?
– Swordfish
Nov 22 at 21:03
because vectors are too slow for my needs. – too slow for what?
– Swordfish
Nov 22 at 21:03
3
3
@ventaquil "double pointer" - Why would you want to do that? Keep code simple - more stars usually indicate worse code, not better. Modern C++ has containers, smart pointers and more that you can use to avoid having to use low level stuff and keep things sane (and the abstractions usually optimize away).
– Jesper Juhl
Nov 22 at 21:03
@ventaquil "double pointer" - Why would you want to do that? Keep code simple - more stars usually indicate worse code, not better. Modern C++ has containers, smart pointers and more that you can use to avoid having to use low level stuff and keep things sane (and the abstractions usually optimize away).
– Jesper Juhl
Nov 22 at 21:03
1
1
@Mentos1105 Duh! Use one vector with size
rows * columns
and calculate the indexes like (row * columns + col)
. If you know you'll stay in the same row for the next n accesses, cache the index and just do the addition. Term to look up: Cache locality.– Swordfish
Nov 24 at 15:27
@Mentos1105 Duh! Use one vector with size
rows * columns
and calculate the indexes like (row * columns + col)
. If you know you'll stay in the same row for the next n accesses, cache the index and just do the addition. Term to look up: Cache locality.– Swordfish
Nov 24 at 15:27
|
show 13 more comments
3 Answers
3
active
oldest
votes
The classical C++ solution would involve using vectors of vectors. If it's not suitable (because you want more speed or more control over memory), you can define your own class for a square 2-D array.
One idea I used in my code is, implement it using an underlying 1-D vector
, with accessor method returning a pointer.
struct My_2D_Array
{
explicit My_2D_Array(size_t n):
m_size(n),
m_data(n * n)
{
}
int* operator(size_t i)
{
return m_data.data() + i * m_size;
}
size_t m_size;
std::vector<int> m_data;
};
This not only lacks all sanity checks, and also makes bound-checked access impossible (because the accessor returns a bare pointer), but will work as a quick-and-dirty solution.
Usage in your code:
int foo(My_2D_Array& matrix)
{
// example
return matrix[2][3] + matrix[3][2];
}
int main()
{
int n;
cin >> n;
My_2D_Array tab(n);
foo(tab);
return 0;
}
This idea is highly customizable - you can make the code for My_2D_Array
as simple or as clever as you want. For example, if you still don't like usage of vector
, even though it's 1-D, you can manage (allocate/deallocate) your memory separately, and store int*
, instead of vector<int>
, in My_2D_Array
.
add a comment |
With cin >> n;int tab[n][n];
, you declare a variable length array (i.e. an array which's dimensions are not compile-time-constants).
You have two problems here: First, they are not supported by standard C++, and second they are not compatible with fixed size array parameters you introduced.
If you declare your array with compile time known size, however, it will work:
#define N 10
void foo(int (&tab)[N][N]) {
cout << tab[1][1] << endl;
}
int main() {
int tab[N][N] = {};
tab[1][1]=15;
foo(tab);
return 0;
}
Yeah, but size of my array is based on data from file which is read by my code earlier. So in order to avoid any slow dynamic data-types I need to declare huge 2-dimensional array and just work on part of it?
– Mentos1105
Nov 22 at 20:49
Even that's not possible, sincefoo
will always work on a compile-time-constant dimensionN
. you cannot pass anything to it other than an array of dimensions[N][N]
. Dynamic dimensions imply dynamically allocated arrays.
– Stephan Lechner
Nov 22 at 20:56
Yeah, I meant that in function body i will just use needed part of my array.
– Mentos1105
Nov 22 at 21:00
add a comment |
Just use a vector<>
of vector<int>
. No need for mucking around with non-standard arrays.
I said in post that vectors are too slow for my needs.
– Mentos1105
Nov 22 at 20:56
1
@Mentos1105 too slow for what? Thats a weird claim. A vector is as fast as you can get a dynamically sized array
– user463035818
Nov 22 at 20:58
1
@Mentos1105 Then use a single vector and view it as a 2D thing.
– juanchopanza
Nov 22 at 20:59
Have you measured it? You can't make broad sweeping statements such as this with out having first done some leg work to verify if a vector is indeed slower (which would be mightily surprising..) Write clean code - let the compiler do it's job.
– Nim
Nov 22 at 21:08
1
Fully optimized? And when you say static arrays - do you mean statically sized arrays (compile time) or dynamically allocated arrays? And it's the "details" that will determine whether you get a quality answer or something else - keep that in mind for the future...
– Nim
Nov 22 at 21:18
|
show 3 more comments
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%2f53437738%2fc-passing-static-2d-array-to-functions%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
The classical C++ solution would involve using vectors of vectors. If it's not suitable (because you want more speed or more control over memory), you can define your own class for a square 2-D array.
One idea I used in my code is, implement it using an underlying 1-D vector
, with accessor method returning a pointer.
struct My_2D_Array
{
explicit My_2D_Array(size_t n):
m_size(n),
m_data(n * n)
{
}
int* operator(size_t i)
{
return m_data.data() + i * m_size;
}
size_t m_size;
std::vector<int> m_data;
};
This not only lacks all sanity checks, and also makes bound-checked access impossible (because the accessor returns a bare pointer), but will work as a quick-and-dirty solution.
Usage in your code:
int foo(My_2D_Array& matrix)
{
// example
return matrix[2][3] + matrix[3][2];
}
int main()
{
int n;
cin >> n;
My_2D_Array tab(n);
foo(tab);
return 0;
}
This idea is highly customizable - you can make the code for My_2D_Array
as simple or as clever as you want. For example, if you still don't like usage of vector
, even though it's 1-D, you can manage (allocate/deallocate) your memory separately, and store int*
, instead of vector<int>
, in My_2D_Array
.
add a comment |
The classical C++ solution would involve using vectors of vectors. If it's not suitable (because you want more speed or more control over memory), you can define your own class for a square 2-D array.
One idea I used in my code is, implement it using an underlying 1-D vector
, with accessor method returning a pointer.
struct My_2D_Array
{
explicit My_2D_Array(size_t n):
m_size(n),
m_data(n * n)
{
}
int* operator(size_t i)
{
return m_data.data() + i * m_size;
}
size_t m_size;
std::vector<int> m_data;
};
This not only lacks all sanity checks, and also makes bound-checked access impossible (because the accessor returns a bare pointer), but will work as a quick-and-dirty solution.
Usage in your code:
int foo(My_2D_Array& matrix)
{
// example
return matrix[2][3] + matrix[3][2];
}
int main()
{
int n;
cin >> n;
My_2D_Array tab(n);
foo(tab);
return 0;
}
This idea is highly customizable - you can make the code for My_2D_Array
as simple or as clever as you want. For example, if you still don't like usage of vector
, even though it's 1-D, you can manage (allocate/deallocate) your memory separately, and store int*
, instead of vector<int>
, in My_2D_Array
.
add a comment |
The classical C++ solution would involve using vectors of vectors. If it's not suitable (because you want more speed or more control over memory), you can define your own class for a square 2-D array.
One idea I used in my code is, implement it using an underlying 1-D vector
, with accessor method returning a pointer.
struct My_2D_Array
{
explicit My_2D_Array(size_t n):
m_size(n),
m_data(n * n)
{
}
int* operator(size_t i)
{
return m_data.data() + i * m_size;
}
size_t m_size;
std::vector<int> m_data;
};
This not only lacks all sanity checks, and also makes bound-checked access impossible (because the accessor returns a bare pointer), but will work as a quick-and-dirty solution.
Usage in your code:
int foo(My_2D_Array& matrix)
{
// example
return matrix[2][3] + matrix[3][2];
}
int main()
{
int n;
cin >> n;
My_2D_Array tab(n);
foo(tab);
return 0;
}
This idea is highly customizable - you can make the code for My_2D_Array
as simple or as clever as you want. For example, if you still don't like usage of vector
, even though it's 1-D, you can manage (allocate/deallocate) your memory separately, and store int*
, instead of vector<int>
, in My_2D_Array
.
The classical C++ solution would involve using vectors of vectors. If it's not suitable (because you want more speed or more control over memory), you can define your own class for a square 2-D array.
One idea I used in my code is, implement it using an underlying 1-D vector
, with accessor method returning a pointer.
struct My_2D_Array
{
explicit My_2D_Array(size_t n):
m_size(n),
m_data(n * n)
{
}
int* operator(size_t i)
{
return m_data.data() + i * m_size;
}
size_t m_size;
std::vector<int> m_data;
};
This not only lacks all sanity checks, and also makes bound-checked access impossible (because the accessor returns a bare pointer), but will work as a quick-and-dirty solution.
Usage in your code:
int foo(My_2D_Array& matrix)
{
// example
return matrix[2][3] + matrix[3][2];
}
int main()
{
int n;
cin >> n;
My_2D_Array tab(n);
foo(tab);
return 0;
}
This idea is highly customizable - you can make the code for My_2D_Array
as simple or as clever as you want. For example, if you still don't like usage of vector
, even though it's 1-D, you can manage (allocate/deallocate) your memory separately, and store int*
, instead of vector<int>
, in My_2D_Array
.
edited Nov 22 at 21:12
answered Nov 22 at 20:59
anatolyg
16.9k44590
16.9k44590
add a comment |
add a comment |
With cin >> n;int tab[n][n];
, you declare a variable length array (i.e. an array which's dimensions are not compile-time-constants).
You have two problems here: First, they are not supported by standard C++, and second they are not compatible with fixed size array parameters you introduced.
If you declare your array with compile time known size, however, it will work:
#define N 10
void foo(int (&tab)[N][N]) {
cout << tab[1][1] << endl;
}
int main() {
int tab[N][N] = {};
tab[1][1]=15;
foo(tab);
return 0;
}
Yeah, but size of my array is based on data from file which is read by my code earlier. So in order to avoid any slow dynamic data-types I need to declare huge 2-dimensional array and just work on part of it?
– Mentos1105
Nov 22 at 20:49
Even that's not possible, sincefoo
will always work on a compile-time-constant dimensionN
. you cannot pass anything to it other than an array of dimensions[N][N]
. Dynamic dimensions imply dynamically allocated arrays.
– Stephan Lechner
Nov 22 at 20:56
Yeah, I meant that in function body i will just use needed part of my array.
– Mentos1105
Nov 22 at 21:00
add a comment |
With cin >> n;int tab[n][n];
, you declare a variable length array (i.e. an array which's dimensions are not compile-time-constants).
You have two problems here: First, they are not supported by standard C++, and second they are not compatible with fixed size array parameters you introduced.
If you declare your array with compile time known size, however, it will work:
#define N 10
void foo(int (&tab)[N][N]) {
cout << tab[1][1] << endl;
}
int main() {
int tab[N][N] = {};
tab[1][1]=15;
foo(tab);
return 0;
}
Yeah, but size of my array is based on data from file which is read by my code earlier. So in order to avoid any slow dynamic data-types I need to declare huge 2-dimensional array and just work on part of it?
– Mentos1105
Nov 22 at 20:49
Even that's not possible, sincefoo
will always work on a compile-time-constant dimensionN
. you cannot pass anything to it other than an array of dimensions[N][N]
. Dynamic dimensions imply dynamically allocated arrays.
– Stephan Lechner
Nov 22 at 20:56
Yeah, I meant that in function body i will just use needed part of my array.
– Mentos1105
Nov 22 at 21:00
add a comment |
With cin >> n;int tab[n][n];
, you declare a variable length array (i.e. an array which's dimensions are not compile-time-constants).
You have two problems here: First, they are not supported by standard C++, and second they are not compatible with fixed size array parameters you introduced.
If you declare your array with compile time known size, however, it will work:
#define N 10
void foo(int (&tab)[N][N]) {
cout << tab[1][1] << endl;
}
int main() {
int tab[N][N] = {};
tab[1][1]=15;
foo(tab);
return 0;
}
With cin >> n;int tab[n][n];
, you declare a variable length array (i.e. an array which's dimensions are not compile-time-constants).
You have two problems here: First, they are not supported by standard C++, and second they are not compatible with fixed size array parameters you introduced.
If you declare your array with compile time known size, however, it will work:
#define N 10
void foo(int (&tab)[N][N]) {
cout << tab[1][1] << endl;
}
int main() {
int tab[N][N] = {};
tab[1][1]=15;
foo(tab);
return 0;
}
answered Nov 22 at 20:45
Stephan Lechner
25.4k21839
25.4k21839
Yeah, but size of my array is based on data from file which is read by my code earlier. So in order to avoid any slow dynamic data-types I need to declare huge 2-dimensional array and just work on part of it?
– Mentos1105
Nov 22 at 20:49
Even that's not possible, sincefoo
will always work on a compile-time-constant dimensionN
. you cannot pass anything to it other than an array of dimensions[N][N]
. Dynamic dimensions imply dynamically allocated arrays.
– Stephan Lechner
Nov 22 at 20:56
Yeah, I meant that in function body i will just use needed part of my array.
– Mentos1105
Nov 22 at 21:00
add a comment |
Yeah, but size of my array is based on data from file which is read by my code earlier. So in order to avoid any slow dynamic data-types I need to declare huge 2-dimensional array and just work on part of it?
– Mentos1105
Nov 22 at 20:49
Even that's not possible, sincefoo
will always work on a compile-time-constant dimensionN
. you cannot pass anything to it other than an array of dimensions[N][N]
. Dynamic dimensions imply dynamically allocated arrays.
– Stephan Lechner
Nov 22 at 20:56
Yeah, I meant that in function body i will just use needed part of my array.
– Mentos1105
Nov 22 at 21:00
Yeah, but size of my array is based on data from file which is read by my code earlier. So in order to avoid any slow dynamic data-types I need to declare huge 2-dimensional array and just work on part of it?
– Mentos1105
Nov 22 at 20:49
Yeah, but size of my array is based on data from file which is read by my code earlier. So in order to avoid any slow dynamic data-types I need to declare huge 2-dimensional array and just work on part of it?
– Mentos1105
Nov 22 at 20:49
Even that's not possible, since
foo
will always work on a compile-time-constant dimension N
. you cannot pass anything to it other than an array of dimensions [N][N]
. Dynamic dimensions imply dynamically allocated arrays.– Stephan Lechner
Nov 22 at 20:56
Even that's not possible, since
foo
will always work on a compile-time-constant dimension N
. you cannot pass anything to it other than an array of dimensions [N][N]
. Dynamic dimensions imply dynamically allocated arrays.– Stephan Lechner
Nov 22 at 20:56
Yeah, I meant that in function body i will just use needed part of my array.
– Mentos1105
Nov 22 at 21:00
Yeah, I meant that in function body i will just use needed part of my array.
– Mentos1105
Nov 22 at 21:00
add a comment |
Just use a vector<>
of vector<int>
. No need for mucking around with non-standard arrays.
I said in post that vectors are too slow for my needs.
– Mentos1105
Nov 22 at 20:56
1
@Mentos1105 too slow for what? Thats a weird claim. A vector is as fast as you can get a dynamically sized array
– user463035818
Nov 22 at 20:58
1
@Mentos1105 Then use a single vector and view it as a 2D thing.
– juanchopanza
Nov 22 at 20:59
Have you measured it? You can't make broad sweeping statements such as this with out having first done some leg work to verify if a vector is indeed slower (which would be mightily surprising..) Write clean code - let the compiler do it's job.
– Nim
Nov 22 at 21:08
1
Fully optimized? And when you say static arrays - do you mean statically sized arrays (compile time) or dynamically allocated arrays? And it's the "details" that will determine whether you get a quality answer or something else - keep that in mind for the future...
– Nim
Nov 22 at 21:18
|
show 3 more comments
Just use a vector<>
of vector<int>
. No need for mucking around with non-standard arrays.
I said in post that vectors are too slow for my needs.
– Mentos1105
Nov 22 at 20:56
1
@Mentos1105 too slow for what? Thats a weird claim. A vector is as fast as you can get a dynamically sized array
– user463035818
Nov 22 at 20:58
1
@Mentos1105 Then use a single vector and view it as a 2D thing.
– juanchopanza
Nov 22 at 20:59
Have you measured it? You can't make broad sweeping statements such as this with out having first done some leg work to verify if a vector is indeed slower (which would be mightily surprising..) Write clean code - let the compiler do it's job.
– Nim
Nov 22 at 21:08
1
Fully optimized? And when you say static arrays - do you mean statically sized arrays (compile time) or dynamically allocated arrays? And it's the "details" that will determine whether you get a quality answer or something else - keep that in mind for the future...
– Nim
Nov 22 at 21:18
|
show 3 more comments
Just use a vector<>
of vector<int>
. No need for mucking around with non-standard arrays.
Just use a vector<>
of vector<int>
. No need for mucking around with non-standard arrays.
answered Nov 22 at 20:54
Nim
29.2k24787
29.2k24787
I said in post that vectors are too slow for my needs.
– Mentos1105
Nov 22 at 20:56
1
@Mentos1105 too slow for what? Thats a weird claim. A vector is as fast as you can get a dynamically sized array
– user463035818
Nov 22 at 20:58
1
@Mentos1105 Then use a single vector and view it as a 2D thing.
– juanchopanza
Nov 22 at 20:59
Have you measured it? You can't make broad sweeping statements such as this with out having first done some leg work to verify if a vector is indeed slower (which would be mightily surprising..) Write clean code - let the compiler do it's job.
– Nim
Nov 22 at 21:08
1
Fully optimized? And when you say static arrays - do you mean statically sized arrays (compile time) or dynamically allocated arrays? And it's the "details" that will determine whether you get a quality answer or something else - keep that in mind for the future...
– Nim
Nov 22 at 21:18
|
show 3 more comments
I said in post that vectors are too slow for my needs.
– Mentos1105
Nov 22 at 20:56
1
@Mentos1105 too slow for what? Thats a weird claim. A vector is as fast as you can get a dynamically sized array
– user463035818
Nov 22 at 20:58
1
@Mentos1105 Then use a single vector and view it as a 2D thing.
– juanchopanza
Nov 22 at 20:59
Have you measured it? You can't make broad sweeping statements such as this with out having first done some leg work to verify if a vector is indeed slower (which would be mightily surprising..) Write clean code - let the compiler do it's job.
– Nim
Nov 22 at 21:08
1
Fully optimized? And when you say static arrays - do you mean statically sized arrays (compile time) or dynamically allocated arrays? And it's the "details" that will determine whether you get a quality answer or something else - keep that in mind for the future...
– Nim
Nov 22 at 21:18
I said in post that vectors are too slow for my needs.
– Mentos1105
Nov 22 at 20:56
I said in post that vectors are too slow for my needs.
– Mentos1105
Nov 22 at 20:56
1
1
@Mentos1105 too slow for what? Thats a weird claim. A vector is as fast as you can get a dynamically sized array
– user463035818
Nov 22 at 20:58
@Mentos1105 too slow for what? Thats a weird claim. A vector is as fast as you can get a dynamically sized array
– user463035818
Nov 22 at 20:58
1
1
@Mentos1105 Then use a single vector and view it as a 2D thing.
– juanchopanza
Nov 22 at 20:59
@Mentos1105 Then use a single vector and view it as a 2D thing.
– juanchopanza
Nov 22 at 20:59
Have you measured it? You can't make broad sweeping statements such as this with out having first done some leg work to verify if a vector is indeed slower (which would be mightily surprising..) Write clean code - let the compiler do it's job.
– Nim
Nov 22 at 21:08
Have you measured it? You can't make broad sweeping statements such as this with out having first done some leg work to verify if a vector is indeed slower (which would be mightily surprising..) Write clean code - let the compiler do it's job.
– Nim
Nov 22 at 21:08
1
1
Fully optimized? And when you say static arrays - do you mean statically sized arrays (compile time) or dynamically allocated arrays? And it's the "details" that will determine whether you get a quality answer or something else - keep that in mind for the future...
– Nim
Nov 22 at 21:18
Fully optimized? And when you say static arrays - do you mean statically sized arrays (compile time) or dynamically allocated arrays? And it's the "details" that will determine whether you get a quality answer or something else - keep that in mind for the future...
– Nim
Nov 22 at 21:18
|
show 3 more comments
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53437738%2fc-passing-static-2d-array-to-functions%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
1
This shoudn't even work for a "1D array". C++ doesn't support variable length arrays.
– juanchopanza
Nov 22 at 20:39
1
Why not double pointer
int **tab
?– ventaquil
Nov 22 at 20:47
2
because vectors are too slow for my needs. – too slow for what?
– Swordfish
Nov 22 at 21:03
3
@ventaquil "double pointer" - Why would you want to do that? Keep code simple - more stars usually indicate worse code, not better. Modern C++ has containers, smart pointers and more that you can use to avoid having to use low level stuff and keep things sane (and the abstractions usually optimize away).
– Jesper Juhl
Nov 22 at 21:03
1
@Mentos1105 Duh! Use one vector with size
rows * columns
and calculate the indexes like(row * columns + col)
. If you know you'll stay in the same row for the next n accesses, cache the index and just do the addition. Term to look up: Cache locality.– Swordfish
Nov 24 at 15:27