fgets problems


 
Thread Tools Search this Thread
Top Forums Programming fgets problems
# 22  
Old 02-21-2010
Quote:
Originally Posted by achenle
Let me get this straight: you have a custom memory management library, and your code using that library crashes?
I don't know what you mean by "custom", but yes I have a library and yes it forces its users to manage memory in a particular way. But memory management is not the purpose of the library: it's for computational and algebraic number theory.

The code did crash when I posted this thread. I since rewrote large portions of the code and it works now.

Quote:
Originally Posted by achenle
You need all the help you can get. But there's not much out there.
Probably. Smilie

Quote:
Originally Posted by achenle
There's a reason why nobody writes their own memory management libraries. First off, the multiple libraries available for free on your OS of choice are almost certainly fast enough. If they're not, you're most likely doing something wrong, like an over-reliance on malloc()/free() and/or new/delete. And if the OS libaries truly aren't fast enough (and if you're not multithreaded on a massively parallel application, they ARE fast enough!), there are third-party memory management libraries available.
I don't really have a choice: if I want to use the Pari library, I have to work with its memory management.

But I don't really think it's that bad. Here are some selections from the user manual on Pari:
• First, PARI routines do their own garbage collecting, which means that whenever a documented function from the library returns, only its result(s) have been added to the stack, possibly up to a very small overhead (non-documented ones may not do this). In particular, a PARI function that does not return a GEN does not clutter the stack. Thus, if your computation is small enough (e.g. you call few PARI routines, or most of them return long integers), then you do not need to do any garbage collecting. This is probably the case in many of your subroutines. Of course the objects that were on the stack before the function call are left alone. Except for the ones listed below, PARI functions only collect their own garbage.
• It may happen that all objects that were created after a certain point can be deleted - for instance, if the final result you need is not a GEN, or if some search proved futile. Then, it is enough to record the value of avma just before the first garbage is created, and restore it upon exit:
Code:
     pari_sp av = avma; /* record initial avma */
     garbage ...
     avma = av; /* restore it */

All objects created in the garbage zone will eventually be overwritten: they should no longer be accessed avma has been restored.
[...]
First, gerepile has turned out to be a flexible and fast garbage collector for number-theoretic computations, which compares favorably with more sophisticated methods used in other systems. Our benchmarks indicate that the price paid for using gerepile and gerepile-related copies, when properly used, is usually less than 1% of the total running time, which is quite acceptable!
Second, it is of course harder on the programmer, and quite error-prone if you do not stick to a consistent PARI programming style. If all seems lost, just use gerepilecopy (or gerepileall) to fix up the stack for you. You can always optimize later when you have sorted out exactly which routines are crucial and what objects need to be preserved and their usual sizes.


Quote:
Originally Posted by achenle
Go price something like Smartheap. Then calculate all the hours spent on your custom memory library and how much those hours cost. Want to bet Smartheap is cheaper? If you even need it in the first place.
It's not an option, because I need to use Pari.

---------- Post updated at 02:23 PM ---------- Previous update was at 02:17 PM ----------

Quote:
Originally Posted by Corona688
So you have no idea why code using your own personally-developed stack-modifier was crashing or how, or why it stopped, but are 100% certain it wasn't a segfault despite it having all the hallmarks of one because that would be inconvenient. Faith-based software-maintenance, I tell you. Smilie
I saw what the error was at the time, but that was several days ago. I don't recall now. I can recognize a segfault when I see one. Smilie

Important clarification here: I certainly did not develop the library! It's taken two Ph.D projects and dozens of contributors almost two decades to create.

Quote:
Originally Posted by Corona688
Oh well, I'm done arguing until you have new data or questions. You know our positions already. Like achenle says, good luck, you're going to need it.
I very much appreciate your time and opinions.
# 23  
Old 02-22-2010
This is something of a "Corona688-told-me-so" post.

I found the error, and it was a SEGFAULT-causing error. But it wasn't anything to do with Pari's novel memory management; just a simple mistake anyone could have made (and I was apparently boneheaded enough to make).

Code:
char* kept = getBValue(line);
GEN value = strtoi(kept);
if (kept == NULL)
	continue;

My function getBValue, if it cannot find a value in the line it is passed, returns NULL. Rather than testing its return value (kept) for NULL, I pass it to anther function... and only then test for NULL.

Smilie

How did I miss that?
# 24  
Old 02-22-2010
Glad you found the problem! No hard feelings.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

while and do problems

Any ideas how to clear this error as it seems I dont understand if,do,while and els commands #!/bin/ksh set -x print "This script creates test messages" print "Please enter test case name" read testcasename echo $testcasename skipfield=Y while print "Do you want to skip this field... (4 Replies)
Discussion started by: andrew.p.mcderm
4 Replies

2. Programming

fgets read file line with "\n" inside

Hi, I have a string like this, char str ="This, a sample string.\\nThis is the second line, \\n \\n, we will have one blank line"; if I want to use strtok() to seperate the string, which token should I use? I tried "\n", "\\n", either not working. peter (1 Reply)
Discussion started by: laopi
1 Replies

3. Programming

fgets problems newline

hello, i'm trying to write a C-program that reads a file line by line. (and searches each line for a given string) This file is an special ASCII-database-file, with a lot of entries. I checked the line with most length, and it was about 4000 characters. With google i found several... (4 Replies)
Discussion started by: p1cm1n
4 Replies

4. UNIX for Dummies Questions & Answers

Problems with using less

Hello, I am having problems with using less on Linux version 2.6.18-92.1.17.el5 (brewbuilder@hs20-bc1-7.build.redhat.com) (gcc version 4.1.2 20071124 (Red Hat 4.1.2-42)). I am using csh but have the same problems on bash. If I pipe something to less it works perfectly i.e. cat file | less... (9 Replies)
Discussion started by: z1dane
9 Replies

5. Programming

[C] fgets problem with SIGINT singlal!!!

Hi all, I have this method to read a string from a STDIN: void readLine(char* inputBuffer){ fgets (inputBuffer, MAX_LINE, stdin); fflush(stdin); /* remove '\n' char from string */ if(strlen(inputBuffer) != 0) inputBuffer = '\0'; } All work fine but if i... (1 Reply)
Discussion started by: hurricane86
1 Replies

6. Programming

Question about NULL Character & fgets()

Assume client send the message " Hello ", i get output such as Sent mesg: hello Bytes Sent to Client: 6 bytes_received = recv(clientSockD, data, MAX_DATA, 0); if(bytes_received) { send(clientSockD, data, bytes_received, 0); data = '\0';... (2 Replies)
Discussion started by: f.ben.isaac
2 Replies

7. Programming

Problem with fgets and rewind function ..

Hello Friends, I got stuck with fgets () & rewind() function .. Please need help.. Actually I am doing a like, The function should read lines from a txt file until the function is called.. If the data from the txt file ends then it goes to the top and then again when the function is called... (1 Reply)
Discussion started by: user_prady
1 Replies

8. Programming

fgets()

does anyone knows how to accept a command from a user.. i was wondering to use fgets(), but got no idea how to start it... (4 Replies)
Discussion started by: skanky
4 Replies

9. UNIX for Dummies Questions & Answers

Problems with ld.so.1

I renamed ld.so.1 on a Sun machine running Solaris 2.6. Now I cannot boot the system and I can use only very few commands in Maintenance Mode. Can someone help me? (3 Replies)
Discussion started by: ciccio
3 Replies

10. UNIX for Advanced & Expert Users

'make' problems (compliation problems?)

I'm trying to compile and install both most recent version of 'make' and the most recent version of 'openssh' on my Sparc20. I've run into the following problems... and I don't know what they mean. Can someone please help me resolve these issues? I'm using the 'make' version that was... (5 Replies)
Discussion started by: xyyz
5 Replies
Login or Register to Ask a Question