Operating System - Tru64 Unix
1753814 Members
7850 Online
108805 Solutions
New Discussion юеВ

Re: Unix V4.0 1229 alpha , C programing 2 x array addressing

 
Bob Hayward
Occasional Advisor

Unix V4.0 1229 alpha , C programing 2 x array addressing

Hello,

I've written a spam filter in C. When I port it
of to the alpha cluster it compiles fine but
core dumps while initializing a floating point
array which was dynamically allocated.
Size is 5466 x 5466.

The debuger tells me the SIGSEGV occured in
this function. line--> a[i][j] = .1;

It is called from
get_new_matrice(float **a,int size)
which creates a new 2 x matrice.

void
set_to_zero(float **a,int size)
{
int i,j;

for(i = 0;i < size;i++){
for(j = 0;j < size;j++){
a[i][j] = .1;
}
}
}

This code works on RedHat 9.0 and Windows XP.
Why a seg fault on such a basic operation.

How does the Unix "v4.0 1229 alpha" need to
coded to allow the array to be initialized,
or does the alpha unix addresses arrays
differently. I use calloc so everything is set
to zero, but must be reset to .1 .

Thanks for any help on this.

I attached the full code, matrices.c

Bob Hayward




9 REPLIES 9
Hein van den Heuvel
Honored Contributor

Re: Unix V4.0 1229 alpha , C programing 2 x array addressing


As a small logic test, perhaps try it with a much smaler matrix? 1000 x 1000 ?

I could not try here as you did not seem to have attached "matrices.h"

Maybe you experienced a simple ulimit problem? It is in the ballpark. For example on a box here I get:
> ulimit -a
time(seconds) unlimited
file(blocks) unlimited
data(kbytes) 131072
stack(kbytes) 8192
memory(kbytes) 4068040
coredump(blocks) unlimited
nofiles(descriptors) 4096
vmemory(kbytes) 4194304

The data component there is 131 MB which is right around 5466*5466 = 30 M floats no?

So check sysconfig to bump data into gig space?

hth,
Hein.


Bob Hayward
Occasional Advisor

Re: Unix V4.0 1229 alpha , C programing 2 x array addressing

Attached is matrices.h

I once tried 450 x 450 with the same result.
Our help people don't think it is memory.
I don't think it is a bug or coding, how
else does one address an array in C.

Humm what does data 131072 kbytes
refer to if not ram memory since it is
1048576 kbytes.

Thanks for the response.
Bob Hayward
Occasional Advisor

Re: Unix V4.0 1229 alpha , C programing 2 x array addressing

Hello Hein,

Thank your for your time. It turns out we have
a linux cluster RH 7.0 also and it runs on it. Still am curious, but is no longer an issue.

Take care,

Bob
Hein van den Heuvel
Honored Contributor

Re: Unix V4.0 1229 alpha , C programing 2 x array addressing


Ah, I think I C it now...

You address the floats as if they were stored in flat address space: a[n][m] but you allocate n independend and not necesarily contiguous chuncks of m floats: *b[n]

I believe this violates C language as for example outlined in K&R 5.9 " Pointers vs. Multi-dimensional Arrays"1 (2nd edition)

If I am correct then you were very unfortunate to have this work at all. It may fail at any time on any platform. Had you worked on Tru64 from the get go, then you would have found your problem sooner. (You might get it to work 'accidently' on Tru64 by doing a large sbreak to make the malloc code request all required VM from the OS in one swoop.

hth,
Hein.
Ralf Puchner
Honored Contributor

Re: Unix V4.0 1229 alpha , C programing 2 x array addressing

this is a typical beginner problem:

allocating memory for an array of pointers but using the size of the data.

Have a look into your get_new_matrice() function. The line

a = (float**)calloc(size,sizeof(float));

should be

a = (float**)calloc(size,sizeof(float*));

because you are allocating memory for an array of pointers to additional memory and not for the values itself. This is also the reason why there was no problem on 32-bit machines.

Btw. compiling your program leads to definition problems of the prototypes.
Help() { FirstReadManual(urgently); Go_to_it;; }
Bob Hayward
Occasional Advisor

Re: Unix V4.0 1229 alpha , C programing 2 x array addressing

Thank you so much. I tried playing around with
how I was allocating. I got the basic concept
off the web. So worked with it around with it
until the compiler to accepted it. Could fine
little documentation in the books on how to do
this.

http://www.eskimo.com/~scs/cclass/int/sx9b.html

Bob Hayward
Bob Hayward
Occasional Advisor

Re: Unix V4.0 1229 alpha , C programing 2 x array addressing

One more question. Do have a good resource on
C for multiplatforms.

I'm pasted this one, but now am getting SIGFPE
using if(a[i][j] == ZERO) ZERO is 0.1E0.

My current C book only mentions the IEEE
floating point standards.

Thanks again,

Bob Hayward
Ralf Puchner
Honored Contributor

Re: Unix V4.0 1229 alpha , C programing 2 x array addressing

this error could not be seen, but why not defining ZERO as 0.1f ?

Btw. your allocation and zero mechanism is quite slow and not really "C"-like. Why not using pointers and offsets?
Help() { FirstReadManual(urgently); Go_to_it;; }
Bob Hayward
Occasional Advisor

Re: Unix V4.0 1229 alpha , C programing 2 x array addressing

I'm setting up a probability matrice using markov chains, to build a spam filter. So
we define zero as .1 to avoid divide by zero
errors when we compute the probabilities.

Thanks for the advice of the pointers and offsets. I'll check it out.