LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 05-10-2004, 07:28 AM   #1
LiquidSky
LQ Newbie
 
Registered: May 2004
Posts: 2

Rep: Reputation: 0
Question What is the difference between " strcpy " and opperator " = " [C/C++]


Hello !!
I am new here .. I am impressed about this forum.
I would like to ask you a question:

What is the difference between strcpy and = ?


Eg:
char *aux= new char[7];

strcpy(aux, "012345");
aux="012345";

delete [] aux;

I konw there is a differece, and I can't figuret out.


And also a stupid question:
It gives me

assertion faild (MFC)
segmentation fault (linux)

when i try to delete it.

Thanks !!
 
Old 05-10-2004, 07:51 AM   #2
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
I'll try to explain it here.

Say you have two strings, str1 and str2 declared as char * and you have allocated memory for them and whatnot.

str1 = str2; What would happen here is str1 and str2 would be pointing to the same value. If you changed element 2 of str1 you'd notice you also changed element 2 of str2.

strcpy(str1,str2); Here str1 gets the value pointed to by str2, but stays at it's current address. This way to you get two independant copies of the same data. Edit one and the other doesn't change.

The first syntax, str1 = str2, is also dangerous if you have allocated memory for str1 because the pointer to that memory has not been lost. Do this in a function that is called often and all of a sudden you have a nice memory leak.

An dirty example of real code:

Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
	char *str1 = new char[7];
	printf("This is a string addr: %d\n", (int) str1);
        str1 = "1234567";
	printf("This is a string addr: %d\n", (int) str1);
        delete str1;
        return 0;
}
Results:
This is a string addr: 134520912
This is a string addr: 134514303
Segmentation fault

Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
	char *str1 = new char[7];
	printf("This is a string addr: %d\n", (int) str1);
        strcpy(str1,"1234567");
	printf("This is a string addr: %d\n", (int) str1);
        delete str1;
        return 0;
}
Results:
This is a string addr: 134520912
This is a string addr: 134520912


Conclusion:
As you can see here, in the first example the address pointed to by str1 changed after the assignment. So the memory allocated was lost. Then when you go to delete str1, it segfaults.

In the 2nd example the address pointed to does not change, and there is no seg fault.

In short, use strcpy.

Last edited by jtshaw; 05-10-2004 at 07:53 AM.
 
Old 05-12-2004, 04:18 AM   #3
LiquidSky
LQ Newbie
 
Registered: May 2004
Posts: 2

Original Poster
Rep: Reputation: 0
Thank You !!!
a very nice explication. Thanks !!
 
Old 05-12-2004, 05:15 AM   #4
UltimaGuy
Member
 
Registered: Aug 2003
Location: Chennai, India
Distribution: PCLinuxOS .92, FC4
Posts: 840

Rep: Reputation: 32
Well, this is indeed a nice explanation. It cleared some doubts for me also
 
  


Reply



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
newbie question: whats the difference between "su root", "su" and "su -&quo mojarron Slackware 9 12-07-2009 04:08 PM
Can you explain the difference between "Free Software (GNU)" and "Open Source"? vharishankar General 5 03-03-2005 09:40 AM
what is difference between "reset" and "clean " commands? zameer_india Linux - Newbie 2 01-10-2005 06:23 AM
difference between "Web server local URL" and "IPv4 address"? kpachopoulos Linux - General 2 09-17-2004 01:30 PM
"User" & "System" CPU load difference JJX Linux - General 3 06-06-2004 01:42 AM

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

All times are GMT -5. The time now is 03:37 AM.

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