passing an array as a pointer to a function
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 30

Thread: passing an array as a pointer to a function

  1. #1
    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. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,844

    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?
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    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. #4
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,844

    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[])
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,604

    Re: passing an array as a pointer to a function

    Quote Originally Posted by nebula987 View Post
    ... 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?
    Victor Nijegorodov

  6. #6
    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. #7
    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. #8
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,844

    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?
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  9. #9
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,844

    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?
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,228

    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. #11
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,604

    Re: passing an array as a pointer to a function

    Quote Originally Posted by nebula987 View Post
    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.
    Victor Nijegorodov

  12. #12
    Join Date
    Aug 2013
    Posts
    55

    Re: passing an array as a pointer to a function

    Quote Originally Posted by nebula987 View Post
    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 05:05 AM.

  13. #13
    Join Date
    Apr 2014
    Posts
    5

    Re: passing an array as a pointer to a function

    Quote Originally Posted by 2kaud View Post
    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


    Quote Originally Posted by GCDEF View Post
    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. #14
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,391

    Re: passing an array as a pointer to a function

    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  15. #15
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,100

    Re: passing an array as a pointer to a function

    Quote Originally Posted by zizz View Post
    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.

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center