Strings copyingmore than expected

  • Thread starter taupune
  • Start date
  • Tags
    Strings
In summary, the conversation is about a user's code that is not working properly due to possible errors in the disect function and not properly initializing character arrays. The user is seeking help and suggestions from others to resolve the issues in their code. They also discuss the benefits of using passing by value versus passing by reference, and the importance of properly terminating strings and checking for valid input in C++.
  • #1
taupune
25
0
Hi ,I am doing some strings operation but it seems that compiler is doing extra copping.
Can someone else verify this or is it my sloppy coding?

Also I had to write
std::cin ...etc otherwise compiler would like the code without that expression in main function.
I am using Visual studio 2010.

#include <iostream>
using namespace std;
#include "string.h"

void disect (char TheMessage[], int startloc, char messagepart[], int messagelength);
void ResultCodeTable ( char Result[]);

int main ()
{

char test[3];
char message [140];
char enter;
std::cout << "hello enter the mesage: "<<endl;
std::cin>> message;
std:: cout<<"uncopied string: " <<test<<endl;
disect(message,3,test, 3);
std:: cout<<"copied string: " <<test<<endl;
std::cout<<"this is the message: "<<message<<endl;
std::cin>> enter;
return (0);
}


//Dissect cuts the mesage into several parts , where each part provides the separate meaning of itself

void disect (char TheMessage[], int startloc, char messagepart[], int messagelength)
{
cout<< "this is pre message part inside function printing: "<<messagepart<<endl;
cout<<"start loc is:"<<startloc<<" end loc is "<<messagelength<<endl;
for ( int i = 0; i<messagelength; i++)
{
messagepart= TheMessage[startloc];
startloc++;
cout<<"message part array : "<<messagepart<<endl;
cout<<"message original mesage : "<<TheMessage<<endl;
}
cout<< "end of function execution inside function printing: "<<messagepart<<endl;
}
 
Technology news on Phys.org
  • #2
taupune said:
Hi ,I am doing some strings operation but it seems that compiler is doing extra copping.
Can someone else verify this or is it my sloppy coding?

Also I had to write
std::cin ...etc otherwise compiler would like the code without that expression in main function.
I am using Visual studio 2010.

Code:
#include <iostream>
using namespace std;
#include "string.h"

void disect (char TheMessage[], int startloc, char messagepart[], int messagelength);
void ResultCodeTable ( char Result[]);

int main ()
{
	
	char test[3];
	char message [140];
	char enter;
	std::cout << "hello enter the mesage: "<<endl;
	std::cin>> message;
	std:: cout<<"uncopied string:   " <<test<<endl;
	disect(message,3,test, 3);
	std:: cout<<"copied string:   " <<test<<endl;
	std::cout<<"this is the message: "<<message<<endl;
	std::cin>> enter;
	return (0);
}


//Dissect cuts the mesage into several parts , where each part provides the separate meaning of itself

void disect (char TheMessage[], int startloc, char messagepart[], int messagelength)
{
	cout<< "this is pre message part inside function printing: "<<messagepart<<endl;
	cout<<"start loc is:"<<startloc<<"  end loc is "<<messagelength<<endl;
	for ( int i = 0;  i<messagelength; i++)
	{
		messagepart[i]= TheMessage[startloc];
		startloc++;	
		cout<<"message part array : "<<messagepart[i]<<endl;
		cout<<"message original mesage : "<<TheMessage[i]<<endl;		
	}
	cout<< "end of function execution inside function printing: "<<messagepart<<endl;
}

This is just a dummy post to put code tags around your code. As a pointer if you post code in the future but CODE and /CODE tages with square braces around them.
 
  • #3
taupune said:
Hi ,I am doing some strings operation but it seems that compiler is doing extra copping.
Can someone else verify this or is it my sloppy coding?

It's your coding.

Made a small change to your disect function
Code:
void disect (char TheMessage[], int startloc, char messagepart[], int messagelen
gth)
{
    cout<< "this is pre message part inside function printing: "<<messagepart<<e
ndl;
    cout<<"start loc is:"<<startloc<<" end loc is "<<messagelength<<endl;

    int i;
    for ( i = 0; i<messagelength; i++)
    {
        messagepart[i]= TheMessage[startloc];
        startloc++;

        cout<<"message part array : "<<messagepart[i]<<endl;
        cout<<"message original mesage : "<<TheMessage[i]<<endl;
    }
    messagepart[i] = 0;

    cout<< "end of function execution inside function printing: "<<messagepart<<
endl;
}

A lot of standard library functions expect a 0 ('\0') to indicate end of string.


Also if you use C++ strings instead of C strings, the program becomes much easier
Code:
#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::string;

int main()
{
        string message;
        string test;

        cout<<"Enter message: ";
        cin>>message;


        test = message.substr(3,3);
        cout<<test<<'\n';

}
 
  • #4
Ok I had a look at your code and I will share my thoughts.

The first thing is that it seems that are you passing the two arguments for your disect function by value and not by reference.

