CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: passing an array as a pointer to a function

1. Junior Member
Join Date
Apr 2014
Posts
5

## passing an array as a pointer to a function

Hello!!
It's the first time I post at your forum..
I can clearly understand how to pass a dynamic array to a function and modified within the function..
My aim is to get back the array initialized as I am trying on the next sample of code..
Any help would mean a lot to me!

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <fstream>

using namespace std;

#define N	3	//number of bodies

void initialise( double *M[], double *X[], double *y[])
{
*M[1]=0.001; *M[2]= 0.002; *M[3]= 1.;	//Masses (the last body must be the gratest)

*X[0]=0.;				//Time
*X[1]=0.9741; *X[2]=0.; *X[3]=0.1; 	//x,y,z for body 1 MOON
*X[4]=2.2933; *X[5]=0.; *X[6]=1.; 	//x,y,z for body 2 EARTH
*X[7]=0.0055607; *X[8]=0.; *X[9]=0.0021;//x,y,z for body 3 SUN

*X[10]=0.2; *X[11]=1.0327; *X[12]=0.0005; 	//Vx,Vy,Vz for body 1
*X[13]=0.1; *X[14]=0.6320; *X[15]=0.0005; 	//Vx,Vy,Vz for body 2
*X[16]=0.01; *X[17]=0.0022967; *X[18]=0.0000015;//Vx,Vy,Vz for body 3

for ( int i=0; i<=3*(N-1); ++i ) 	         *y[i] = *X[i];
for ( int i=3*(N-1)+1; i<=6*(N-1); ++i ) *y[i] = *X[i+3];
}

int main()
{
double *M=new double[N+1];		//the masses of the bodies
double *X=new double[6*N+1];		//time,positions,velocities of N bodies
double *y=new double[6*(N-1)+1];	//time,positions,velocities of N-1 bodies

initialise( &M, &X, &y);

for (int i=0; i<=6*N; ++i) cout<<X[i]<<endl;

return 0;
}```

2. ## Re: passing an array as a pointer to a function

I can clearly understand how to pass a dynamic array to a function and modified within the function..
Perhaps not. You just need to pass a pointer to the memory of the array
Code:
```void initialise( double *M, double *X, double *y)
{
M[1]=0.001; M[2]= 0.002; M[3]= 1.;	//Masses (the last body must be the gratest)

X[0]=0.;				//Time
X[1]=0.9741; X[2]=0.; X[3]=0.1; 	//x,y,z for body 1 MOON
X[4]=2.2933; X[5]=0.; X[6]=1.; 	//x,y,z for body 2 EARTH
X[7]=0.0055607; X[8]=0.; X[9]=0.0021;//x,y,z for body 3 SUN

X[10]=0.2; X[11]=1.0327; X[12]=0.0005; 	//Vx,Vy,Vz for body 1
X[13]=0.1; X[14]=0.6320; X[15]=0.0005; 	//Vx,Vy,Vz for body 2
X[16]=0.01; X[17]=0.0022967; X[18]=0.0000015;//Vx,Vy,Vz for body 3

for ( int i=0; i<=3*(N-1); ++i )
y[i] = X[i];

for ( int i=3*(N-1)+1; i<=6*(N-1); ++i )
y[i] = X[i+3];
}

int main()
{
double *M = new double[N+1];		//the masses of the bodies
double *X = new double[6*N+1];		//time,positions,velocities of N bodies
double *y = new double[6*(N-1)+1];	//time,positions,velocities of N-1 bodies

initialise( M, X, y);

for (int i=0; i<=6*N; ++i)
cout<<X[i]<<endl;```
Why not use the STL vector class?

3. Junior Member
Join Date
Apr 2014
Posts
5

## Re: passing an array as a pointer to a function

I can clearly understand how to pass a dynamic array to a function and modified within the function..
Of course! I can't... I saw my mistake afterwards, and i didn't know how to modify the post...

It was so simple..!!

Why not use the STL vector class?
Because vectors need more time to execute (at least this what I know)...
I am doing a project, that has to run for many iterations, so I have to take under consideration the execution time..

Thank you very much 2kaud!
Thank you for your help and you instant reply!

4. ## Re: passing an array as a pointer to a function

i didn't know how to modify the post...
I believe you need to have made several (5?) posts before you obtain the ability to edit your posts.

initialise() could also be defined as
Code:
`void initialise( double M[], double X[], double y[])`

5. ## Re: passing an array as a pointer to a function

Originally Posted by nebula987
... Because vectors need more time to execute (at least this what I know)...
And what is the source (or the reason) of that "knowledge"? Did you measure the difference in executing using vectors and plain pointers?
Or you read it in some of the articles? Which one?
And what was the difference?

6. Junior Member
Join Date
Apr 2014
Posts
5

## Re: passing an array as a pointer to a function

I haven't read that in an article, I suppose that needs more time, because STL vector is a class, and I include an extra library...

Of course for programs that has to run a short time, this difference is insignificant, but for programs that has to run for hours or days, I have to take that in mind..

7. Junior Member
Join Date
Apr 2014
Posts
5

## Re: passing an array as a pointer to a function

I would be grateful to you if you could solve me a problem in the same part of code..
What if I wanted to pass an array to a function and again inside a function call it to an other function.. (like Inception :-P)

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <fstream>

using namespace std;

#define N	3	//number of bodies

void init_y_arr( double *y, double *X )
{
for ( int i=0; i<=3*(N-1); ++i ) 	 y[i] = X[i];
for ( int i=3*(N-1)+1; i<=6*(N-1); ++i ) y[i] = X[i+3];
}

void initialise( double *M, double *X, double *y)
{
M[1]=0.001; M[2]= 0.002; M[3]= 1.;	//Masses (the last body must be the gratest)

X[0]=0.;				//Time
X[1]=0.9741; X[2]=0.; X[3]=0.1; 	//x,y,z for body 1 MOON
X[4]=2.2933; X[5]=0.; X[6]=1.; 	//x,y,z for body 2 EARTH
X[7]=0.0055607; X[8]=0.; X[9]=0.0021;//x,y,z for body 3 SUN

X[10]=0.2; X[11]=1.0327; X[12]=0.0005; 	//Vx,Vy,Vz for body 1
X[13]=0.1; X[14]=0.6320; X[15]=0.0005; 	//Vx,Vy,Vz for body 2
X[16]=0.01; X[17]=0.0022967; X[18]=0.0000015;//Vx,Vy,Vz for body 3

init_y_arr(y, X);//How sould i write it?

}

int main()
{
double *M = new double[N+1];		//the masses of the bodies
double *X = new double[6*N+1];		//time,positions,velocities of N bodies
double *y = new double[6*(N-1)+1];	//time,positions,velocities of N-1 bodies

initialise( M, X, y);

for (int i=0; i<=6*(N-1); ++i)
cout<<y[i]<<endl;

return 0;
}```

8. ## Re: passing an array as a pointer to a function

Code:
`init_y_arr(y, X);//How sould i write it?`
What's wrong with this?

9. ## Re: passing an array as a pointer to a function

because STL vector is a class, and I include an extra library...

Of course for programs that has to run a short time, this difference is insignificant, but for programs that has to run for hours or days, I have to take that in mind..
Take what in mind? Why do you think that as STL vector is a class it includes an extra library? and why do you think that using a class like a vector has a performance impact over managing the memory allocation and access direct in the program?

10. Elite Member Power Poster
Join Date
Nov 2003
Location
Florida
Posts
12,635

## Re: passing an array as a pointer to a function

I'd like to suggest that you get out of the habit of putting multiple statements on one line. That code is pretty messy to read. As you develop larger apps, you need to make sure they're easy to read and debug. Multiple statements on a single line goes against that goal.

Also, avoid single character or meaningless variable names. You know what they mean now, but you won't a year from now and neither will the next guy that has to look at your code.

11. ## Re: passing an array as a pointer to a function

Originally Posted by nebula987
I haven't read that in an article, I suppose that needs more time, because STL vector is a class, and I include an extra library...

Of course for programs that has to run a short time, this difference is insignificant, but for programs that has to run for hours or days, I have to take that in mind..
I have to tell you that you are wrong.
And I am 100% sure that your own array and memory management is much less effective than the STL vector class has.

12. Banned
Join Date
Aug 2013
Posts
55

## Re: passing an array as a pointer to a function

Originally Posted by nebula987
I haven't read that in an article, I suppose that needs more time, because STL vector is a class, and I include an extra library...

Of course for programs that has to run a short time, this difference is insignificant, but for programs that has to run for hours or days, I have to take that in mind..
Both std::array and std::vector are defined to have a contiguous memory layout (as of the C++ 11 standard). This means they are guaranteed to be as efficient as old-style C arrays (you're currently using) when it comes to accesses.

Note that if you allocate heap memory using new[] you also need to deallocate it using delete[]. Otherwise you risk a memory leak.
Last edited by zizz; April 25th, 2014 at 04:05 AM.

13. Junior Member
Join Date
Apr 2014
Posts
5

## Re: passing an array as a pointer to a function

Originally Posted by 2kaud
Code:
`init_y_arr(y, X);//How sould i write it?`
What's wrong with this?
sorry! It works fine..! I have messed it up to this thread.. :-D

Originally Posted by GCDEF
I'd like to suggest that you get out of the habit of putting multiple statements on one line. That code is pretty messy to read. As you develop larger apps, you need to make sure they're easy to read and debug. Multiple statements on a single line goes against that goal.

Also, avoid single character or meaningless variable names. You know what they mean now, but you won't a year from now and neither will the next guy that has to look at your code.
I am going to agrree with you, I will have it in my mind...

I have to tell you that you are wrong.
And I am 100% sure that your own array and memory management is much less effective than the STL vector class has.
Both std::array and std::vector are defined to have a contiguous memory layout (as of the C++ 11 standard). This means they are guaranteed to be as efficient as old-style C arrays (you're currently using) when it comes to accesses.

Note that if you allocate heap memory using new[] you also need to deallocate it using delete[]. Otherwise you risk a memory leak.
For the 2 posts above, I must say that I don't know so much deep details about how effective is using arrays or vectors, although thank you for your suggestions..!
The reason why I reject the vector method is because I am doing a project for a master and I have to do a code that is going to be used to an already existing code, and I have to do it that that way...

14. Elite Member Power Poster
Join Date
Jan 2006
Location
Singapore
Posts
6,765

## Re: passing an array as a pointer to a function

Originally Posted by nebula987
The reason why I reject the vector method is because I am doing a project for a master and I have to do a code that is going to be used to an already existing code, and I have to do it that that way...
That is not a good reason to reject the usage of std::vector or std::array. You can always write your own using these standard library facilities, and then expose them as pointers to the first element of an array when you need to interface with code that cannot handle them, but that can handle such pointers.

15. Elite Member Power Poster
Join Date
Apr 2000
Location
Belgium (Europe)
Posts
4,626

## Re: passing an array as a pointer to a function

Originally Posted by zizz
Both std::array and std::vector are defined to have a contiguous memory layout (as of the C++ 11 standard). This means they are guaranteed to be as efficient as old-style C arrays (you're currently using) when it comes to accesses.
This is slightly off.

it'll be as efficient as old-style ALLOCATED C style array.

by it's very nature, a C style array does not need allocation or deallocation, nor does it need (indirect) access through a pointer.

in that respect c-style array -> std::array<>
heap allocated c-style array -> std::vector<>

there are cases where local vs heap allocated might be an important decision factor.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Click Here to Expand Forum to Full Width

Featured