473,386 Members | 1,698 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,386 software developers and data experts.

copy the string

1
Hi all

I am facing the problem of grabage value while copying the string as follows-

char strTemp[150]="";
if (cls.GetVerifySucValue()==0)
strcpy(strTemp,"#No Suction or Discharge valves were found matching your Pre-Selection options");

(Actually this is in the intemmedite DLL so that i can not debug it)

Thanks in advance
Meenal
May 5 '07 #1
14 2325
Savage
1,764 Expert 1GB
Hi all

I am facing the problem of grabage value while copying the string as follows-

char strTemp[150]="";
if (cls.GetVerifySucValue()==0)
strcpy(strTemp,"#No Suction or Discharge valves were found matching your Pre-Selection options");

(Actually this is in the intemmedite DLL so that i can not debug it)

Thanks in advance
Meenal
This is not going to work.char strTemp[150]="",it is not correct.Instead use

char strTemp[150];
and then strTemp[0]='\0' where '\0' is null terminating character which terminate the string,this means that now this string is empty and ready to copy data into it correctly

Savage.
May 5 '07 #2
AdrianH
1,251 Expert 1GB
This is not going to work.char strTemp[150]="",it is not correct.Instead use

char strTemp[150];
and then strTemp[0]='\0' where '\0' is null terminating character which terminate the string,this means that now this string is empty and ready to copy data into it correctly

Savage.
Sorry to disagree Savage, but it is valid to do that.

The code has nothing wrong with it. The garbage value is coming from somewhere else. If strTemp is being returned, that would probably be the problem as strTemp is located on the stack and is indeterminate after returning from the call.

If strTemp is not being returned, then you will have to provide more information to help you debug the problem.


Adrian
May 5 '07 #3
Savage
1,764 Expert 1GB
Sorry to disagree Savage, but it is valid to do that.

The code has nothing wrong with it. The garbage value is coming from somewhere else. If strTemp is being returned, that would probably be the problem as strTemp is located on the stack and is indeterminate after returning from the call.

If strTemp is not being returned, then you will have to provide more information to help you debug the problem.


Adrian
U are right.The problem is not in that part and the code is correct.I made a big stupid error. :(

Thanks,for correction!

Savage
May 5 '07 #4
AdrianH
1,251 Expert 1GB
U are right.The problem is not in that part and the code is correct.I made a big stupid error. :(
Happens to the best of us. ;)


Adrian
May 5 '07 #5
Savage
1,764 Expert 1GB
Happens to the best of us. ;)


Adrian
Tnx! ;)

Savage
May 5 '07 #6
arunmib
104 100+
Try doing a "memset" of strTemp, before copying the string....
May 5 '07 #7
AdrianH
1,251 Expert 1GB
Try doing a "memset" of strTemp, before copying the string....
What is your reasoning for suggesting that?


Adrian
May 5 '07 #8
arunmib
104 100+
What is your reasoning for suggesting that?


Adrian
Nothing much, I think it's always a safe habit to clear the location (critical ones) before using it. If some other section of code is overwriting this location (strTemp), now that location will be giving errornous or not acceptable result which would help in locating where the real problem would be....

Or did I say something wrong, if so then can you correct me....
May 7 '07 #9
AdrianH
1,251 Expert 1GB
Nothing much, I think it's always a safe habit to clear the location (critical ones) before using it. If some other section of code is overwriting this location (strTemp), now that location will be giving errornous or not acceptable result which would help in locating where the real problem would be....

Or did I say something wrong, if so then can you correct me....
Not exactly wrong, just redundant. The OP stated this:
Expand|Select|Wrap|Line Numbers
  1. char strTemp[150]="";
