![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Re: Incompatible pointer type (double pointer in a function)
Let me caution you about preoptimizing your code. If adding logarithms were faster than multiplying, the machine would implement multiplication by adding logarithms, don't you think?
Optimization, particularly in these days of on-chip cache, is an area for experts. Generally speaking, the compiler writer knows more about it than the user. Write your program and make it work. Then run a profiler and find out what needs to be optimized. You might want to have a look at this.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#12 |
|
Newbie
Join Date: Nov 2007
Posts: 9
Rep Power: 0
![]() |
Re: Incompatible pointer type (double pointer in a function)
Thank you for the tip! I'll spend some more time with that link later.
I'm aware of optimization... But my intention to use logarithms is basically due to precision (likelihoods, for instance, can be really small). Maybe I can use logs just on these parts. See you, Gabriel. |
|
|
|
|
|
#13 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Re: Incompatible pointer type (double pointer in a function)
You are correct that doubles/floats can raise questions of accuracy/precision. You might want to investigate arbitray-precision (bignum) arithmetic.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#14 |
|
Newbie
Join Date: Nov 2007
Posts: 9
Rep Power: 0
![]() |
Re: Incompatible pointer type (double pointer in a function)
Hello, DaWei.
Sorry to bother you one more time... I managed to do my functions work again!! On the other hand, another doubt came up: if a function expects a pointer like c Syntax (Toggle Plain Text)
void main() {
int i;
double **M;
M = (double **)R_alloc(p1, sizeof(double *));
for (i=0;i<p1;i++) M[i] = (double *)R_alloc(p1, sizeof(double));
...
}example converting M with a cast?I know the pointers to each row are pointing to different locations in the memory (M is not a contiguous array) and I've managed to do it work, for example, for just one row (and the others get messed up). Do I have to rewrite my code, allocating every matrix as a 1 dimension array?? Thank you in advance. Gabriel. |
|
|
|
|
|
#15 |
|
Programmer
Join Date: Nov 2007
Posts: 33
Rep Power: 0
![]() |
Re: Incompatible pointer type (double pointer in a function)
> void main()
main returns an int. http://c-faq.com/ansi/voidmain.html > M = (double **)R_alloc(p1, sizeof(double *)); I'm going to assume R_alloc is a wrapper around malloc, and the return type of R_alloc is void*. It is not necessary to cast a void* in C, the compiler will do it for you. http://c-faq.com/malloc/decl.html What the cast will do however is hide that you either failed to include stdlib.h, or are trying to compile C with a C++ compiler. If it's a wrapper for calloc, you might want to take note of this as well. http://c-faq.com/malloc/calloc.html > is there a way I can call function example converting M with a cast? You should really find better ways to solve the problem. Indiscriminate casting just to make the compiler shut up rarely produces good long term results. There is a way to allocate a dynamic 2D which results in all the data being in contiguous memory. #include <stdio.h>
#include <stdlib.h>
#define MAX_ROW 10
#define MAX_COL 32
int main ( ) {
double **p;
double *rp;
int row;
p = malloc( MAX_ROW * sizeof *p );
p[0] = malloc( MAX_ROW * MAX_COL * sizeof *p[0] );
for ( row = 0, rp = p[0] ; row < MAX_ROW ; row++, rp += MAX_COL ) {
p[row] = rp;
}
for ( row = 0 ; row < MAX_ROW ; row++ ) {
printf( "Row %d begins at %p\n", row, (void*)p[row] );
}
free( p[0] );
free( p );
return 0;
}
# My results
$ gcc foo.c
$ ./a.exe
Row 0 begins at 0x660188
Row 1 begins at 0x660288
Row 2 begins at 0x660388
Row 3 begins at 0x660488
Row 4 begins at 0x660588
Row 5 begins at 0x660688
Row 6 begins at 0x660788
Row 7 begins at 0x660888
Row 8 begins at 0x660988
Row 9 begins at 0x660a88
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. |
|
|
|
|
|
#16 |
|
Newbie
Join Date: Nov 2007
Posts: 9
Rep Power: 0
![]() |
Re: Incompatible pointer type (double pointer in a function)
Dear Salem,
Thanks for your reply. This code is actually used to create a shared library to be loaded by software R. I won't really have a main section there (just used void main() here as an example - sorry for this mistake).R_alloc is a function used by R to allocate memory... Used instead of malloc, not calloc, as you figured. Yes, I agree with you about using these casts. I think it will be better on the long run to rewrite my code, treating matrixes as one-dimensional arrays. Thank you again. Cheers, Gabriel. |
|
|
|
|
|
#17 |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,033
Rep Power: 5
![]() |
Re: Incompatible pointer type (double pointer in a function)
For doing dynamic memory allocation of multidimensional arrays, there are two main approaches.
The first is to allocate an array of pointers to pointers. You then loop through, allocating a block of memory for each pointer. If I were to do this for a 2D array of type int, it would look something like this:int **array;
if(!(array = malloc(ROWS * sizeof(int *))))
// unable to allocate array, handle error here
else
{
for(int x=0; x<ROWS; ++x)
if(!(array[x] = malloc(COLUMNS * sizeof(int))))
// error condition, unable to allocate row
// remember to free any successfully-allocated rows
}
.
.
array[row][column] = 57; // use arrayThe second approach is to allocate one big block. After doing this, you allocate a second block to store the row pointers. Again, assuming an array of type int, it might look like this:int *array_space, **array;
if(!(array_space = malloc(ROWS * COLUMNS * sizeof(int))))
// error, unable to allocate space for array
else if(!(array = malloc(ROWS * sizeof(int *))))
// error, unable to allocate space for row pointers
else
for(int x=0; x<ROWS; ++x)
array[x] = x * COLUMNS * sizeof(int) + array_space;Another thing you could try is to use a structure. This way, you could have structure members (pointers) that hold the dynamic array, as well as other members that describe the dimensions. You could then pass this structure by reference to functions, ie by passing its address, and those functions could look at the appropriate members to know how to deal with the array: typedef struct
{
int **array;
size_t rows, columns;
} dynamic2D_t;A final warning: I haven't slept much lately, and haven't bothered compiling the code, so while I think it's okay, don't lynch me if there's a minor error or two in there. ![]()
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot. - Vaarsuvius, Order of the Stick |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| warning: assignment from incompatible pointer type warning in a function poiter | jakh | C | 5 | Aug 16th, 2007 11:05 PM |
| double pointer question | rwm | C++ | 3 | Apr 16th, 2007 3:12 AM |
| Pointer return type,.. ??? | Mack1982 | C++ | 28 | May 26th, 2006 8:02 PM |
| Function pointer to a member function | Jimbo | C++ | 3 | May 12th, 2006 4:53 PM |
| function pointer | raom | C | 9 | Nov 18th, 2005 2:42 PM |