Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Segfault - strcpy() copying into array (http://www.velocityreviews.com/forums/t741779-segfault-strcpy-copying-into-array.html)

arnuld 01-10-2011 06:14 AM

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

Owen Jacobson 01-10-2011 06:30 AM

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).


arnuld 01-10-2011 07:23 AM

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


Seebs 01-10-2011 08:52 AM

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.

arnuld 01-10-2011 10:06 AM

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

Ike Naar 01-10-2011 10:07 AM

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''.

jacob navia 01-10-2011 11:43 AM

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.


Francois Grieu 01-10-2011 02:37 PM

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

Francois Grieu 01-10-2011 04:44 PM

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

Keith Thompson 01-10-2011 07:13 PM

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.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57