Hye all,
I have noted while debugging my program (or also in runtime
execution) that sometimes (not regularly) char * pointer gets
overwritten and looses its value...
So, because of this my couple of products have failed and this is the
reason I have made it compulsary to use char [] instead of char *
because char[] don't overwrite our data...
So, anybody from you can please make me understand why this happens ??
suppose my code is,
char *name;
strcpy(name, "Jigar Mehta");
..
..
..
..
//After some lines of execution (say 50 lines) if I see what's there in
name address, it gets overwritten and value becomes "" instead of
"Jigar Mehta"... 13 2794
Jigar Mehta wrote: Hye all, I have noted while debugging my program (or also in runtime execution) that sometimes (not regularly) char * pointer gets overwritten and looses its value...
So, because of this my couple of products have failed and this is the reason I have made it compulsary to use char [] instead of char * because char[] don't overwrite our data...
So, anybody from you can please make me understand why this happens ??
suppose my code is,
char *name; strcpy(name, "Jigar Mehta");
General answer to your problem - USE std::string.
Your problem is that "char *" is a *POINTER* to a char (or the first
element in an array of chars).
The definition :
char * str;
Does not allocate any storage for "char"s at all and if you write to an
unitialized "str" you are invoking - UNDEFINED BEHAVIOUR.
For example:
char * str; strcpy(str, "THIS OVERWRITES SOME RANDOM PLACE IN MEMORY");
You can allocate "automatic" storage by placing an array of chars in the
function and then point to it however this is another source of
"UNDEFINED BEHAVIOUR".
e.g.
char * foo()
{
char data[ 100 ];
char * pointer_to_char;
pointer_to_char = &data[0];
strcpy(str, "THIS IS DEALLOCATED WHEN EXECUTION LEAVES SCOPE");
return pointer_to_char;
}
Thanks for you reply, but I am new to std::string that you suggested..
You please tell me how to use this in program by giving a code... I
understand the prob. that char * is doing... and I am programming in MS
VC++ 6.0.. So, will I be able to use std::string normally or any
special .h should be included..
Thanks again,
Jigar Mehta
Jigar Mehta wrote: Thanks for you reply, but I am new to std::string that you suggested.. You please tell me how to use this in program by giving a code... I understand the prob. that char * is doing... and I am programming in MS VC++ 6.0.. So, will I be able to use std::string normally or any special .h should be included..
You need a book. Seriously.
"Accelerated C++" by "Koenig & Moo"
is often a good choice.
--
Karl Heinz Buchegger kb******@gascad.at
Thanks for the suggestion.. I will refer to it..
Jigar Mehta wrote: Thanks for you reply, but I am new to std::string that you suggested.. You please tell me how to use this in program by giving a code... I understand the prob. that char * is doing... and I am programming in MS VC++ 6.0.. So, will I be able to use std::string normally or any special .h should be included..
VC++ 6 is too old and does not support the language in a satisfactory
level. Use VC++ 7.x (2002, 2003) or you can download and install Dev-C++: http://www.bloodshed.net/dev/devcpp.html
--
Ioannis Vranos http://www23.brinkster.com/noicys
OK!! then is Dev-C++ giving same or more functionalities than what vc++
6.0 provides.. As I am used to get features from VC++ 6.0 so, It would
be difficult for me to migrate to something giving lesser
functionality...
On 27 Jan 2005 22:23:04 -0800, "Jigar Mehta"
<ji********@gatescorp.com> wrote in comp.lang.c++: Hye all, I have noted while debugging my program (or also in runtime execution) that sometimes (not regularly) char * pointer gets overwritten and looses its value...
So, because of this my couple of products have failed and this is the reason I have made it compulsary to use char [] instead of char * because char[] don't overwrite our data...
If the return address is legitimate, here is a statement from the home
page of Jigar Mehta's employer:
"GATES Information focuses on software products and software services
with international resources and access to latest technologies. With
our highly trained development teams; a blend of experience and
academic qualifications, quality and timely services are being offered
by us."
If the OP is member of one of their development teams, with a blend of
experience and academic qualifications, and not just a first semester
student, I shudder.
And I would avoid owning stock in the company...
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Thanks for the comment.. Jigar mehta is himself representing for the
question.. No need to comment on GATES Information.. and it will take
some time to analyse http://JK-Technology.com
Thanks again for pointing...
And If I give my introduction to you, I am M.Sc. in computer
application and information techology (not a first year student.. but
thanks for giving a challenge, i like it).. age is 22.. from India..
Jack Klein wrote: If the return address is legitimate, here is a statement from the home page of Jigar Mehta's employer:
"GATES Information focuses on software products and software services with international resources and access to latest technologies. With our highly trained development teams; a blend of experience and academic qualifications, quality and timely services are being offered by us."
If the OP is member of one of their development teams, with a blend of experience and academic qualifications, and not just a first semester student, I shudder.
And I would avoid owning stock in the company...
Well he may program in some other programming language(s) and just
checking C++ (or C thinking that it is the same, while it is not).
--
Ioannis Vranos http://www23.brinkster.com/noicys
Jigar Mehta wrote: char *name; strcpy(name, "Jigar Mehta");
And If I give my introduction to you, I am M.Sc. in computer application and information techology (not a first year student.. but thanks for giving a challenge, i like it).. age is 22.. from India..
What university?
Let us know, so we can avoid taking their C courses (and their
English courses, for that matter).
Ha Ha Ha.. Thanks for the complements..
Jigar Mehta wrote: Ha Ha Ha.. Thanks for the complements..
It is not a complement. Having a Msc degree in Computer Applications
doesnt seem to help you in coding correct C++ programs. I know that you
have undergone C++ courses in your Msc degree programme. If not then
throw the Msc degree out of the window and take some decent course
which will teach what it calims to teach you.
Shan This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Jasper Dozer |
last post by:
Is this a healthy way to get a pointer to point ?
char *p = "longenough";
regards,
jasper
|
by: Marcia Hon |
last post by:
Hi,
I would like to recursively force to empty and delete a directory. As
such I would like to append the char *.
Here is the code:
char * directory = "Directory";
char * command = "rm -rf...
|
by: Peter Nilsson |
last post by:
In a post regarding toupper(), Richard Heathfield once asked me to think
about what the conversion of a char to unsigned char would mean, and whether
it was sensible to actually do so. And pete has...
|
by: jab3 |
last post by:
(again :))
Hello everyone.
I'll ask this even at risk of being accused of not researching adequately.
My question (before longer reasoning) is: How does declaring (or defining,
whatever) a...
|
by: arkobose |
last post by:
hey everyone!
i have this little problem. consider the following declaration:
char *array = {"wilson", "string of any size", "etc", "input"};
this is a common data structure used to store...
|
by: mr_semantics |
last post by:
I have been reading about the practise of casting values to unsigned
char while using the <ctype.h> functions. For example,
c = toupper ((unsigned char) c);
Now I understand that the standard...
|
by: Lee |
last post by:
Hi
Whenever I use the gets() function, the gnu c compiler gives a
warning that it is dangerous to use gets(). Is this due to the
possibility of array overflow? Is it correct that the program...
|
by: aruna.mysore |
last post by:
Hi all,
I have a simple definitioin in a C file something like this.
main()
{
char a;
.......
int k;
}
|
by: rtillmore |
last post by:
Hello,
I did a quick google search and nothing that was returned is quite
what I am looking for. I have a 200 character hexadecimal string that
I need to convert into a 100 character string.
...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
| |