Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 12th, 2007, 6:43 AM   #11
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Nov 12th, 2007, 9:26 AM   #12
Gabriel Margarido
Newbie
 
Join Date: Nov 2007
Posts: 9
Rep Power: 0 Gabriel Margarido is on a distinguished road
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.
Gabriel Margarido is offline   Reply With Quote
Old Nov 12th, 2007, 9:36 AM   #13
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Nov 22nd, 2007, 6:14 AM   #14
Gabriel Margarido
Newbie
 
Join Date: Nov 2007
Posts: 9
Rep Power: 0 Gabriel Margarido is on a distinguished road
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)
  1. void example(double *M, int row, int col) {
  2. ...
  3. }
and I have a matrix allocated as
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));
  ...
}
is there a way I can call function 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.
Gabriel Margarido is offline   Reply With Quote
Old Nov 22nd, 2007, 8:02 AM   #15
Salem
Programmer
 
Join Date: Nov 2007
Posts: 33
Rep Power: 0 Salem is on a distinguished road
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.
Salem is offline   Reply With Quote
Old Nov 22nd, 2007, 10:32 AM   #16
Gabriel Margarido
Newbie
 
Join Date: Nov 2007
Posts: 9
Rep Power: 0 Gabriel Margarido is on a distinguished road
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.
Gabriel Margarido is offline   Reply With Quote
Old Nov 23rd, 2007, 3:04 AM   #17
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,033
Rep Power: 5 lectricpharaoh will become famous soon enough
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 array
I'll leave freeing the memory as an exercise for you. If you want to create a dynamic array with more than two dimensions, you'd just increase the number of levels of indirection. The advantage to this approach is memory can be more fragmented without it failing, since the largest contiguous block you need is only the size of an individual row, or the collection of row pointers, whichever is greater. The disadvantages are that it's more complex, leading to a greater chance of memory leaks, and (because the memory blocks can be scattered) it may exhibit poorer cache performance (this is probably a minor issue, depending on the length of each row).

The 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;
This approach is simpler because there are only two allocations. If the first allocation fails, you can try with a smaller array, and if the second fails, you only need to free() the first block before retrying. Again, you can expand this to arrays with more than two dimensions by adding in another level of indirection, but I'll leave that up to you.

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;
You'd then write a function to allocate the array using one of the above methods (though you'd need to add another member to the structure if using the second method), another to free it, and others to manipulate the contents. A benefit here is you can pass all the information about your array, including size, with a single pointer. Another benefit comes when allocating the array; having a single call to a function that can return a success/failure code makes error handling a lot easier.

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
lectricpharaoh is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:45 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC