Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > help segmentation fault

Reply
Thread Tools

help segmentation fault

 
 
subirs
Guest
Posts: n/a
 
      06-13-2010
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[i]=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[i]=malloc(xcols * sizeof(*matu[i]));

if ( matu[i] == 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[i]=0.0;
//for(i=0;i<=trows;i++) t[i]=0.0;

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

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

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

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



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

free(matu[i]);

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

return 0;

}
 
Reply With Quote
 
 
 
 
Malcolm McLean
Guest
Posts: n/a
 
      06-13-2010
On Jun 13, 8:31*am, subirs <(E-Mail Removed)> wrote:
>
> for(i=0;i<=xcols;i++) x[i]=0.0;
>

Off by one. C arrays go from 0 to N-1, not 0 to N.
 
Reply With Quote
 
 
 
 
Ike Naar
Guest
Posts: n/a
 
      06-13-2010
In article <(E-Mail Removed)>,
subirs <(E-Mail Removed)> wrote:
>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[i]=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[i]=malloc(xcols * sizeof(*matu[i]));
>
> if ( matu[i] == 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[i]=0.0;
> //for(i=0;i<=trows;i++) t[i]=0.0;
>
> //for(i=0;i<=xcols;i++)
> //for(j=0;j<=trows;j++)
> //matu[i][j]=0.0;
>
> /*dx=(1.0-0.0)/xcols;
> dt=(1.0-0.0)/trows;
>
> for(i=0;i<=xcols;i++)
> x[i]=x[0]+dx*i;
>
> for(i=0;i<=trows;i++)
> t[i]=t[0]+i*dt; */
>
>
>
>for(i=0; i<trows; i++)
>
>free(matu[i]);
>
>free(matu);
>free(x);
>free(t);
>muntrace();
>
>return 0;
>
>}


 
Reply With Quote
 
Ike Naar
Guest
Posts: n/a
 
      06-13-2010
In article <hv2202$igh$(E-Mail Removed)-september.org>,
Ike Naar <(E-Mail Removed)> wrote:
[...]

Sorry, made a mistake while creating the previous post.
Here's new attempt, hope this one works out better.

>In article <(E-Mail Removed)>,
>subirs <(E-Mail Removed)> wrote:


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


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

>> [...]
>>
>>int main()


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.
 
Reply With Quote
 
subirs
Guest
Posts: n/a
 
      06-13-2010
On Jun 13, 10:31*am, subirs <(E-Mail Removed)> wrote:
> 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[i]=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[i]=malloc(xcols * sizeof(*matu[i]));
>
> * * * * if ( matu[i] == 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[i]=0.0;
> * * * * * * * * //for(i=0;i<=trows;i++) t[i]=0.0;
>
> * * * * * * * * //for(i=0;i<=xcols;i++)
> * * * * * * * * //for(j=0;j<=trows;j++)
> * * * * * * * * //matu[i][j]=0.0;
>
> * * * * * * * * /*dx=(1.0-0.0)/xcols;
> * * * * * * * * dt=(1.0-0.0)/trows;
>
> * * * * * * * * for(i=0;i<=xcols;i++)
> * * * * * * * * x[i]=x[0]+dx*i;
>
> * * * * * * * * for(i=0;i<=trows;i++)
> * * * * * * * * t[i]=t[0]+i*dt; */
>
> for(i=0; i<trows; i++)
>
> free(matu[i]);
>
> free(matu);
> free(x);
> free(t);
> muntrace();
>
> return 0;
>
> }
>
>


Sorry made very basic mistakes..........ignore it
 
Reply With Quote
 
Phil Carmody
Guest
Posts: n/a
 
      06-13-2010
http://www.velocityreviews.com/forums/(E-Mail Removed) (Ike Naar) writes:
> In article <(E-Mail Removed)>,
> subirs <(E-Mail Removed)> wrote:
> >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[i]=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
--
I find the easiest thing to do is to k/f myself and just troll away
-- David Melville on r.a.s.f1
 
Reply With Quote
 
Seebs
Guest
Posts: n/a
 
      06-13-2010
On 2010-06-13, subirs <(E-Mail Removed)> wrote:
> for(i=0;i<=xcols;i++) x[i]=0.0;


Imagine that xcols were 3.

You have three objects: 0, 1, 2, and 3. Or do you?

Think about it...

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / (E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with array/pointer segmentation fault needed Ben C Programming 8 06-13-2006 07:10 AM
Beginner requesting help - Segmentation fault Fra-it C Programming 13 11-06-2005 11:46 PM
Segmentation fault need help Marcia Hon C Programming 4 02-16-2004 08:13 PM
Re: string calloc segmentation fault -- help! Kevin Goodsell C++ 0 09-02-2003 09:44 PM
Re: string calloc segmentation fault -- help! Andrey Tarasevich C++ 0 09-02-2003 07:03 PM



Advertisments