LinuxQuestions.org
Help answer threads with 0 replies.
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 08-06-2005, 11:21 PM   #1
Dstruct0
Member
 
Registered: Jun 2005
Distribution: Debian Etch, Lenny, Squeeze
Posts: 37

Rep: Reputation: 0
Can't copy from string in Gnu C??


Hello! I'm trying to copy the contents of m to q. But i'm getting the following error:

Segmentation Fault: Core dumped

Can anyone tell me what's wrong with it?
Thanks.


------------------------------------------------------------------------------------
#include <stdio.h>
#include <string.h>




char *strdup(const char *m)
{

char *q;
strcpy(q, m);
return q;
}

int main()
{
const char *p = "s";

char *aa;
aa = strdup(p);

printf("%s\n", aa);

return 0;

}
 
Old 08-06-2005, 11:30 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
You need to alloc space for the copied string...

Hi -

1. The main problem was you didn't allocate space for your string before you tried to copy it.

char *q; // This allocates 32-bits on the stack for a pointer: nothing more
char *q = (char *)malloc (strlen (mystring) + 1); // This is actually what you wanted

2. A second problem is that the standard C library *already* provides an "strdup()"
function. If you wanted to write your own, it would probably be wise to call it
something else.

3. Here's the modified code (I also verified that the standard "strdup()" function
works fine):
Code:
#include <stdio.h>
#include <malloc.h>
#include <string.h>

char
*my_strdup(const char *m)
{
  char *q = (char *)malloc (strlen (m) + 1);
  strcpy(q, m);
  return q;
}

int
main()
{
  const char p[] = "s";
  char *aa = my_strdup(p);

  printf("%s\n", aa);

  return 0;
}
 
Old 08-06-2005, 11:41 PM   #3
Dstruct0
Member
 
Registered: Jun 2005
Distribution: Debian Etch, Lenny, Squeeze
Posts: 37

Original Poster
Rep: Reputation: 0
Thanks for ur help. Now it makes sense now.
 
Old 08-07-2005, 01:00 AM   #4
carl.waldbieser
Member
 
Registered: Jun 2005
Location: Pennsylvania
Distribution: Kubuntu
Posts: 197

Rep: Reputation: 32
Didn't you forget to free() the malloc()ed memory at the end of the program? I think that in such a short lived program, the OS will reclaim the memory, but in a long-lived program, that could be a source of problems.
 
Old 08-07-2005, 02:29 AM   #5
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Picky, picky picky.

No, you don't need to free the memory before you exit - the OS will do it for you. If Linux or Windows, not "think" - but *will*.

Yes, you definitely need to free the memory when you're done with it if the program will continue running. This is equally true of our "homegrown strdup" (where you clearly see the "malloc") as well as the standard library's version of "strdup" (which does the "malloc" behind your back, and which can be a source of memory leaks if you're not careful).

I'd strongly suggest that any time any code ever does a "free", it is ALWAYS good practice to ALSO set the pointer to NULL.

If you were coding in C++, it would be wise to use "new" and "delete" instead of "malloc()" and "free()", and it would also be wise to consider using a class (ideally, the standard C++ "string" class).

And if performance was an issue, I would consider whether or not the overhead of the default "malloc/free" and/or "new/delete" was acceptable, or whether I wanted to devise some strategy for re-using memory that I had already allocated.

All of which brings us very far afield from the original question.

Which I believe has already been answered ;-)
 
Old 08-07-2005, 10:13 AM   #6
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
I always get confused on this one.

when you create a function with malloc do you need to free the memory in the function or in the main. Also i thought that after a function exits all of the information that was contained in it is destoryed unless its static or passed back to main.
 
Old 08-07-2005, 10:39 AM   #7
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
when you create a function with malloc do you need to free the memory in the function or in the main.
When you allocate memory, you need to free it when its storage is no more needed, or before it can be used no more because reference (i.e. pointer) to it is lost. Not doing it is called a memory leak.
There is no rule about where (in the function, in the caller, in the main, wherever) the free must be done.
One important point is that the free must be done only once, thus the advice to set the pointer to NULL after freeing it.
Quote:
Also i thought that after a function exits all of the information that was contained in it is destoryed unless its static or passed back to main.
You are definitely confusing the stack and the heap here.
What is automatically freed when the program leave a function are its local variables, stored on the stack.
Malloc do not use the stack to get memory space, but the heap and that one is independant of function calls.
 
Old 08-07-2005, 10:48 AM   #8
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
Quote:
You are definitely confusing the stack and the heap here.
What is automatically freed when the program leave a function are its local variables, stored on the stack.
Malloc do not use the stack to get memory space, but the heap and that one is independant of function calls.

ohhh really wow me learned something new today i knew malloc created a pointer to
a memory location but i wasent sure if it obeyed same rules as varibles and scope.
 
  


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
need script to copy all files containing a certain string to a directory dc6463 Programming 21 04-18-2013 01:56 PM
GNU/Linux vs. GNU/OpenSolaris win32sux Linux - Software 5 11-27-2005 10:10 AM
java test if string in string array is null. exodist Programming 3 02-21-2004 01:39 PM
Exercise 1-9. Write a program to copy its input to its output, replacing each string zombi3 Programming 3 12-21-2003 02:28 AM
String copy problem rajatgarg Programming 1 11-14-2003 06:48 PM

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

All times are GMT -5. The time now is 01:19 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