fopen problem when allocating big matrices : need help 
Author Message
 fopen problem when allocating big matrices : need help

Hi,

I have a problem and I can't figure how to solve it. I wrote a little C
program that creates 9 matrices and do computation with them. Then I
open a file for saving the results in an ASCII file (in a matrix
too). The problem is when the matrices size is greater than 256*256, the
program crashes when it tries to open the file. (note : I used Linux
Mandrake/KDevelop)

Thanks in advance

Ludovic Grossard

Here is a sample of the (crashing) code :

#include <iostream.h>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <string.h>

        const kmax=1;
        const imax=256;
        const jmax=256;

int main()
{

        int i,j,k;

  //memory allocation (9 matrices but just one in this example)

        float **a = (float **) malloc((imax+1) * sizeof(float *));
        for(i = 0 ; i< (imax+1) ; i++)
                a[i] = (float *) malloc((jmax+1) * sizeof(float));

// then linear calculations with those 9 matrices

// save data

        FILE *fp;
        //int code;
        //char fichier[20];
        //char buffer[80];
        //code=sprintf(fichier,"v-%d.dat",k);
        //cout << "Itration " << k << "\tfichier : " << fichier <<
endl;
        fp = fopen(fichier, "w");

        //for(int i=1;i<imax+1;i++){
        //      for(int j=1;j<jmax+1;j++){
        //              code=sprintf(buffer,"%20.5f\t",matrice[i][j]);
        //              fputs(buffer,fp);
        //              }
        //              strncpy(buffer,"\n",2);
        //              fputs(buffer,fp);
        //}
        fclose(fp);

Quote:
}



Tue, 24 Sep 2002 03:00:00 GMT  
 fopen problem when allocating big matrices : need help

Quote:

> I have a problem and I can't figure how to solve it. I wrote a little C
> program that creates 9 matrices and do computation with them. Then I
> open a file for saving the results in an ASCII file (in a matrix
> too). The problem is when the matrices size is greater than 256*256, the
> program crashes when it tries to open the file. (note : I used Linux
> Mandrake/KDevelop)
> Here is a sample of the (crashing) code :

> #include <iostream.h>

This is C++, not C; take it to comp.lang.c++.

Richard



Tue, 24 Sep 2002 03:00:00 GMT  
 fopen problem when allocating big matrices : need help
Quote:

> Hi,

> I have a problem and I can't figure how to solve it. I wrote a little C
> program that creates 9 matrices and do computation with them. Then I
> open a file for saving the results in an ASCII file (in a matrix
> too). The problem is when the matrices size is greater than 256*256, the
> program crashes when it tries to open the file. (note : I used Linux
> Mandrake/KDevelop)

> Thanks in advance

> Ludovic Grossard

> Here is a sample of the (crashing) code :

> #include <iostream.h>
> #include <stdlib.h>
> #include <math.h>
> #include <stdio.h>
> #include <string.h>

>         const kmax=1;
>         const imax=256;
>         const jmax=256;

the constants should be declared as int!

Quote:
> int main()
> {

good main returns an int!!

- Show quoted text -

Quote:

>         int i,j,k;

>   //memory allocation (9 matrices but just one in this example)

>         float **a = (float **) malloc((imax+1) * sizeof(float *));
>         for(i = 0 ; i< (imax+1) ; i++)
>                 a[i] = (float *) malloc((jmax+1) * sizeof(float));

> // then linear calculations with those 9 matrices

> // save data

>         FILE *fp;
>         //int code;
>         //char fichier[20];
>         //char buffer[80];
>         //code=sprintf(fichier,"v-%d.dat",k);
>         //cout << "Itration " << k << "\tfichier : " << fichier <<
> endl;
>         fp = fopen(fichier, "w");

>         //for(int i=1;i<imax+1;i++){
>         //      for(int j=1;j<jmax+1;j++){
>         //              code=sprintf(buffer,"%20.5f\t",matrice[i][j]);
>         //              fputs(buffer,fp);
>         //              }
>         //              strncpy(buffer,"\n",2);
>         //              fputs(buffer,fp);
>         //}
>         fclose(fp);
> }

where is your return statement?

