473,398 Members | 2,113 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,398 software developers and data experts.

why char * gets overwritten after some time in execution...

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"...

Jul 23 '05 #1
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;
}
Jul 23 '05 #2
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

Jul 23 '05 #3
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
Jul 23 '05 #4
Thanks for the suggestion.. I will refer to it..

Jul 23 '05 #5
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
Jul 23 '05 #6
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...

Jul 23 '05 #7
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
Jul 23 '05 #8
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...

Jul 23 '05 #9
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..

Jul 23 '05 #10
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
Jul 23 '05 #11
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).

Jul 23 '05 #12
Ha Ha Ha.. Thanks for the complements..

Jul 23 '05 #13

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

Jul 23 '05 #14

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

Similar topics

19
by: Jasper Dozer | last post by:
Is this a healthy way to get a pointer to point ? char *p = "longenough"; regards, jasper
16
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...
2
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...
5
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...
7
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...
14
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...
302
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...
74
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; }
14
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. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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...
0
Oralloy
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,...
0
jinu1996
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...
0
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...
0
tracyyun
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...
0
agi2029
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,...
0
isladogs
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...

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.