LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-15-2005, 01:49 AM   #1
gjagadish
Member
 
Registered: Oct 2005
Location: India
Distribution: Fedora Core
Posts: 88

Rep: Reputation: 15
Problem the Strings in C++


Hi guys,

i wrote one simple program in C++ as follows,

#include<iostream.h>
#include<string.h>
main()
{
char *temp="How are you";
char *ret="";
strcpy(ret,temp);
cout<<temp;
}

It is running in windows fine.
But with linux, it gives "Segmentation Fault (core dumped)" Error.
But it compiles with no error.

If i change the destination variable in strcpy to fixed array size like
char ret[100];

it works fine in linux.

why linux is not accepting the destination as pointer char.

thanx in advance
Jagadish
 
Old 10-15-2005, 02:05 AM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,783

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
The question should be, "Why is Windows accepting running this buggy code ?"
No surprise so many viruses are happy in the Windows ecosystem !
 
Old 10-15-2005, 02:14 AM   #3
gjagadish
Member
 
Registered: Oct 2005
Location: India
Distribution: Fedora Core
Posts: 88

Original Poster
Rep: Reputation: 15
Hi,

Can u explain me what is wrong with that code.

Can't we use the char pointer as destination in strcpy.

thanx for ur reply
Jagadish
 
Old 10-15-2005, 03:43 AM   #4
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
The problem is that the character array at "ret" is not large enough (only 1 char) to hold the null-terminated string "How are you" (12 chars) and you're writing past the end of the array into other parts of memory.
 
Old 10-15-2005, 03:45 AM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,783

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
You can use a char pointer as destination, but it need to point to some memory you are allowed to write to and with sufficient storage.
This pointer is initialized in your code to a one byte array that may or may not be writable, depending of the compiler implementation/options, the strcpy is also trashing some part of your windows stack as you are overwriting whatever is after this one byte array with the remaining of your source string.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to find duplicate strings in vertical column of strings markhod Programming 7 11-02-2005 04:04 AM
php problem with strings graziano1968 Programming 5 10-07-2005 02:10 PM
parsing strings problem McB Programming 6 04-19-2004 09:50 AM
Problem in C: arrays, functions and strings OrganicX Programming 15 03-18-2003 09:31 AM
Problem with strings in Red Hat 8.0 smriti Linux - General 1 03-03-2003 07:59 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration