LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 03-08-2005, 08:34 AM   #1
monil
LQ Newbie
 
Registered: Mar 2005
Posts: 19

Rep: Reputation: 0
strcat Core Dump


I have used the sys function to get the pathname as follows...

Code:
#include <string.h>

int main()
{
char *cmd = "asas/sdfsdf/ghgf";
char *dir = "dirname ";

strcat(dir,cmd);
puts(dir);
//system(dir);
return 0;
}
What i need is "dirname asas/sdfsdf/ghgf"

I need to pass this to the system function (commented...)

Thank you.

-Monil
 
Old 03-08-2005, 08:51 AM   #2
hk_linux
Member
 
Registered: Nov 2004
Location: India
Distribution: RedHat, PCQLinux, Fedora
Posts: 95

Rep: Reputation: 15
The memory for cmd and dir are allocated at compile time itself. You are tyring to copy another string into the same variable without allocating more memory. Thats why it is dumping.

Try allocating for cmd, dir and then reallocate for dir and proceed.

int main()
{
char *cmd;
char *dir;
int newsize;

cmd = (char *) calloc (20,1);
dir = (char *) calloc (20,1);

strcpy (cmd, "asas/sdfsdf/ghgf");
strcpy (dir, "dirname ");
newsize = strlen(cmd) + strlen(dir) +1; // 1 for NULL

dir = (char *) realloc (dir, newsize);
strcat(dir,cmd);
puts(dir);
//system(dir);
return 0;
}


HTH
 
Old 03-08-2005, 08:52 AM   #3
hk_linux
Member
 
Registered: Nov 2004
Location: India
Distribution: RedHat, PCQLinux, Fedora
Posts: 95

Rep: Reputation: 15
Also check for the return status of calloc and realloc.
 
Old 03-08-2005, 11:15 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally posted by hk_linux
Also check for the return status of calloc and realloc.
...also free() the memory when your program doesn't need the strings anymore.
e.g.:
Code:
int main()
{
    char *cmd;
    char *dir;
    int newsize;

    cmd = (char *) calloc (20,1);
    dir = (char *) calloc (20,1);

    strcpy (cmd, "asas/sdfsdf/ghgf");
    strcpy (dir, "dirname ");
    newsize = strlen(cmd) + strlen(dir) +1; // 1 for NULL

    dir = (char *) realloc (dir, newsize);
    strcat(dir,cmd);
    puts(dir);
    //system(dir);
    free(cmd);
    free(dir);
    return 0;
}
 
Old 03-08-2005, 11:20 AM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Or, easier:
Code:
#include <stdio.h>
#include <string.h>

#define MAXLEN 80

int main()
{
    char dir[MAXLEN];

    strcpy(dir, "dirname ");
    strcat(dir, "asas/sdfsdf/ghgf");
    puts(dir);
    //system(dir);
    return 0;
}
 
  


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
Core Dump File squinn Linux - Software 1 02-18-2005 12:41 PM
php <core dump>!!! shadow_wwp Programming 0 08-27-2004 02:58 AM
Core Dump MrPotatoHead AIX 3 01-14-2004 09:42 AM
core dump tomservo Linux - General 4 09-23-2002 05:35 PM
core dump lacrimae Linux - General 3 02-19-2002 01:43 PM

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

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