![]() |
Segfault - strcpy() copying into array
I don't get it why I am getting Segmentation Fault here in strcpy():
#include <stdio.h> #include <string.h> struct myStruct { char title[10]; }; void checkArgument(struct myStruct *p); int main(void) { struct myStruct *st; strcpy(st->title, "clc"); checkArgument(st); return 0; } void checkArgument(struct myStruct *p) { if(NULL == p) { printf("*ERROR* - Invalid Args\n"); } else if('\0' == p->title[0]) { printf("Empty member\n"); } else { printf("Title = %s\n", p->title); } } -- http://www.lispmachine.wordpress.com |
Re: Segfault - strcpy() copying into array
On Jan 9, 10:14*pm, arnuld <sunr...@invalid.address> wrote:
> I don't get it why I am getting Segmentation Fault here in strcpy(): > > #include <stdio.h> > #include <string.h> > > struct myStruct > { > * char title[10]; > > }; > > void checkArgument(struct myStruct *p); > > int main(void) > { > * struct myStruct *st; This line declares a variable of type "pointer to struct myStruct" but does not initialize it. > * strcpy(st->title, "clc"); This line dereferences the uninitialized variable. Boom, segfault (or any other possible result). |
Re: Segfault - strcpy() copying into array
> On Jan 10, 11:30*am, Owen Jacobson <angrybald...@gmail.com> wrote:
> This line declares a variable of type "pointer to struct myStruct" but > does not initialize it. Eh.. needed to malloc() . Thanks |
Re: Segfault - strcpy() copying into array
On 2011-01-10, arnuld <sunrise@invalid.address> wrote:
> I don't get it why I am getting Segmentation Fault here in strcpy(): I am starting to wonder whether you should just give up on C. > struct myStruct > { > char title[10]; > }; > int main(void) > { > struct myStruct *st; > strcpy(st->title, "clc"); What does st point to? If this two-line bug doesn't leap out at you, after the number of times you've gone through this, maybe this is not the right language for you. It's one thing not to understand this stuff when you've never seen it before, but you've had MANY questions which involved uninitialized pointers, and any compiler I've used in the last decade or two would have warned you about this if you had it configured sanely. -s -- Copyright 2010, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated! I am not speaking for my employer, although they do rent some of my opinions. |
Re: Segfault - strcpy() copying into array
> On Mon, 10 Jan 2011 08:52:56 +0000, Seebs wrote:
>> On 2011-01-10, arnuld <sunrise@invalid.address> wrote: > I am starting to wonder whether you should just give up on C. I won't :) >> struct myStruct >> { >> char title[10]; >> }; > >> int main(void) >> { >> struct myStruct *st; > >> strcpy(st->title, "clc"); > What does st point to? > > If this two-line bug doesn't leap out at you, after the number of times > you've gone through this, maybe this is not the right language for you. > > It's one thing not to understand this stuff when you've never seen it > before, but you've had MANY questions which involved uninitialized > pointers, and any compiler I've used in the last decade or two would > have warned you about this if you had it configured sanely. gcc -ansi -pedantic -Wall -Wextra does not give any warning: [arnuld@dune downloads]$ gcc --version gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-48) Copyright (C) 2006 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. I think I missed out the bug probably because my brain was engaged somewhere else, I am trying to solve an issue of Segmentation Fault in some code (which I can't post as its proprietary). There is a linked list of such kind of struct pointers and a for loop is going on it. NULL != st- >title passes through but '\0' == st->title[0] gives a Segfault in some very rare situations. Don't know why its happening but unlike this 2 line example that code does have a malloc() with a null check. My brain was totally into that when I wrote this example. -- http://www.lispmachine.wordpress.com |
Re: Segfault - strcpy() copying into array
On 2011-01-10, arnuld <sunrise@invalid.address> wrote:
> I don't get it why I am getting Segmentation Fault here in strcpy(): > [...] > int main(void) > { > struct myStruct *st; st has type ``pointer to struct myStruct''. st is an uninitialized pointer; it does not point to a struct myStruct object. > strcpy(st->title, "clc"); Here you try to dereference an indeterminate pointer ``st''. |
Re: Segfault - strcpy() copying into array
Le 10/01/11 12:37, Richard a écrit :
> > > Simple answer : step through with a DEBUGGER. > > The line will leap out at you. > > Sheesh. DEBUGGER? Please do not use 8 letter words here in comp.lang.c! They are twice as nasty as 4 letter words. |
Re: Segfault - strcpy() copying into array
On 10/01/2011 11:06, arnuld wrote
> I am trying to solve an issue of Segmentation Fault in > some code (which I can't post as its proprietary). > There is a linked list of such kind of struct pointers > and a "for" loop is going on it. > NULL != st->title passes through but > '\0' == st->title[0] gives a Segfault in some > very rare situations. Don't know why its happening but > that code does have a malloc() with a null check. Perhaps st->title was never set to a valid non-NULL pointer, or was set to the result of malloc() for zero length. Francois Grieu |
Re: Segfault - strcpy() copying into array
On 10/01/2011 17:25, Richard wrote:
> Francois Grieu <fgrieu@gmail.com> writes: > >> On 10/01/2011 11:06, arnuld wrote >>> I am trying to solve an issue of Segmentation Fault in >>> some code (which I can't post as its proprietary). >>> There is a linked list of such kind of struct pointers >>> and a "for" loop is going on it. >>> NULL != st->title passes through but >>> '\0' == st->title[0] gives a Segfault in some >>> very rare situations. Don't know why its happening but >>> that code does have a malloc() with a null check. >> >> Perhaps st->title was never set to a valid non-NULL pointer, >> or was set to the result of malloc() for zero length. >> >> Francois Grieu > > Did you not see the other correct answers? Yes I did, but I'm trying to solve the REAL problem of the Original Poster as summarized in the text I quoted, NOT what he stated in his irrelevant original post. Francois Grieu |
Re: Segfault - strcpy() copying into array
arnuld <sunrise@invalid.address> writes:
>> On Mon, 10 Jan 2011 08:52:56 +0000, Seebs wrote: > >>> On 2011-01-10, arnuld <sunrise@invalid.address> wrote: > >> I am starting to wonder whether you should just give up on C. > > I won't :) > > >>> struct myStruct >>> { >>> char title[10]; >>> }; >> >>> int main(void) >>> { >>> struct myStruct *st; >> >>> strcpy(st->title, "clc"); > > >> What does st point to? >> >> If this two-line bug doesn't leap out at you, after the number of times >> you've gone through this, maybe this is not the right language for you. >> >> It's one thing not to understand this stuff when you've never seen it >> before, but you've had MANY questions which involved uninitialized >> pointers, and any compiler I've used in the last decade or two would >> have warned you about this if you had it configured sanely. > > gcc -ansi -pedantic -Wall -Wextra does not give any warning: [...] gcc typically doesn't recognize uninitialized variables unless it's invoked with optimization. The kind of analysis needed to detect the error is also necessary to perform many optimizations. Adding "-O1" to the above produces a warning. (Other compilers are likelyi to behave similarly.) -- Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst> Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" |
| All times are GMT. The time now is 11:46 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.