473,385 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

[Help] Program crashes/seg-faults on strcpy().

a2x
Hi, I've fixed this error, but I don't know why it occurs. Do you?

Code:

#include <stdlib.h>
#include <string.h>

void another();

int main()
{
char* temp;
strcpy(temp, "Hi.\n");
printf(temp);

another();
return(0);
}

void another()
{
char* temp1;
strcpy(temp, "Hi again.\n");
}
Output:
Hi.
Segmentation fault
The segmentation fault refers to the second call of strcpy(). If I
don't call strcpy() the second time the program runs fine. Everything
works fine if I replace 'char* temp1' with 'char temp1 = (int*) malloc
(200)'.

Do I have to use malloc before every call to strcpy()? Is my compiler
(gcc) just randomly letting me off doing it the first time?

Thanks.

Nov 15 '05 #1
4 2049
In article <11*********************@o13g2000cwo.googlegroups. com>,
a2x <ad***********@hotmail.com> wrote:
char* temp;
strcpy(temp, "Hi.\n");
You haven't allocated any space for temp to point to.
char* temp1;
strcpy(temp, "Hi again.\n");
You haven't allocated any space for temp1 to point to.
If I
don't call strcpy() the second time the program runs fine.
Just luck. Both calls are wrong.
Do I have to use malloc before every call to strcpy()?


That's almost right. You have to have space pointed to by the
destination for each strcpy. You don't necessarily have to malloc it
every time, if you happen to have it already pointing to enough space.

-- Richard
Nov 15 '05 #2
"a2x" <ad***********@hotmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
Hi, I've fixed this error, but I don't know why it occurs. Do you?

Code:

#include <stdlib.h>
#include <string.h>

void another();

int main()
{
char* temp;
strcpy(temp, "Hi.\n");

<snip>

Whatever happens later in the code does not matter at this point.

When you declare a pointer to char, the system gives you x bytes of data
from the memory somewhere, which may contain the address of a zero
terminated array of chars.

The strcpy function assumes that it already does, and starts copying the
contents of the second argument into it, which basically means writing bytes
to a random place in the memory, which the computer may respond negatively
to, such as a segfault.
Nov 15 '05 #3
a2x wrote:
Hi, I've fixed this error, but I don't know why it occurs. Do you?

Code:

#include <stdlib.h>
#include <string.h>

void another();

int main()
{
char* temp;
strcpy(temp, "Hi.\n");
printf(temp);

another();
return(0);
}

void another()
{
char* temp1;
strcpy(temp, "Hi again.\n"); ^^^^
1) There is no variable 'temp' in scope. This suggests that this is
*not* the code you actually used.
2) if you meant 'temp1', that is a wild pointer and you gave copied
"Hi again.\n" into your grandfather's pacemaker.
}
Output:
Not for the code you posted, which doesn't compile.
Hi.
Segmentation fault

Nov 15 '05 #4

"a2x" <ad***********@hotmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
Hi, I've fixed this error, but I don't know why it occurs. Do you?

Code:

#include <stdlib.h>
#include <string.h>
#include <stdio.h> /* contains the declaration for the printf()
function you used */
void another();

int main()
This should be int main(void).
{
char* temp;
strcpy(temp, "Hi.\n");
You haven't allocated any space for 'temp' to point to, so this invokes
undefined behaviour.
printf(temp);
There is no declaration of printf() in scope because you failed to #include
<stdio.h>.
another();
return(0);
}

void another()
{
char* temp1;
strcpy(temp, "Hi again.\n");
As someone already pointed out, there is no variable 'temp' in scope. When
posting code, try copying and pasting it directly into your newsreader,
rather than retyping it.
Otherwise, if you have meant 'temp1' then, again, you do not allocate space
for the pointer 'temp1' to point to and are copying your string literal in a
random place in memory.
}

Do I have to use malloc before every call to strcpy()? Is my compiler
(gcc) just randomly letting me off doing it the first time?


You have to allocate space for the destination of strcpy(). One method of
achieving this is calling malloc().

-- Denis
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: kschnee | last post by:
Hi. I've got a neat artificial intelligence project I want to distribute as an EXE. I'm using Python, Pygame, and Numeric. My problem is: When I use py2exe, I'm told "The following modules appear...
4
by: arizvi | last post by:
Hi, My program crashes when it reaches a certain line. There is nothing wrong with that particular line as I have changed it to something simple like i=0, and it still crashes. If I remove...
2
by: Adrian | last post by:
hi I had the following code (fragment) in a VB6 app, what should the XML bits look like in VB.Net? Thanks Public Sub XML_list(ByVal xml As String) Dim xmldom As MSXML2.DOMDocument
7
by: BT | last post by:
Ok, for a school assignment we have to use a pointer for an array of ints, intstead of the usual X way, it compiles fine but when i run it I am getting a seg fault that i can't figure out how to...
31
by: Extremest | last post by:
I have a loop that is set to run as long as the arraylist is > 0. at the beginning of this loop I grab the first object and then remove it. I then go into another loop that checks to see if there...
8
by: Ben | last post by:
Hi, I am having trouble debugging a segmentation fault...here's my data structure: typedef struct CELL *pCELL; /* Pointers to cells */ struct CELL { SYMBOL symbol; pCELL prev_in_block;...
6
by: HelpME | last post by:
I wrote a program in Vb.Net that was running fine. However I am unable to install it on a couple of machines. When i run it I get a windows error message that says My Project.exe has...
0
by: xenia200 | last post by:
I HAVE THIS FOR EXAMPLE <misc> <cntextdef /> <vntextdef /> <xreflist> <xrefdef id="1" name="`Heading &amp; Page'"> “<ut value="&lt;$paratext&gt;" />” on page<ut...
4
by: Goran | last post by:
Hi all, i have a problem on program termination. At the moment when main() ends my program creates a segmentation fault. #include <libmylib/myObject.h>
4
by: nitusa | last post by:
Hey Everyone, I am doing a VB6 to C# conversion and everything was going smoothly until I realized that I needed to call a Fortran 77 (.for) .dll inside my code. I have looked through everything...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.