|
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_
|
|
|
|
|
Hi,
What is the difference b/w strlen(NULL) and strlen(""). strlen("") return 0 whereas strlen(NULL) crashes my program leading memory leak.Could any one explain me please?
Thanks in advance.
|
|
|
|
|
"" returns a valid const char pointer to an empty string while NULL is a invalid pointer.
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]
|
|
|
|
|
and why TF whould you do such a thing like strlen(NULL) ?
if you had looked at the doc before asking, you would see that strlen() gets a const char* parameter. obviously, it has to be valid (that means, it has to point to a valid string). NULL is not a valid pointer, hence the crash.
on the other hand, "" is a valid string (even though empty, but valid), so strlen("") returns the number of character in that empty string : 0 characters.
|
|
|
|
|
Hi all,
I want to make option buttons in my tree control...
How can i do this??
thanks in advance
|
|
|
|
|
hi how to create and run a webserver in VC++(MFC)
|
|
|
|
|
You just posted this same inane query 4 hours back. At first, you post a query that is not in compliance with the guidelines[^], and you can't wait for a day before re-posting 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++]
|
|
|
|
|
There's a link to one on the front page you clown.
A C++ Embedded Web Server[^]
And yeah - I did 1 vote both posts from you asking this question today.
|
|
|
|
|
The source code for the Apache HTTP server[^] shows a pretty good example of a web server implementation
Seriously, what are you asking? Do you want to use someone elses
server or develop your own? To do your own you're going to need
to have a firm grasp on the TCP/IP protocol and the HTTP protocol[^].
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hi everybody,
i like to center a label into a CDialog.
I calculate the left and right position of the label to set the postition via a SetWindowPos.
left gets the value of the calculation and the right value is (left + WidthOfLabel)
In this case the width of the displayed label should be constant at a width of "WidthOfLabel", right?
But in my application the width of the label grows sometimes of one pixel..
If i resize the dialog at a double size, the label has a bigger width than the initial width
Is there a special treatement behind the SetWindowPos which changes the sizes sometimes to fit to the Dialog automatically ?
Big thanks
|
|
|
|
|
baerten wrote: i like to center a label into a CDialog.
You don't like CenterWindow() , do you?
Regards,
Sandip.
|
|
|
|
|
LOL
ehm Okay ... i think your way seems to be easier
But anyway a strange behavour.
Thanks SandipG
|
|
|
|
|
baerten wrote: Is there a special treatement behind the SetWindowPos which changes the sizes sometimes to fit to the Dialog automatically ?
Not that I know of.
How are you calculating WidthOfLabel?
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
i have a structure and BYTE array
typedef struct _my_struct
{
DWORD dwValue ;
BYTE yValue1 ;
BYTE yValue2 ;
WORD wValue1 ;
BYTE yValue3 ;
WORD wValue2 ;
BYTE yValue4 ;
}mystruct ;
BYTE Buf[10] = {"ab--c--d"} ;
i need to copy
a to yValue1,
b to yValue2,
-- to wValue1,
c to yValue3,
-- to wValue2,
and d to yValue1.
i tried the following code but its not working
mystruct st ;
memcpy( &st.yValue1 , Buf , 6 ) ;
is any way to do that ?
Thanks & Regards
|
|
|
|
|
nitin3 wrote: mystruct st ;
memcpy( &st.yValue1 , Buf , 6 ) ;
i
do not use this unless you are sure about the alignment of your structure.
If you are not aware of what is alignment read this Pragma packing[^]
Regards,
Sandip.
|
|
|
|
|
nitin3 wrote: is any way to do that ?
Yes, using the recipe
nitin3 wrote: copy
a to yValue1,
b to yValue2,
-- to wValue1,
c to yValue3,
-- to wValue2,
and d to yValue1.
you proposed.
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]
|
|
|
|