logo
down
shadow

pointer to preallocated memory as an input parameter and have the function fill it


pointer to preallocated memory as an input parameter and have the function fill it

By : jOkes7
Date : November 22 2020, 07:01 PM
To fix this issue Test code: ,
code :


Share : facebook icon twitter icon
How can I allocate memory and return it (via a pointer-parameter) to the calling function?

How can I allocate memory and return it (via a pointer-parameter) to the calling function?


By : André Bouatchidzé
Date : March 29 2020, 07:55 AM
like below fixes the issue I have some code in a couple of different functions that looks something like this: , You want to use a pointer-to-pointer:
code :
void someFunction (int **data) {
  *data = malloc (sizeof (int));
}

void useData (int *data) {
  printf ("%p", data);
}

int main () {
  int *data = NULL;

  someFunction (&data);

  useData (data);

  return 0;
}
Fit function with preallocated buffer memory in scipy.optimize.leastsq

Fit function with preallocated buffer memory in scipy.optimize.leastsq


By : Noel Morgan
Date : March 29 2020, 07:55 AM
hope this fix your issue Ok, I think this is what happens here: The underlying FORTRAN routine LMDIF presents the user-defined function with memory in *fvec, where the result is to be stored. This pointer may point to scratch memory, since LMDIF needs to cache the results of several function evaluations to estimate the Jacobian.
Since the user-defined function is called from Python, the memory in *fvec cannot be used directly, so the wrapper raw_multipack_lm_function() works by evaluating the Python function first and then coping the result to *fvec. Before entering LMDIF, the user-defined function is called once to find out the shape of the output array.
code :
diff --git a/scipy/optimize/__minpack.h b/scipy/optimize/__minpack.h
index 2c0ea33..465724b 100644
--- a/scipy/optimize/__minpack.h
+++ b/scipy/optimize/__minpack.h
@@ -483,7 +483,7 @@ static PyObject *minpack_lmdif(PyObject *dummy, PyObject *args) {
   qtf = (double *) ap_qtf->data;
   fjac = (double *) ap_fjac->data;
   ldfjac = dims[1];
-  wa = (double *)malloc((3*n + m)* sizeof(double));
+  wa = (double *)malloc((3*n + 2*m)* sizeof(double));
   if (wa == NULL) {
     PyErr_NoMemory();
     goto fail;
@@ -492,12 +492,15 @@ static PyObject *minpack_lmdif(PyObject *dummy, PyObject *args) {

   /* Call the underlying FORTRAN routines. */
   n_int = n; /* to provide int*-pointed storage for int argument of LMDIF */
-  LMDIF(raw_multipack_lm_function, &m, &n_int, x, fvec, &ftol, &xtol, &gtol, &maxfev, &epsfcn, diag,
-    
+  LMDIF(raw_multipack_lm_function, &m, &n_int, x, wa+3*n+m, &ftol, &xtol, &gtol, &maxfev, &epsfcn, d
+
   RESTORE_FUNC();

   if (info < 0) goto fail;           /* Python error */

+  /* Copy final residuals back to initial array */
+  memcpy(fvec, wa+3*n+m, m*sizeof(double));
+
   free(wa);
   Py_DECREF(extra_args); 
   Py_DECREF(ap_diag);
In [1]: def dy0(p, x, y): return p[0]*np.exp(-p[1]*x) - y

In [2]: %timeit p = opt.leastsq(dy2, p0, args=(x, buf, y))
1000 loops, best of 3: 399 us per loop

In [3]: %timeit p = opt.leastsq(dy, p0, args=(x, buf, y))
1000 loops, best of 3: 363 us per loop

In [4]: %timeit p = opt.leastsq(dy0, p0, args=(x, y))
1000 loops, best of 3: 341 us per loop
In [5]: %timeit p = mp.leastsq(dy, (p0.copy(), x, buf, y))
1000 loops, best of 3: 279 us per loop
In [6]: %timeit p = opt.leastsq(fitfun.e2_diff, p0, args=(x, buf, y))
10000 loops, best of 3: 48.2 us per loop
In [7]: %timeit p = mp.leastsq(fitfun.e2_diff, (p0.copy(), x, buf, y))
100000 loops, best of 3: 6.94 us per loop
In [8]: %timeit p = fitfun.e2_fit(p0.copy(), x, buf, y)
100000 loops, best of 3: 6.13 us per loop
Fill out preallocated matrix in R

Fill out preallocated matrix in R


By : Jaxs010
Date : March 29 2020, 07:55 AM
I hope this helps . Test the size of the matrix and increase its size by chunks. Here is an example:
code :
set.seed(42)
mat <- matrix(NA, ncol=2, nrow=10)
i <- 1
repeat {
  if (i > nrow(mat)) mat <- rbind(mat, matrix(NA, ncol=2, nrow=10))
  x <- rnorm(1)
  y <- rnorm(1, mean=3)
  mat[i,] <- c(x, y)
  if (x > y) break
  i <- i+1  
}

mat[seq_len(i),]
How to avoid memory leak when passing function-returned pointer as input to other function?

How to avoid memory leak when passing function-returned pointer as input to other function?


By : Jany García
Date : March 29 2020, 07:55 AM
hope this fix your issue C does not provide much help in dealing with this automatically. You can store pointers to temporary objects, and free them at the end of each iteration:
code :
while (len--){
    Matrix t1, t2, t3, t4;
    Matrix E = matAdd(D, t1=matAdd(D, t2=matMult(t3=matMult(A, B),C)));
    mprint(t4=matMult(A, B));
    mprint(A);
    mprint(E);
    matFree(t1);
    matFree(t2);
    matFree(t3);
    matFree(t4);
}
how to allocate memory for a pointer parameter to an array through a function in C#

how to allocate memory for a pointer parameter to an array through a function in C#


By : Craig Elliott
Date : March 29 2020, 07:55 AM
wish helps you If the unmanaged code you are calling requires a pointer to the buffer for a long time then you do not want to be passing a managed buffer in the first place. Why? Because the managed buffer then has to be pinned for a long time, and that makes the garbage collector do extra work.
Instead, allocate an unmanaged buffer with an unmanaged memory allocator, and pass a pointer to that to the unmanaged code.
Related Posts Related Posts :
  • How to create ActiveX DLL in Visual C++
  • Generic allocator class without variadic templates?
  • Comparation in JNI
  • Using a function with reference as a function with pointers?
  • How to initialize an unsigned long long type?
  • How to practically customize IE context menu?
  • Trying to Create a ToolBar with an ImageList, not working
  • How does the destructor know when to activate itself? Can it be relied upon?
  • Conway's Game of Life - C++ and Qt
  • Accessing any structs members at run-time
  • Including #includes in header file vs source file
  • How does switch compile in Visual C++ and how optimized and fast is it?
  • How to override nested C++ objects methods?
  • penalty for "inlined" classes
  • C++ Library for implementing a web services api over legacy code?
  • C++-Singleton class
  • Floating point precision in Visual C++
  • Compiler error when overriding virtual methods
  • What is the point of function pointers?
  • Which is faster in memory, ints or chars? And file-mapping or chunk reading?
  • ISO file in C++
  • Expected Class-name before { token
  • Can I trust floats or doubles representing integers to retain precision?
  • Qt execute multiple get with QNetworkAccessManager
  • Big and Little endian question
  • Visual Studio 2008 compiles anything in C++ file?
  • C++ pointer to functions, Beginner Question
  • Fastest algorithm for primality test
  • Passing array of pointers to another class
  • Handling macro redefinition without modifying .h files ... C / C++ language
  • Implement SSL with SSPI: How to start?
  • Creating a simple VS2008 visualizer inside autoexp.dat (problem with casting)
  • C++ - Unicode Newline
  • Program crashing with 'std::out_of_range' error
  • Use signed or unsigned char in constructing CString?
  • Parallel execution policies in C++ 17
  • C++ Class Templates (Queue of a class)
  • Convert image into useable byte array in C?
  • Animated Image in Win32
  • error on compiling statically linked library created in Qt
  • How to easily pass a very long string to a worker process under Windows?
  • Can a class add friend classes at runtime in C++?
  • C++ priority queue in ascending order by specific method for objects
  • Initialize and declare dynamically multiple variables of random entities in a loop in C++
  • Serializing, integer without libaries , getting weird results
  • Function pointers with default parameters in C++
  • Compilation errors with RapidXML
  • Bad_alloc exception when using new for a struct c++
  • count specific things within a code in c++
  • How to extract 48*48 icon image from resource of an executable file?
  • C++ Convert string (or char*) to wstring (or wchar_t*)
  • C++ include .h includes .cpp with same name as well?
  • Any way in C++ to forward declare a function prototype?
  • boost graph adjacency_list, retrieving a node's parents
  • Copy method optimization in compilers
  • what the problem in this program? it doesn't use the second number, and doesn't print the last line?
  • Need to format character precedence in Strings
  • Cross-Platform Camera API
  • sizeof continues to return 4 instead of actual size
  • String search algorithm used by string::find() c++
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk