Solved

using strcpy in c++ error

Posted on 2011-03-21
7
390 Views
Last Modified: 2012-08-13
Can you please tell me what's wrong with this code?
I need to use strcpy for my homework and I keep getting a "Run-time check failure #2:Stack around st2 was corrupted"

btw, I am trying to just print State

Thanks guys!
#include<iostream>
#include<cstring>

using namespace std;

int main(){
	char st1[20]="State of Ca";
	char st2[5]={' '};

	strcpy(st2, st1);
	cout<<st2<<endl;
	
return 0;
}

Open in new window

0
Comment
Question by:pgmerLA
  • 3
  • 2
  • 2
7 Comments
 
LVL 8

Accepted Solution

by:
crysallus earned 300 total points
ID: 35185946
st2 is only 5 elements in size, which is too small to fit all of st1 in it. If you only want to copy part of the string, then use strncpy, where the third argument is the no. of char's to copy. Eg:

const int ST2_LEN = 5;
...
char st2[ST2_Len] = {' '};
strncpy(st2, st1, ST2_LEN);
...

Open in new window

0
 

Author Comment

by:pgmerLA
ID: 35185954
is there any way I could use strcpy?
0
 
LVL 8

Expert Comment

by:crysallus
ID: 35185967
strcpy always copies the whole of the source (st1), into the destination (st2), as such the destination must be at least as large as the source, including null-terminating character.

So, to copy only "State", or have the destination string smaller than the source, no.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 200 total points
ID: 35186107
BTW, if you want to store "state" as a c-style string in an array, then don't forget that you need a terminating null byte for a total of 6 bytes. Your st2 is only 5 bytes.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35186111
oops, crysallus, I didn't see that you said "including null-terminating character". So, never mind.
0
 
LVL 8

Expert Comment

by:crysallus
ID: 35186181
Actually, you were still kind of right to pick up on that, as I hadn't put 6 in my sample code in my first post, which I should have.

But yes, my explanation there probably should have got that across.
0
 

Author Comment

by:pgmerLA
ID: 35186453
Thanks guys.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now