This very much looks like c-- code, so please ask an other ng!
We only want to cope with the C language!!!!!!!!!!!!!!!!!!!!!!!



Tue, 24 Sep 2002 03:00:00 GMT  
 fopen problem when allocating big matrices : need help
Well, for starters, you might want to use C++ file I/O, since you're
already using C++ for output.  Second, I would #define those consts.
I'm not sure why, I just would =-).  Third, you use k without
initializing it.  You just declare it, then use it in sprintf(), which
might have something to do with it crashing.

~{*filter*}cosis

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Tue, 24 Sep 2002 03:00:00 GMT  
 fopen problem when allocating big matrices : need help
On Fri, 07 Apr 2000 14:09:56 +0200, Ludovic Grossard

Quote:

>Hi,

>I have a problem and I can't figure how to solve it. I wrote a little C
>program that creates 9 matrices and do computation with them. Then I
>open a file for saving the results in an ASCII file (in a matrix
>too). The problem is when the matrices size is greater than 256*256, the
>program crashes when it tries to open the file. (note : I used Linux
>Mandrake/KDevelop)

>Thanks in advance

>Ludovic Grossard

>Here is a sample of the (crashing) code :

>#include <iostream.h>
>#include <stdlib.h>
>#include <math.h>
>#include <stdio.h>
>#include <string.h>

>        const kmax=1;
>        const imax=256;
>        const jmax=256;

Those declarations should be "const int", not just "const".  Implicit type
will cease to be ANSI C as soon as ANSI approves ISO C99.

Quote:

>int main()
>{

>        int i,j,k;

>  //memory allocation (9 matrices but just one in this example)

>        float **a = (float **) malloc((imax+1) * sizeof(float *));
>        for(i = 0 ; i< (imax+1) ; i++)
>                a[i] = (float *) malloc((jmax+1) * sizeof(float));

By commenting out the C++ code which followed, you've actually made this
into C code, though not strictly conforming, because it #includes a
non-standard header, so it actually turns out to be relatively on-topic for
comp.lang.c, and since you appear to be trying to write code which is, as
much as possible, both C and C++, I won't even give you the standard
comp.lang.c scolding for casting malloc, especially with a visible include
of <stdlib.h>.

The real problem is that you never check the results of your calls to
malloc.  If the crash occurs only when you increase the size of your
allocations, it's almost certainly because one of the calls to malloc is
failing, and by not checking for a NULL return from malloc, you're causing
your program, later on, to write through a NULL pointer, and the resulting
undefined behavior, in this case, is a delayed crash in fopen().
--

Kenan Systems Corp., a wholly-owned subsidiary of Lucent Technologies



Tue, 24 Sep 2002 03:00:00 GMT  
 fopen problem when allocating big matrices : need help
Hello.

I can't guess why line "strncpy(buffer, "\n", 2)"  is to execute.
If  obeying this line, the value of matrice[any][jmax] have to be written
twice in a file.
Simply this line may be "strcpy(buffer, "\n")"?


  for (int i=1;i<imax+1;i++){
     for(int j=1;j<jmax+1;j++){
           code=sprintf(buffer,"%20.5f\t",matrice[i][j]);
           fputs(buffer,fp);
         }
           // strncpy(buffer,"\n",2); <== why??
           strcpy(buffer, "\n");
           fputs(buffer,fp);
         }
    fclose(fp);

Quote:
}

Regards,
--
Takashi Itoh



Wed, 25 Sep 2002 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Problems allocating vectors or matrices with Borland C++

2. Need help with fopen, file ptr problem

3. Big Problem in C : Need an urgent help - projtmp.cpp (0/1)

4. Big Problem in C : Need an urgent help - projtmp.cpp (1/1)

5. Very strange MFC behaviour and BIG problem, urgent help needed

6. Allocating memory for a matrix

7. How to use malloc() to allocate matrix

8. Symmetric matrices: memory-friendly allocating and (efficient) referencing?

9. Dynamically allocating n-dimensional matrices.

10. READING BIG ASCII DATA MATRIX WITH SCANF

11. Newbie - fopen help needed

12. Need help with fopen() in Borland C++ 4.5

 

 
Powered by phpBB® Forum Software