954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Modify arrays passed by reference

Hello, I've been creating a page in php that needs to invoke heavy calculation functions for which I've created dinamic extensions for linux (.so) in c++, but there's one problem I have in one of those functions, I need to modify an array passed to that function by reference and simply return true on it.
I've tried in different ways that I've found on the web, but nothing works.
Actually it does not compile, and I have no idea of how to modify that array within that function.

If somebody can give me a hand I'd really appreciate it.

angel_e1205
Newbie Poster
2 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

We ain't mind readers. please post some code

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

Please post your code so that we can render our help more effectively.

And btw as far as my knowledge goes, arrays are by default passed by reference in C / C++, keeping in mind that passing a huge chunk of data strucutre (array) can result in a greater overhead.

Still post your code and then we will see...

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

I suspect the problem is in the PHP code -- I don't know the first thing about PHP. But in C or C++ passing arrays is very common thing to do. Does php duplicate the array before passing it to c/c++ then toss it into the bit bucket after the c++ function returns to php ? From your description it sounds like that is what is happening. Maybe you need to pose this question on the PHP board, not the c/c++ board.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

ok, you're right I must post some code, this is what I have:

I need to pass this code from a regular c++ code to a php invoked .so library, this is the original code, which I need to translate in order to make my .so extension:

bool _stdcall Gauss(int n,double *mat, double *x)
{
    int i,j,z;
    double aux, pivote;

    for (i=0;i<n;++i){
        pivote = mat[i*(1+n) + i];
        for (j=i;j<n+1;++j)
            mat[i*(1 + n) + j] = mat[i*(1 + n) + j] / pivote;
        for (z=0;z<n;++z){
            if (z!=i){
                aux = mat[z*(1+n) + i];
                for (j=i;j<n+1;++j)
                    mat[z*(1+n) + j] =  mat[z*(1+n) + j] - (aux * mat[i*(1+n) + j]);
            }
        }
    }
    for (i=0;i<n;++i){
        x[i] = mat[i*(1+n) + n]; 
    }
    return true;
}

this was created for a windows dll but the application needs to be migrated to lunux, this should be, as far as I know the format to pass that function to a linux dinamic library:
[php]
#include "/usr/local/src/php-5.1.6/main/php.h"

PHP_FUNCTION(test);

static function_entry firstmod_functions[] = {
PHP_FE(test,NULL)
{NULL, NULL, NULL}
};

/* compiled module information */
zend_module_entry firstmod_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"test", // Extension name
firstmod_functions,
NULL,
NULL,
NULL,
NULL,
NULL,
#if ZEND_MODULE_API_NO >= 20010901
"0.1",
#endif
STANDARD_MODULE_PROPERTIES
};

ZEND_GET_MODULE(firstmod)

PHP_FUNCTION(test){
zval *mat

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &mat) == FAILURE) {
RETURN_STRING("No sirve con arrya",1);
}

else{
// do whatever I must do with the array, and here's where my problem is
RETURN_STRING("funciona con array",1);
}
}
[/php]
I hope I made myself clear enough, and pardon my english I'm actually not from the states or anywhere near any english speaking country.

angel_e1205
Newbie Poster
2 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

Looks like some form of gaussian elimination to me. Can't you just translate the c code to php cos it'll be easier to work just in php.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You