The difference between the two is that passing by value passes a copy of the actual data on the stack to the function whereas passing by reference gives the function a pointer to the object. In C, you use a pointer to the object to pass by reference and if you are using C++ you can use an ampersand '&' to pass the pointer to the function, but still treat the object like you are treating it in your function. If you are unaware of these things it would be helpful for your to look these up.

Also you have not initialized your character arrays and you are printing the test array that is not initialized at the beginning of your code. You also need to make sure the strings are terminated properly. I don't know if this was a mistake or not.

Another thing to help us would be to give us the output from the console window. Since you are using cout to dump output to the standard output device, this information will basically tell us exactly what is happening.

Also another tip would include to do things like make sure that you do not copy parts of the string that don't exist. This would mean checking that the part of the string you are copying in your disect function checks to make sure that all the characters that are being copied are in the range of characters that is inputted by the user. Usually in C++ people use either things like a standard library implementation for string or they write their own custom class that has an extra variable for the string size. In addition to this, they also add class functions to do all this kind of checking automatically so you don't have to.
 
  • #5
chiro said:
Ok I had a look at your code and I will share my thoughts.

The first thing is that it seems that are you passing the two arguments for your disect function by value and not by reference.

The difference between the two is that passing by value passes a copy of the actual data on the stack to the function whereas passing by reference gives the function a pointer to the object. In C, you use a pointer to the object to pass by reference and if you are using C++ you can use an ampersand '&' to pass the pointer to the function, but still treat the object like you are treating it in your function. If you are unaware of these things it would be helpful for your to look these up.

His argument passing has no problems.

char message[140];

to function f, if you pass
f(message);

What you are actually passing is
f(&message[0]);

So you are passing a pointer to the first element of the array.
So a copy is created, but not a copy of the array, but a copy of the pointer, which is exactly what you want.


chiro said:
Also you have not initialized your character arrays and you are printing the test array that is not initialized at the beginning of your code.

message gets initialized by cin
test gets initialized in disect

chiro said:
You also need to make sure the strings are terminated properly. I don't know if this was a mistake or not.

message gets terminated by 0 ('\0') by cin.
test is not - that's the main mistake in his code.
 
  • #6
phiby said:
His argument passing has no problems.

char message[140];

to function f, if you pass
f(message);

What you are actually passing is
f(&message[0]);

So you are passing a pointer to the first element of the array.
So a copy is created, but not a copy of the array, but a copy of the pointer, which is exactly what you want.

I think I've mixed up something here.

If you pass say something like char myString[140], then that gets put on the stack but if you pass it without a size then that gets passed by reference. It's been a while since I've done any serious coding so thanks for the correction.
 
  • #7
chiro said:
I think I've mixed up something here.

If you pass say something like char myString[140], then that gets put on the stack
No. What gets passed is the address of the string in memory, which is to say &myString[0], just as phiby said.

This is the way it works in C and C++ if you're working with strings as arrays of type char.

In general, the name of an array is an address, so an actual parameter that is the name of an array is passed by reference.
chiro said:
but if you pass it without a size then that gets passed by reference. It's been a while since I've done any serious coding so thanks for the correction.
 
  • #8
Mark44 said:
No. What gets passed is the address of the string in memory, which is to say &myString[0], just as phiby said.

This is the way it works in C and C++ if you're working with strings as arrays of type char.

In general, the name of an array is an address, so an actual parameter that is the name of an array is passed by reference.

So just do I don't forget next time, how do you pass an array on the stack in C/C++ functions? As an example, a character array?
 
  • #9
chiro said:
So just do I don't forget next time, how do you pass an array on the stack in C/C++ functions? As an example, a character array?

There is no simple way to pass a copy of the whole array in C (you could do it in a twisted way - by wrapping the array in a struct & passing the struct by value).

In C++, if you have a std::string, it can be passed by value semantics.
 

1. Why is my string being copied more than the expected number of times?

This could be due to a mistake in your code, such as using a loop that is not properly controlled or using the wrong function for copying strings. It could also be caused by unexpected input data.

2. How can I prevent my string from being copied more than the expected number of times?

To prevent this issue, carefully review your code and ensure that your loops are properly controlled and that you are using the correct function for copying strings. Also, consider adding input validation to ensure that unexpected data does not cause unexpected behavior.

3. Can memory issues cause strings to be copied more than expected?

Yes, if your program is running low on memory or if there is a memory leak, it could cause unexpected behavior such as strings being copied more than expected. It is important to review your memory management in your code and make sure that you are properly allocating and freeing memory.

4. Is it possible for a string to be copied an infinite number of times?

No, it is not possible for a string to be copied an infinite number of times. Eventually, the program will reach its memory limit or encounter an error that will cause it to stop copying the string.

5. How can I troubleshoot and debug issues with strings being copied more than expected?

To troubleshoot and debug this issue, you can use debugging tools such as a debugger or print statements to track the flow of your code and see where the issue may be occurring. You can also try running your program with different input data to see if that affects the number of times the string is copied.

Similar threads

  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
12
Views
1K
Replies
10
Views
950
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
5
Views
871
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
2
Views
860
Back
Top