GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Jan-2005, 09:46
noamfrie noamfrie is offline
New Member
 
Join Date: Dec 2004
Posts: 4
noamfrie is on a distinguished road
Exclamation

problem reading to a dynamic array


Hi!
I've allocated a dynamic array of "double" variables using malloc function.
The problem is when I try to input numbers ("double" type) into this dynamic array, I always get a "segmentation fault (core dumped)" error.
I would appriciate your help. It is preety urgent for me....
Here is the code:

CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int n,k;
double *p;
scanf ("%d",&n);
p=(double *)malloc(n*sizeof(double));
if (p==NULL)
printf ("Error: Not enough memory.\n");
for (k=0;k<n;k++)
scanf ("%lf",p+k);
free (p);
return 0;
}
Last edited by dsmith : 02-Jan-2005 at 10:01. Reason: Please use [c] & [/c] for syntax highlighting
  #2  
Old 02-Jan-2005, 10:05
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi noamfrie.

What compiler are you using? I downloaded your code and without any changes it seemed to work fine for me.

I am using gcc under Linux.
  #3  
Old 02-Jan-2005, 10:20
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by noamfrie
Hi!
I've allocated a dynamic array of "double" variables using malloc function.
The problem is when I try to input numbers ("double" type) into this dynamic array, I always get a "segmentation fault (core dumped)" error.
I would appriciate your help. It is preety urgent for me....
Here is the code:

CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int n,k;
double *p;
scanf ("%d",&n);
p=(double *)malloc(n*sizeof(double));
if (p==NULL)
printf ("Error: Not enough memory.\n");
for (k=0;k<n;k++)
scanf ("%lf",p+k);
free (p);
return 0;
}


Since there is no way for you (or me) to tell what your code is doing, I am going to suggest that you put some printf() statements in strategic places.

For example:

CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>

int main ()
{
  int n, k;
  double *p;

  printf("Enter the number of data items: ");
  scanf ("%d",&n);
  printf("You entered %d\n", n);

  p = malloc(n * sizeof(double));
  if (p == NULL) {
    printf ("Error: Not enough memory.\n");
  }
  else {
    for (k = 0; k < n; k++) {
      printf("%d: ", k);
      scanf ("%lf",p+k);
      printf("You entered %f\n", p[k]);
    }

    for (k = 0; k < n; k++) {
      printf("%d: %f:\n", k, p[k]);
    }

    free (p);
  }
  return 0;
}


Then:

1. Maybe you can see how far it gets before it bombs out.
2. Maybe you can see if the program is reading the user input numbers that you think it should see.

Regards,

Dave
  #4  
Old 02-Jan-2005, 12:51
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Is n a good value coming into the allocation? The code looks fine to me, other than the fact that you are using scanf(). Try using getchar() coupled with the atoi() function if you think the input might be getting goofed up. The code *should* be working, though.

Now that I know what a segfault is, I can tell you that you are accessing memory that doesn't belong to you, and this is usually caused by going out of the bounds of your array.

My theory: Since your memory is coming from the free store, any time you go outside the array, you're trying to access memory that's in the free store. Of course, that memory doesn't belong to you, since you haven't allocated it with malloc(), so you get a segfault.
__________________
-Aaron
  #5  
Old 02-Jan-2005, 13:27
Dr. Evil Dr. Evil is offline
Member
 
Join Date: Oct 2004
Location: Netherlands
Posts: 120
Dr. Evil will become famous soon enough
I had that problem when trying to allocate memory for an int array. Try using the function LocalAlloc(), for which you should include <windows.h>. It would look something like this, although I don't know if it works for doubles.

CPP / C++ / C Code:
#include <windows.h>

int main()
{
 int *arr;
 
 arr = LocalAlloc(LPTR, sizeof(int)*/*whatever*/);
 if(arr != NULL)
 {
  //do whatever
  LocalFree(arr);
 }
 return 0;
}
  #6  
Old 02-Jan-2005, 14:39
noamfrie noamfrie is offline
New Member
 
Join Date: Dec 2004
Posts: 4
noamfrie is on a distinguished road

**Correction**Sorry..here is the correct and full code


Hi..
Sorry but the error happens in the following program, not in the partial code I submitted earlier.
This program gets n number of "double" values, puts them in a dynamic array and prints the minimum and maximum value in the array.
Also, I can't use libary functions in "minmax" function, only in "main".
Thanks in advance...

Noam.

Here is the full code:
CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>
void minmax (double *a,int n,double *min,double *max);
int main ()
{
int n,k;
double *p,*min,*max,x=10000,y=1;
min=&x;
max=&y;
scanf ("%d",&n);
p=(double *)malloc(n*sizeof(double));
if (p==NULL)
printf ("Error: Not enough memory.\n");
for (k=0;k<n;k++)
scanf ("%lf",p+k);
minmax (p,n,min,max);
printf ("Double_min=%f\nDouble_max=%f",*min,*max);
free (p);
return 0;
}
void minmax (double *p,int n,double *min,double *max)
{
for (p;p<p+n;p++)
{
if (*p<=*min)
*min=*p;
if (*p>=*max)
*max=*p;
}
}
Quote:
Originally Posted by noamfrie
Hi!
I've allocated a dynamic array of "double" variables using malloc function.
The problem is when I try to input numbers ("double" type) into this dynamic array, I always get a "segmentation fault (core dumped)" error.
I would appriciate your help. It is preety urgent for me....
Here is the code:

CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int n,k;
double *p;
scanf ("%d",&n);
p=(double *)malloc(n*sizeof(double));
if (p==NULL)
printf ("Error: Not enough memory.\n");
for (k=0;k<n;k++)
scanf ("%lf",p+k);
free (p);
return 0;
}
Last edited by dsmith : 02-Jan-2005 at 14:44. Reason: Please use [c] & [/c] for syntax highlighting
  #7  
Old 02-Jan-2005, 15:17
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by noamfrie
Hi..
Sorry but the error happens in the following program, not in the partial code I submitted earlier.
Always test your programs before posting. We can't fix what ain't broke.

Quote:
Originally Posted by noamfrie
This program gets n number of "double" values, puts them in a dynamic array and prints the minimum and maximum value in the array.
Also, I can't use libary functions in "minmax" function, only in "main".
Why not? Did your teacher say you can't? Did the compiler complain?

Quote:
Originally Posted by noamfrie
Thanks in advance...

Noam.

Here is the full code:
CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>
void minmax (double *a,int n,double *min,double *max);
int main ()
{
int n,k;
double *p,*min,*max,x=10000,y=1;
min=&x;
max=&y;
scanf ("%d",&n);
p=(double *)malloc(n*sizeof(double));
if (p==NULL)
printf ("Error: Not enough memory.\n");
for (k=0;k<n;k++)
scanf ("%lf",p+k);
minmax (p,n,min,max);
printf ("Double_min=%f\nDouble_max=%f",*min,*max);
free (p);
return 0;
}
void minmax (double *p,int n,double *min,double *max)
{
for (p;p<p+n;p++)
{
if (*p<=*min)
*min=*p;
if (*p>=*max)
*max=*p;
}
}

You did not mention what happened when you did what Dave suggested. Well?

I suspect minmax() is your culprit. See Dave's post.

You need to learn to indent your code so it's readable, as well as read the sticky at the top of the forum. The Preview button will help you help us help you Hey I like that! I'll have to use it again...
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #8  
Old 02-Jan-2005, 15:54
noamfrie noamfrie is offline
New Member
 
Join Date: Dec 2004
Posts: 4
noamfrie is on a distinguished road

Function gives seg. fault


The teacher said I can't use libary functions.
And you're right. The "minmax" function is the problem.
Any idea why it gives segmentation fault?

Quote:
Originally Posted by WaltP
Always test your programs before posting. We can't fix what ain't broke.


Why not? Did your teacher say you can't? Did the compiler complain?




You did not mention what happened when you did what Dave suggested. Well?

I suspect minmax() is your culprit. See Dave's post.


You need to learn to indent your code so it's readable, as well as read the sticky at the top of the forum. The Preview button will help you help us help you Hey I like that! I'll have to use it again...
  #9  
Old 02-Jan-2005, 18:32
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
As has been suggested:
Quote:
Originally Posted by davekw7x
Since there is no way for you (or me) to tell what your code is doing, I am going to suggest that you put some printf() statements in strategic places.
. . .

Then:

1. Maybe you can see how far it gets before it bombs out.
2. Maybe you can see if the program is reading the user input numbers that you think it should see.

Regards,

Dave

And pointed out:
Quote:
Originally Posted by WaltP
You did not mention what happened when you did what Dave suggested. Well?

I assume you haven't done this because
Quote:
Originally Posted by noamfrie
The teacher said I can't use libary functions.
This does not mean you can't debug your code with them. You just have to remove them before turning in your assignment
.
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #10  
Old 02-Jan-2005, 19:35
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by noamfrie
Hi..
Sorry but the error happens in the following program, not in the partial code I submitted earlier.
This program gets n number of "double" values, puts them in a dynamic array and prints the minimum and maximum value in the array.
Also, I can't use libary functions in "minmax" function, only in "main".
Thanks in advance...

Noam.


Look at this:
CPP / C++ / C Code:
for (p;p<p+n;p++)

The first p does nothing (but it doesn't hurt anything either).

The second p < p+n says to continue looping as long as p is less than p+n.

The third p++ increments p each time.

After incrementing, the test p < p + n always fails, so there is an infinite loop. Why does it fail? Because it is seeing if (the new value of p) is less than (the new value of p) + n. And it is

You can print out values of pointers with printf (although they don't usually tell you this at first).

CPP / C++ / C Code:
  for (; p<p+n; p++) {
    printf("pointer = %p\n", p);
    if (*p<=*min) {
      *min=*p;
    }
    if (*p>=*max) {
      *max=*p;
    }
  }

If you run this, you can abort the continuous printout with Ctrl-C.

Regards,

Dave
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB 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
How to search an array problem jenmaz C Programming Language 4 17-Nov-2004 02:58
Problem reading structs in a file The_Kingpin C Programming Language 16 27-Oct-2004 00:07
Problem in array Kay Chan C Programming Language 2 05-Oct-2004 22:16
3D array dynamic memory allocation cjwatchdog C Programming Language 3 20-Feb-2004 16:27

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:03.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.