which means that it is already initialised. Doing a memset (I'm assuming you mean to all 0s) would just do the same thing.

Initialisation is always a good habit arunmib, and I'm glad you do it. Stack (auto) variables however are better initialised using compiler based initialisation techniques instead of using functional ones because it can be faster (though not necessarily true, the optimiser may be able to do additional things more easily) and because using the initialisation at declaration idiom is (in my mind at least) safer as you are ensuring that the variable is always initialised right from the beginning.

I don’t subscribe to initialising only critical variables before using them idiom. IMHO, I feel all variables are fairly critical to the operation of the function, otherwise you really shouldn’t have them. I feel all variables should be initialised at declaration, and let the optimiser remove any redundant initialisations from there.

Keep up the good work arunmib,


Adrian
May 7 '07 #10
weaknessforcats
9,208 Expert Mod 8TB
The code as written runs just fine. The error is elsewhere.

Check every location that uses strTemp and verify there are no errors.
May 7 '07 #11
Hi, It's always good to initialize char array with memset to avoid any memory corruption further ..memset(nameofarray,'\0',sizeof(nameofarray)).
May 8 '07 #12
weaknessforcats
9,208 Expert Mod 8TB
Hi, It's always good to initialize char array with memset to avoid any memory corruption further ..memset(nameofarray,'\0',sizeof(nameofarray)).
This does no good. There are no provisions to force you to stay within array bounds and a zero may not be the correct initial value.

If you are in C, and the char array is to contain a C-string , then only element 0 need be \0. Your programming must simply be correct as there are no protections.

If you are in C++, you should be using basic_string<char>, which is the C++ string type.
May 8 '07 #13
AdrianH
1,251 Expert 1GB
Hi, It's always good to initialize char array with memset to avoid any memory corruption further ..memset(nameofarray,'\0',sizeof(nameofarray)).
No need. Please read post 10 for further explination.


Adrian
May 8 '07 #14
AdrianH
1,251 Expert 1GB
This does no good. There are no provisions to force you to stay within array bounds and a zero may not be the correct initial value.

If you are in C, and the char array is to contain a C-string , then only element 0 need be \0. Your programming must simply be correct as there are no protections.

If you are in C++, you should be using basic_string<char>, which is the C++ string type.
memset is valid to use here, just not necessary. '\0' is equal to 0, if it weren't, it would break a lot of code.

You are correct that there are no provisions to keep you from straying outside the bounds of the array except for the definition of a C string, which is that it is null terminated (ends with the last char in the string being '\0'.

basic_string is good for many things, and can be very safe. It is up to the programmer to decide if it is the best thing to do. There are times that effecency requirements may restrict the you from moving to basic_string (though this argument is getting weaker as CPUs get faster).


Adrian
May 8 '07 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

22
by: Shea Martin | last post by:
I have a String class (I know I am re-inventing the wheel, yes I have heard of boost, and of QString). My copy constructor does a deep (strcpy) of the char *_buffer member. I have a member...
42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
5
by: LRS Kumar | last post by:
Stoustrup, in The C++ Programming Language, has the following example in 11.7: string g (string arg) // string passed by value using copy constructor { return arg; //string...
16
by: bluekite2000 | last post by:
I want Matrix A(B) to create shallow copy of B but A=B to create deep copy of B. Is that bad design? Why and why not?
8
by: Jesper | last post by:
Hi, Does the concept "copy constructor" from c++ excist in c#. What is the syntax. best regards Jesper.
14
by: Arne | last post by:
In C++ we have a copy constructor. What is the equivalent in .Net? Would that be a clone method?
12
by: CMirandaman | last post by:
Sounds like a stupid question I know. I can tell that they are used to copy strings. But what is the difference between x = y; versus x = String.Copy(y); Or are they essentially the same?
18
by: sd2004 | last post by:
could someone please show/help me to copy all element from "class dog" to "class new_dog" ? Note: "class new_dog" has new element "age" which should have value "my_age"...
18
by: active | last post by:
Console.WriteLine(String.Equals(s, s.Clone.ToString)) s is type string. This returns True. If Clone really made a new copy I'd expect False.
11
by: Dijkstra | last post by:
Hi folks! First, this is the code I'm using to expose the problem: ------------------------------------------------------------------ #include <functional> #include <string> #include...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.