help segmentation fault

S

subirs

Hi,

I am trying to write a simple code in C using memory allocation. It
gives me segmentation fault. I have tried using mtrace. There are no
memory leaks and no segmentation fault unless I uncomment the line

for(i=0;i<=xcols;i++) x=0.0;
That is If I try to write to any of the dynamically allocated arrays
it gives me seg, fault. The code is attached , any helpis deeply
appreciated.

Regards
Subir

---------------------------------------------------------------------------------------------------

#include <stdio.h>

#include <stdlib.h>
#include <math.h>

#include<mcheck.h>



int main()

{
mtrace();


int trows=100;

int xcols=90;
int i,j,k;
double dx,dt,temp,temp1;


double **matu;



matu=malloc(trows* sizeof(*matu));

for(i=0; i<trows; i++)

{
matu=malloc(xcols * sizeof(*matu));

if ( matu == NULL)

{

printf("No space for allocation of ith=%d row",i);

exit(0);

}
}

double *x=malloc(xcols * sizeof(x));
double *t=malloc(trows * sizeof(t));

for(i=0;i<=xcols;i++) x=0.0;
//for(i=0;i<=trows;i++) t=0.0;

//for(i=0;i<=xcols;i++)
//for(j=0;j<=trows;j++)
//matu[j]=0.0;

/*dx=(1.0-0.0)/xcols;
dt=(1.0-0.0)/trows;

for(i=0;i<=xcols;i++)
x=x[0]+dx*i;

for(i=0;i<=trows;i++)
t=t[0]+i*dt; */



for(i=0; i<trows; i++)

free(matu);

free(matu);
free(x);
free(t);
muntrace();

return 0;

}
 
I

Ike Naar

Hi,

I am trying to write a simple code in C using memory allocation. It
gives me segmentation fault. I have tried using mtrace. There are no
memory leaks and no segmentation fault unless I uncomment the line

for(i=0;i<=xcols;i++) x=0.0;
That is If I try to write to any of the dynamically allocated arrays
it gives me seg, fault. The code is attached , any helpis deeply
appreciated.

Regards
Subir

---------------------------------------------------------------------------------------------------

#include <stdio.h>

#include <stdlib.h>
#include <math.h>

#include<mcheck.h>



int main()

{
mtrace();


int trows=100;

int xcols=90;
int i,j,k;
double dx,dt,temp,temp1;


double **matu;



matu=malloc(trows* sizeof(*matu));

for(i=0; i<trows; i++)

{
matu=malloc(xcols * sizeof(*matu));

if ( matu == NULL)

{

printf("No space for allocation of ith=%d row",i);

exit(0);

}
}

double *x=malloc(xcols * sizeof(x));
double *t=malloc(trows * sizeof(t));

for(i=0;i<=xcols;i++) x=0.0;
//for(i=0;i<=trows;i++) t=0.0;

//for(i=0;i<=xcols;i++)
//for(j=0;j<=trows;j++)
//matu[j]=0.0;

/*dx=(1.0-0.0)/xcols;
dt=(1.0-0.0)/trows;

for(i=0;i<=xcols;i++)
x=x[0]+dx*i;

for(i=0;i<=trows;i++)
t=t[0]+i*dt; */



for(i=0; i<trows; i++)

free(matu);

free(matu);
free(x);
free(t);
muntrace();

return 0;

}
 
I

Ike Naar

[...]

Sorry, made a mistake while creating the previous post.
Here's new attempt, hope this one works out better.
for(i=0;i<=xcols;i++) x=0.0;


x has only xcols elements, but the statement above clobbers the
xcols+1st element in the last iteration of the loop.

Small nit: ``int main(void)'' is better.
[...]
double *x=malloc(xcols * sizeof(x));

x is supposed to be an array of xcols elements of type double,
but this allocates room for xcols elements of type pointer-to-double.

Also, malloc can fail and return NULL, it is a good idea to check for this.
 
S

subirs

Hi,

I am trying to write a simple code in C using memory allocation.  It
gives  me segmentation fault. I have tried using mtrace. There are no
memory leaks and no segmentation fault unless I uncomment the line

for(i=0;i<=xcols;i++) x=0.0;
That is If I try to write to any of the dynamically allocated arrays
it gives me seg, fault.   The code is attached , any helpis deeply
appreciated.

Regards
Subir

---------------------------------------------------------------------------------------------------

#include <stdio.h>

#include <stdlib.h>
#include <math.h>

#include<mcheck.h>

int  main()

{
        mtrace();

        int trows=100;

        int xcols=90;
        int i,j,k;
        double dx,dt,temp,temp1;

        double **matu;

        matu=malloc(trows* sizeof(*matu));

        for(i=0; i<trows; i++)

        {
        matu=malloc(xcols * sizeof(*matu));

        if ( matu == NULL)

        {

        printf("No space for allocation of ith=%d row",i);

        exit(0);

        }
        }

        double *x=malloc(xcols * sizeof(x));
        double *t=malloc(trows * sizeof(t));

                for(i=0;i<=xcols;i++) x=0.0;
                //for(i=0;i<=trows;i++) t=0.0;

                //for(i=0;i<=xcols;i++)
                //for(j=0;j<=trows;j++)
                //matu[j]=0.0;

                /*dx=(1.0-0.0)/xcols;
                dt=(1.0-0.0)/trows;

                for(i=0;i<=xcols;i++)
                x=x[0]+dx*i;

                for(i=0;i<=trows;i++)
                t=t[0]+i*dt; */

for(i=0; i<trows; i++)

free(matu);

free(matu);
free(x);
free(t);
muntrace();

return 0;

}


Sorry made very basic mistakes..........ignore it
 
P

Phil Carmody

Hi,

I am trying to write a simple code in C using memory allocation. It
gives me segmentation fault. I have tried using mtrace. There are no
memory leaks and no segmentation fault unless I uncomment the line

for(i=0;i<=xcols;i++) x=0.0;


If x has length xcols, then its elements are x[0] .. x[xcols-1].
You're addressing x[xcols], and have just shot yourself in the foot.

Phil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top