Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   strcat() and segmentation faults. But malloc() already done. (http://www.velocityreviews.com/forums/t440609-strcat-and-segmentation-faults-but-malloc-already-done.html)

Stanley S 12-20-2005 05:22 PM

strcat() and segmentation faults. But malloc() already done.
 
Hi,

I'm puzzled. Why does the following cause a seg fault? Notwithstanding
that I've already malloc() a certain space for "Hello".

I do understand that using a fixed length array will work very well.
But I wish to find out how can this be achieve using pointers.

Thank you.

Stan

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{

char *Hello = NULL;
char *World = NULL;
Hello = malloc(100);
Hello = "hello";
World = "world";
strcat(Hello, World);
printf("%s\n",Hello);

return 0;
}


Artie Gold 12-20-2005 05:40 PM

Re: strcat() and segmentation faults. But malloc() already done.
 
Stanley S wrote:
> Hi,
>
> I'm puzzled. Why does the following cause a seg fault? Notwithstanding
> that I've already malloc() a certain space for "Hello".
>
> I do understand that using a fixed length array will work very well.
> But I wish to find out how can this be achieve using pointers.
>
> Thank you.
>
> Stan
>
> #include <stdio.h>
> #include <stdlib.h>

#include <string.h> /* for the `str*' functions you call below */
>
> int main(int argc, char *argv[])
> {
>
> char *Hello = NULL;
> char *World = NULL;
> Hello = malloc(100);

Here you (most likely -- you really *should* error check) allocate 100
chars of memory to the pointer `Hello'...
> Hello = "hello";

....which you promptly throw away (causing a memory leak) when you assign
the location of the literal string "hello" to it.

What you wanted was:
strcpy(Hello, "hello");
> World = "world";
> strcat(Hello, World);
> printf("%s\n",Hello);
>
> return 0;
> }
>

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com
http://www.cafepress.com/goldsays
"If you have nothing to hide, you're not trying!"

Eric Sosman 12-20-2005 05:43 PM

Re: strcat() and segmentation faults. But malloc() already done.
 


Stanley S wrote On 12/20/05 12:22,:
> Hi,
>
> I'm puzzled. Why does the following cause a seg fault? Notwithstanding
> that I've already malloc() a certain space for "Hello".
>
> I do understand that using a fixed length array will work very well.
> But I wish to find out how can this be achieve using pointers.
>
> Thank you.
>
> Stan
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int main(int argc, char *argv[])
> {
>
> char *Hello = NULL;
> char *World = NULL;
> Hello = malloc(100);


Now `Hello' points to an anonymous chunk of memory
big enough to hold 100 characters. (Assuming malloc()
succeeded, which you should have checked.)

> Hello = "hello";


Now `Hello' points to a completely different piece
of memory, namely, a six-character array initialized
with 'h','e','l','l','o','\0'. It no longer points to
the memory obtained from malloc(). (As an aside, since
you no longer have any record of where that memory is,
you cannot find it even to free() it -- this is what's
known as a "memory leak.")

You probably wanted `strcpy(Hello, "hello");'.

> World = "world";
> strcat(Hello, World);


This attempts to append five more characters to the
six-character array, and there isn't room for them. The
effects are in general unpredictable; someday you will
come to appreciate how helpful a nice reproducible SIGSEGV
can be. You won't always be so lucky.

> printf("%s\n",Hello);
>
> return 0;
> }
>



Stanley S 12-20-2005 06:03 PM

Re: strcat() and segmentation faults. But malloc() already done.
 
Thanks a million guys.


Christopher Benson-Manica 12-20-2005 06:10 PM

MTA - Google (was: Re: strcat() and segmentation faults. But malloc() already done.)
 
Stanley S <Stanley.Stevenson@gmail.com> wrote:

> Thanks a million guys.


It is proper Usenet etiquette to include the relevant portions of the text
you are replying to. To do this using Google groups, please follow the
instructions below, penned by Keith Thompson:

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.

Emmanuel Delahaye 12-20-2005 06:16 PM

Re: strcat() and segmentation faults. But malloc() already done.
 
Stanley S a écrit :
> Hi,
>
> I'm puzzled. Why does the following cause a seg fault? Notwithstanding
> that I've already malloc() a certain space for "Hello".
>
> I do understand that using a fixed length array will work very well.
> But I wish to find out how can this be achieve using pointers.
>
> Thank you.
>
> Stan
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int main(int argc, char *argv[])
> {
>
> char *Hello = NULL;
> char *World = NULL;
> Hello = malloc(100);


The value you have given to Hello is the address of an allocated block.

> Hello = "hello";


Now, you are replacing this value by the address of a non mutable
string. Sounds nuts, isn't it ?

> World = "world";
> strcat(Hello, World);


Of course, the destination zone not being writable, the behaviour is
undefined. Anything can happen.

I suggest you consider a function to copy the string (say strcpy(), for
instance...)

> printf("%s\n",Hello);
>
> return 0;
> }


--
A+

Emmanuel Delahaye

Martin Ambuhl 12-20-2005 06:19 PM

Re: strcat() and segmentation faults. But malloc() already done.
 
Stanley S wrote:
> Hi,
>
> I'm puzzled. Why does the following cause a seg fault? Notwithstanding
> that I've already malloc() a certain space for "Hello".


And promptly lost it ...

>
> #include <stdio.h>
> #include <stdlib.h>
>
> int main(int argc, char *argv[])
> {
>
> char *Hello = NULL;
> char *World = NULL;
> Hello = malloc(100);
> Hello = "hello";


You just overwrote the pointer Hello to point to the string literal
"hello". Not only have you now lost the pointer to the space allocated
in the previous line, but the string literal "hello" is not guaranteed
to be writable.

> World = "world";
> strcat(Hello, World);


Even if the string literal "hello" were writable, it is only 6 chars
long, so you are now trying to write into the non-allocated space beyond
the probably unwritable string "hello".

> printf("%s\n",Hello);
>
> return 0;
> }
>


Mark B 12-20-2005 06:24 PM

Re: MTA - Google (was: Re: strcat() and segmentation faults. But malloc() already done.)
 
"Christopher Benson-Manica" <ataru@nospam.cyberspace.org> wrote in message
news:do9heq$q1v$1@chessie.cirr.com...
> Stanley S <Stanley.Stevenson@gmail.com> wrote:
>
>> Thanks a million guys.

>
> It is proper Usenet etiquette to include the relevant portions of the text
> you are replying to. To do this using Google groups, please follow the
> instructions below, penned by Keith Thompson:

<snip>
> ... Flames welcome.


You asked for it... ;-)

