Thread: why is this happening?!

  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    why is this happening?!

    This leads to a segfault when we try to read line back in main (eg. "in strlen...") but I cannot see anything wrong:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <stdlib.h>
    #include <errno.h>
    
    int fdreadin (char *source, char *buffer, char end, int max) {
    	char *errmsg;
    	int FDIN=open(source,O_RDONLY);
    	if (FDIN<3) {
    		errmsg=strerror(errno);
    		buffer=malloc(strlen(errmsg+1));
    		strcpy(buffer,errmsg);
    		puts(buffer);   // check buffer
    		return 0;
    	}
    	close(FDIN);
    }
    
    int main (int argc, char *argv[]) {
    	char *line;
    	fdreadin("asdfff", line, 0, 100);
    	puts("fault here");    //comes out
    	printf("%s\n",line);         // doesn't
    }
    edit: Okay, I understand now -- I thought I did this "all the time" but evidently not quite. Why oh why can't I and at least there should be someone I can sue...

    Is there a justification for this madness?
    Last edited by MK27; 01-13-2009 at 07:00 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The problem is that this line merely changes the local pointer in the function:
    Code:
    buffer=malloc(strlen(errmsg+1));
    You need a pointer to a pointer so that the change can be reflected in the caller. Oh, and remember to free().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You mean, it's not working because when you change line in fdreadin(), line isn't changed in main()?

    Well, it would work if you used a variable of type char** pointer-to-pointer for fdreadin()'s buffer parameter.

    Or, since you're not really using the return value of fdreadin(), you could return the char* malloc()'d buffer.

    Or you could use references if this were C++ and not C.

    [edit] Also:
    Code:
    buffer=malloc(strlen(errmsg+1));
    I'm thinking you meant
    Code:
    buffer=malloc(strlen(errmsg) + 1);
    [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is happening here PLZ help me
    By jayee_spicyguy in forum C Programming
    Replies: 1
    Last Post: 11-15-2008, 06:47 AM
  2. Explain me what is happening
    By capvirgo in forum C Programming
    Replies: 2
    Last Post: 02-18-2008, 08:26 AM
  3. Weird things happening!!!!!!
    By korbitz in forum Windows Programming
    Replies: 4
    Last Post: 03-22-2004, 06:31 AM
  4. Funny things happening to values outside of function
    By drb2k2 in forum Windows Programming
    Replies: 2
    Last Post: 04-10-2003, 02:39 PM
  5. What happening with my Signiture Text?
    By Davros in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-23-2002, 07:33 PM