Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: My C program does not compile or gives segmentation fault Date: Thu, 23 Mar 2017 13:24:25 -0700 Organization: None to speak of Lines: 64 Message-ID: References: <89549322-236e-4a59-9bb5-dc881593a846@googlegroups.com> <7e5f00ae-7843-42e8-9335-c46e1959244c@googlegroups.com> <1b60izc23w.fsf@pfeifferfamily.net> <08f2d23e-4308-4676-8d1b-52f93f2fd6df@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="1b33b319c9f9fcfff45ec1f5b5f6ee2d"; logging-data="28253"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19bwNm6VvPiQxVtRGW7JtfJ" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:yIMN3HG5hYEVw9RYKG5OscEm8Pc= sha1:pxQ9nVHZggsoZx6CuCP+Mz0CyTE= Xref: csiph.com comp.lang.c:106849 acidlategamer@gmail.com writes: [...] > I just came to senses. > The malloc I did in create_queue() is a local memory allocation. q > will be again uninitialized after the function returns, hence I was > getting segmentation faults. I did correct way. Almost. The memory allocated by malloc() isn't local, it's on the "heap", and its lifetime continues until it's deallocated by free() or equivalent, or until the program completes. What's local is your only pointer to it. When that pointer object vanishes, you've lost your only reference to the allocated memory. It's a memory leak. And q isn't "again uninitialized". You actually have two distinct objects named "q". One is a global (file-scope) variable. The other is the parameter to the create_queue() function, which is initialized with a *copy* of the value of the argument, the global "q". (Parameters are like local variables, except that they're initialized to the value of the corresponding argument.) > http://codepad.org/sqUT3bx4 Here's your updated create_queue() function: struct queue *create_queue(struct queue *q) { q = malloc(sizeof(struct queue)); q -> rear = NULL;//how this happened without any error?? q -> front = NULL; return q; } It takes an argument of type "struct queue*", but it doesn't do anything with that value; it immediately clobbers it with the value returned by malloc(). This isn't exactly wrong, but it's useless. You can drop the parameter and change it to a local variable: struct queue *create_queue(void) { struct queue q = malloc(sizeof(struct queue)); q -> rear = NULL;//how this happened without any error?? q -> front = NULL; return q; } Then the call in your main function becomes: q = create_queue(); Your insert() function always returns the value of its first parameter, which it never changes. It might as well be a void function, not returning a value. (Unless it *should* modify the value of q; I haven't studied the logic closely enough to be sure.) Consistent indentation would make your code much easier to read. -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"