<ot rant>
Now you're just being an idiot...
Problem was obviously solved and he was thanking those who answered.
The 'relevant portions of the text' he was replying to are no longer
relevant.
Stop being a dumbass.

Btw: Where in the usenet etiquette does it state that it is acceptable to
repeatedly post instructions to people who don't follow what you deam to
be acceptable usenet etiquette?

</ot rant>



Keith Thompson 12-20-2005 07:50 PM

Re: MTA - Google (was: Re: strcat() and segmentation faults. Butmalloc() already done.)
 
"Mark B" <sober@localbar.com> writes:
[...]
> Btw: Where in the usenet etiquette does it state that it is acceptable to
> repeatedly post instructions to people who don't follow what you deam to
> be acceptable usenet etiquette?


We don't (usually) repeatedly post instructions to the same people.
We post instructions in response to people who probably haven't seen
them before. If you did a search for the authors of the parent
articles of all the followups containing Google instructions, I think
you'd find most of them are unique, and many of them get the message
and start to use Google properly.

See also <http://cfaj.freeshell.org/google/>.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Flash Gordon 12-20-2005 09:06 PM

Re: MTA - Google
 
Mark B wrote:
> "Christopher Benson-Manica" <ataru@nospam.cyberspace.org> wrote in message
> news:do9heq$q1v$1@chessie.cirr.com...
>> Stanley S <Stanley.Stevenson@gmail.com> wrote:
>>
>>> Thanks a million guys.

>> It is proper Usenet etiquette to include the relevant portions of the text
>> you are replying to. To do this using Google groups, please follow the
>> instructions below, penned by Keith Thompson:

> <snip>
>> ... Flames welcome.

>
> You asked for it... ;-)
>
> <ot rant>
> Now you're just being an idiot...


No, he is posting useful advice to those unfortunate enough to be using
Google for whatever reason.

> Problem was obviously solved and he was thanking those who answered.
> The 'relevant portions of the text' he was replying to are no longer
> relevant.


Even then it is customary to at least leave a line or two in.

> Stop being a dumbass.


There is no need to be insulting even if you disagree.

> Btw: Where in the usenet etiquette does it state that it is acceptable to
> repeatedly post instructions to people who don't follow what you deam to
> be acceptable usenet etiquette?


It is not just what Christopher considers acceptable, it is normal
usenet etiquette. It is generally considered acceptable here to point
out normal posting conventions to those who are not aware of it, just as
it is considered acceptable to point people at the FAQ when the question
they ask is answered in the FAQ. You can tell it is generally considered
acceptable because a significant portion of the regulars do it and the
only regular poster I see complaining about it is a self acknowledged troll.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.


All times are GMT. The time now is 05:25 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.