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

J

Jigar Mehta

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

Gianni Mariani

Jigar said:
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;
}
 
J

Jigar Mehta

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
 
K

Karl Heinz Buchegger

Jigar said:
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.
 
I

Ioannis Vranos

Jigar said:
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
 
J

Jigar Mehta

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

Jack Klein

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

Jigar Mehta

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

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

Ioannis Vranos

Jack said:
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).
 
O

Old Wolf

Jigar said:
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).
 
S

Shan

Jigar said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top