![]() |
|
#1
|
|||
|
|||
problem reading to a dynamic arrayHi!
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:
Last edited by dsmith : 02-Jan-2005 at 09:01.
Reason: Please use [c] & [/c] for syntax highlighting
|
|
#2
|
||||
|
||||
|
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. __________________
The best damn Sports Blog period. |
|
#3
|
|||
|
|||
|
Quote:
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:
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
|
||||
|
||||
|
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
|
|||
|
|||
|
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:
|
|
#6
|
|||
|
|||
**Correction**Sorry..here is the correct and full codeHi..
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:
Quote:
Last edited by dsmith : 02-Jan-2005 at 13:44.
Reason: Please use [c] & [/c] for syntax highlighting
|
|
#7
|
||||
|
||||
|
Quote:
Quote:
Quote:
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...__________________
Age is unimportant -- except in cheese |
|
#8
|
|||
|
|||
Function gives seg. faultThe 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:
|
|
#9
|
||||
|
||||
|
As has been suggested:
Quote:
And pointed out: Quote:
I assume you haven't done this because Quote:
. __________________
Age is unimportant -- except in cheese |
|
#10
|
|||
|
|||
|
Quote:
Look at this: CPP / C++ / C Code:
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:
If you run this, you can abort the continuous printout with Ctrl-C. Regards, Dave |
Recent GIDBlog
Halfway done! by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to search an array problem | jenmaz | C Programming Language | 4 | 17-Nov-2004 01:58 |
| Problem reading structs in a file | The_Kingpin | C Programming Language | 16 | 26-Oct-2004 23:07 |
| Problem in array | Kay Chan | C Programming Language | 2 | 05-Oct-2004 21:16 |
| 3D array dynamic memory allocation | cjwatchdog | C Programming Language | 3 | 20-Feb-2004 15:27 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The