Access Violation ....Very Interesting
-
This Code gives output as Access violation ...Can anybody tell me what is wrong there.... #include #include void main() { char * szTemp, *cmdline; cmdline = (char *)malloc(sizeof (char) * 1000); if(cmdline) { strcpy(cmdline, "skdjofhdsjfodhfjdhgkjfhgkjdfhgkfhgkjdfhgk"); int len = strlen(cmdline); szTemp = (char *)malloc(sizeof (len) + 1); if(szTemp) { strcpy(szTemp, cmdline); puts(szTemp); } } if(cmdline) { free(cmdline); cmdline = NULL; } if(szTemp) { //Access Violation When We Free szTemp free(szTemp); szTemp = NULL; } return; }
-
This Code gives output as Access violation ...Can anybody tell me what is wrong there.... #include #include void main() { char * szTemp, *cmdline; cmdline = (char *)malloc(sizeof (char) * 1000); if(cmdline) { strcpy(cmdline, "skdjofhdsjfodhfjdhgkjfhgkjdfhgkfhgkjdfhgk"); int len = strlen(cmdline); szTemp = (char *)malloc(sizeof (len) + 1); if(szTemp) { strcpy(szTemp, cmdline); puts(szTemp); } } if(cmdline) { free(cmdline); cmdline = NULL; } if(szTemp) { //Access Violation When We Free szTemp free(szTemp); szTemp = NULL; } return; }
Oh, yes, it is veeeeery interesting. :-D BTW: The error is in the line below, can you spot it?
Aabid wrote:
szTemp = (char *)malloc(sizeof (len) + 1);
Hint: sizeof(len) is 4! :)
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] -
This Code gives output as Access violation ...Can anybody tell me what is wrong there.... #include #include void main() { char * szTemp, *cmdline; cmdline = (char *)malloc(sizeof (char) * 1000); if(cmdline) { strcpy(cmdline, "skdjofhdsjfodhfjdhgkjfhgkjdfhgkfhgkjdfhgk"); int len = strlen(cmdline); szTemp = (char *)malloc(sizeof (len) + 1); if(szTemp) { strcpy(szTemp, cmdline); puts(szTemp); } } if(cmdline) { free(cmdline); cmdline = NULL; } if(szTemp) { //Access Violation When We Free szTemp free(szTemp); szTemp = NULL; } return; }
Aabid wrote:
szTemp = (char *)malloc(sizeof (len) + 1);
I believe it should be (char *)malloc(sizeof (char)* len + 1);. actually you need to have buffer copy commandline into szTemp. but instead of allocating proper buffer to it, you are allocating on buffer for 4 character + 1 for NULL.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
Never mind - my own stupidity is the source of every "problem" - Mixturecheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You
-
This Code gives output as Access violation ...Can anybody tell me what is wrong there.... #include #include void main() { char * szTemp, *cmdline; cmdline = (char *)malloc(sizeof (char) * 1000); if(cmdline) { strcpy(cmdline, "skdjofhdsjfodhfjdhgkjfhgkjdfhgkfhgkjdfhgk"); int len = strlen(cmdline); szTemp = (char *)malloc(sizeof (len) + 1); if(szTemp) { strcpy(szTemp, cmdline); puts(szTemp); } } if(cmdline) { free(cmdline); cmdline = NULL; } if(szTemp) { //Access Violation When We Free szTemp free(szTemp); szTemp = NULL; } return; }
Please check the statement szTemp = (char *)malloc(sizeof (len) + 1); check with following szTemp = (char *)malloc(len + 1);
-
Please check the statement szTemp = (char *)malloc(sizeof (len) + 1); check with following szTemp = (char *)malloc(len + 1);
sorry I was late.. Please ingnoe
-
Aabid wrote:
szTemp = (char *)malloc(sizeof (len) + 1);
I believe it should be (char *)malloc(sizeof (char)* len + 1);. actually you need to have buffer copy commandline into szTemp. but instead of allocating proper buffer to it, you are allocating on buffer for 4 character + 1 for NULL.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
Never mind - my own stupidity is the source of every "problem" - Mixturecheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You
ThatsAlok believe it should be (char *)malloc(sizeof (char)* len + 1);.
That should be
sizeof(char) * (len+1)
if
sizeof(char)
was indeed relevant... :rolleyes: Pardon my nitpick attitude. :-DIf 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] -
ThatsAlok believe it should be (char *)malloc(sizeof (char)* len + 1);.
That should be
sizeof(char) * (len+1)
if
sizeof(char)
was indeed relevant... :rolleyes: Pardon my nitpick attitude. :-DIf 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]CPallini wrote:
if sizeof(char) was indeed relevant...
Yeap i know.. Old programming bad-habit :-)
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
Never mind - my own stupidity is the source of every "problem" - Mixturecheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You
-
CPallini wrote:
if sizeof(char) was indeed relevant...
Yeap i know.. Old programming bad-habit :-)
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
Never mind - my own stupidity is the source of every "problem" - Mixturecheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You
Actually it is a good habit and proves helpful when dealing with
TCHAR
s. :-DIf 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]