s-news
[Top] [All Lists]

Re: Memory faults, tracking

To: immhan@student.dtu.dk
Subject: Re: Memory faults, tracking
From: Terry Therneau <therneau@mayo.edu>
Date: Thu, 20 Sep 2001 08:53:25 -0500 (CDT)
Cc: s-news@wubios.wustl.edu
Reply-to: Terry Therneau <therneau@mayo.edu>
 Below is my most used routine for debugging C code with the kind of error
you report.  In your S function, replace
        .C("myroutine", x= ... 
with
        CC("myroutine", x= ...
and run the program.  The "CC" function will create a short mainline program
Dmyroutine.c which will call myroutine with exactly the same arguments that
Splus would have called it with.  You can then compile Dmyroutine.c, link it
with myroutine.o, and use all of your favorite debugging tools outside of S.

  Note, your S program will likely fail a few lines below the CC call, at the
first point that you try to use the results of the .C call, since
CC doesn't return anything.  This doesn't matter, because at that point the
Dmyroutine.c file has already been written.  Once you have figured out the
bug replace CC with .C again.

        Terry Therneau
        
------------------------------------------------
"CC"<-
function(subroutine, ..., NAOK = F)
{
        assign("file", paste("D", subroutine, ".c", sep = ""), frame = 1)
        assign("out", function(...)
            cat(..., file = file, append = T), frame = 1)
        put <- function(type, name, data)
        {
            out(paste(type, " ", name, "[]", sep = ""), "= {")
            out(data, sep = ",", fill = 60, labels = "\t")
            out("};\n\n")
        }
        unix(paste("rm -f", file))
        args <- list(...)
        index <- seq(along = args)
        n <- names(args)
        if(length(n) == 0)
            n <- rep("", length(args))
        if(length(n) > 0)
            n <- ifelse(n == "", paste("arg", index, sep = ""), n)
        for(i in index)
            switch(storage.mode(args[[i]]),
                character = put("static char *", n[i], paste("\"", args[[i]],   
 
                    "\"", sep = "")),
                integer = put("static long", n[i], args[[i]]),
                single = put("static float", n[i], args[[i]]),
                double = put("static double", n[i], args[[i]]),
                logical = put("static long", n[i], args[[i]]),
                stop(paste("Unknown mode", m[i], "for argument", i)))
        out("main() {\n", subroutine, "(", paste(n, collapse = ", "), ");\n}\n",
            sep = "")
}


<Prev in Thread] Current Thread [Next in Thread>