Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Segfault - strcpy() copying into array

Reply
Thread Tools

Segfault - strcpy() copying into array

 
 
arnuld
Guest
Posts: n/a
 
      01-10-2011
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
 
Reply With Quote
 
 
 
 
Owen Jacobson
Guest
Posts: n/a
 
      01-10-2011
On Jan 9, 10:14*pm, arnuld <(E-Mail Removed)> 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).

 
Reply With Quote
 
 
 
 
arnuld
Guest
Posts: n/a
 
      01-10-2011
> On Jan 10, 11:30*am, Owen Jacobson <(E-Mail Removed)> wrote:

> This line declares a variable of type "pointer to struct myStruct" but
> does not initialize it.


Eh.. needed to malloc() . Thanks

 
Reply With Quote
 
Seebs
Guest
Posts: n/a
 
      01-10-2011
On 2011-01-10, arnuld <(E-Mail Removed)> 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 / http://www.velocityreviews.com/forums/(E-Mail Removed)
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.
 
Reply With Quote
 
arnuld
Guest
Posts: n/a
 
      01-10-2011
> On Mon, 10 Jan 2011 08:52:56 +0000, Seebs wrote:

>> On 2011-01-10, arnuld <(E-Mail Removed)> 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-4
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
 
Reply With Quote
 
Ike Naar
Guest
Posts: n/a
 
      01-10-2011
On 2011-01-10, arnuld <(E-Mail Removed)> 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''.
 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      01-10-2011
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.

 
Reply With Quote
 
Francois Grieu
Guest
Posts: n/a
 
      01-10-2011
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
 
Reply With Quote
 
Francois Grieu
Guest
Posts: n/a
 
      01-10-2011
On 10/01/2011 17:25, Richard wrote:
> Francois Grieu <(E-Mail Removed)> 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
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-10-2011
arnuld <(E-Mail Removed)> writes:
>> On Mon, 10 Jan 2011 08:52:56 +0000, Seebs wrote:

>
>>> On 2011-01-10, arnuld <(E-Mail Removed)> 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) (E-Mail Removed) <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"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
invoking a segfault within a segfault handler - is this undefinedbehavior? Andrey Vul C Programming 8 07-30-2010 02:14 PM
Benchmark segfault [Was: Array#inject to create a hash versus Hash[*array.collect{}.flatten] ] Michal Suchanek Ruby 6 06-13-2007 04:40 AM
Array#inject to create a hash versus Hash[*array.collect{}.flatten] -- Speed, segfault Anthony Martinez Ruby 4 06-11-2007 08:16 AM
Copying file lines into an array [ newbie ] inyc163 C Programming 2 05-25-2004 09:09 PM
Copying file lines into an array [ newbie ] jj C Programming 4 05-24-2004 05:19 PM



Advertisments