|
|
What is wrong in this strcpy()?
I tried this function call, no compile time errors, but results in error during runtime.
What could be the problem in that function call?
char *s= "TEST";
char *s1= NULL;
char * s_cpy(char * s,char * s1)
{
char * p=(char*) malloc(strlen(s));
p=s;
printf("%s\n",p); // prints TEST
printf("%s\n",s1); // prints <null>
printf("%s\n",s); // prints TEST
strcpy(s1,p); //error, test.exe has encountered a problem
return s1;
}
|
|
|
|
|
Member 5502879 wrote: printf("%s\n",s1); // prints
Prints what? Junk? Zero? Null?
Member 5502879 wrote: strcpy(s1,p); //error, test.exe has encountered a problem
You never allocated memory to s1, but are trying to write to something to it.
Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche
.·´¯`·->Rajesh<-·´¯`·.
[Microsoft MVP - Visual C++]
|
|
|
|
|
I am sorry for tat.
Since i have already set
char *s1=NULL;
printf("%s\n",s1); // prints NULL
|
|
|
|
|
If you're calling s_cpy() function with s, s1 (global variables i.e.
char *s= "TEST";
char *s1= NULL; ) the formal parameter s1 will hold NULL (invalid pointer) which makes strcpy() crash.
The prototype for strcpy() is
char *strcpy( char *strDestination, const char *strSource);
Passing invalid pointer at any of the parameter to strcpy (or any string function taking char *) will crash the program.
|
|
|
|
|
Even when i change s1 to
char *s1="TEMP";
strcpy(s1,p); // resulting in error.
|
|
|
|
|
char *s1 = "TEMP" defines the char pointer to constant "TEMP", of which you cann't change the value. which makes strcpy() fail.
|
|
|
|
|
Member 5502879 wrote: char *s= "TEST";
char *s1= NULL;
char * s_cpy(char * s,char * s1)
{
char * p=(char*) malloc(strlen(s));
p=s;
printf("%s\n",p); // prints TEST
printf("%s\n",s1); // prints
printf("%s\n",s); // prints TEST
strcpy(s1,p); //error, test.exe has encountered a problem
return s1;
}
Good C tutorial needed.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
One suggestion
Member 5502879 wrote: char * p=(char*) malloc(strlen(s));
p=s;
you allocating buffer and assigning some other pointer to p again.
this will cause memory leak.
Regards,
Sandip.
|
|
|
|
|
Even if do not create 'p' and directly copy s to s1,
strcpy(s1,s);
im still getting the same error.
|
|
|
|
|
Try allocating memory to s1 before you copy stuff into it and then tell if it crashes. If it doesn't, well, guess why.
Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche
.·´¯`·->Rajesh<-·´¯`·.
[Microsoft MVP - Visual C++]
|
|
|
|
|
I did not answer you why it crashes..
others have already told you the reason.
I just pointed the mistake.
Regards,
Sandip.
|
|
|
|
|
try allocate memory like this:
char * s_cpy(char * s,char * s1)
{
char * p =(char*) malloc(strlen(s)+1);
s1 = (char*) malloc(10); //use this!!!!
p=s;
printf("%s\n",p); // prints TEST
printf("%s\n",s1); // prints
printf("%s\n",s); // prints TEST
strcpy(s1,p); //error, test.exe has encountered a problem
return s1;
}
don forget to free them up later or you will encounter memory leak...
|
|
|
|
|
auralius wrote: char * p =(char*) malloc(strlen(s)+1);
s1 = (char*) malloc(10); //use this!!!!
p=s;
And what's the point of doing the above?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
allocating memory so i won't be a NULL pointer anymore...
|
|
|
|
|
char * p =(char*) malloc(strlen(s)+1);
s1 = (char*) malloc(10);
p=s;
allocating memory for p and then setting p=s; make no sense to me.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
yup, i know...i meant allocating memory for s1..
my concern wasn't on part where p=s. i thought application crashed due to NULL pointer on s1...that's why we must allocate memory for s1...
|
|
|
|
|
I see two issues.
Member 5502879 wrote: char * p=(char*) malloc(strlen(s));
p=s;
You've reassigned p to some other address. This will cause problems if you go to free the address returned by malloc() .
Member 5502879 wrote: strcpy(s1,p); //error, test.exe has encountered a problem
Because s1 has not been allocated any memory. strcpy() is therefore dereferencing a null pointer.
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
|
|
|
|
|
Several things are wrong. As pointed out, s1 is allocated as just a pointer to a string so you cannot copy data to it. In addition, you allocated only strlen bytes for the p copy, you need to allocate strlen+1 to allow the terminating null for string s. You may get away with this if a paragraph (16 bytes) is probably allocated for the malloc, but it is still bad code.
|
|
|
|
|
I observed following issues
1) Issue of Memory allocation
2) this is the case where we can understand the Global an Local variable concept. Try to name ur sencond argument in list with different name. May be it will work.
The secret of life is not enjoyment
but education through experience.
- Swami Vivekananda.
|
|
|
|
|
Hi!
I've two functions in the same file. I've a pointer variable in the first function. I want to know the contents of this pointer variable from the second function. How to make the pointer variable visible in the second function?
|
|
|
|
|
option 1 (plain C ):
make the variable global (or having scope-file, declaring it static ).
option 2 (C++ ):
if both functions are actually methods of a class, they can access class data members...
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Thanks for the reply. I can access the pointer Variable in side the second function(methods of the same class). But I want to access the contents of this pointer.
Consider the following code:
dtCore::RefPtr<dtCore::Object> m_Obj = new Object("Car");
m_Obj->LoadFile("demoMap/StaticMeshes/Pajero.IVE\0");
This is inside the first function. Here I've initialized the m_Obj variable to something. When I access this variable inside the second function it contains NULL value why because I've initialized it in the Constructor to NULL since it's a base member. But I 've to access what I've initalize in the first function from the second function not the one in the Constructor. How to do?
|
|
|
|
|
Probably I didn't get you. Anyway the following simple snippet possibly may help.
class A
{
int * _p;
public:
A(){ _p = NULL;}
void fun1() { _p = new int; *_p = 10; }
void fun2()
{
if (_p)
printf("%d\n", *_p);
else
printf("_p is not initialized\n");
}
~A(){if (_p) delete _p;}
};
void main()
{
A a;
a.fun2();
a.fun1();
a.fun2();
}
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
why don't you use a member variable within a class. This way you can use it across the class
Somethings seem HARD to do, until we know how to do them.
_AnShUmAn_
|
|
|
|