Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Access Violation ....Very Interesting

Access Violation ....Very Interesting

Scheduled Pinned Locked Moved C / C++ / MFC
question
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Aabid
    wrote on last edited by
    #1

    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; }

    CPalliniC T R 3 Replies Last reply
    0
    • A Aabid

      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; }

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      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]

      In testa che avete, signor di Ceprano?

      1 Reply Last reply
      0
      • A Aabid

        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; }

        T Offline
        T Offline
        ThatsAlok
        wrote on last edited by
        #3

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

        cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You

        CPalliniC 1 Reply Last reply
        0
        • A Aabid

          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; }

          R Offline
          R Offline
          Radhakrishnan G
          wrote on last edited by
          #4

          Please check the statement szTemp = (char *)malloc(sizeof (len) + 1); check with following szTemp = (char *)malloc(len + 1);

          R 1 Reply Last reply
          0
          • R Radhakrishnan G

            Please check the statement szTemp = (char *)malloc(sizeof (len) + 1); check with following szTemp = (char *)malloc(len + 1);

            R Offline
            R Offline
            Radhakrishnan G
            wrote on last edited by
            #5

            sorry I was late.. Please ingnoe

            1 Reply Last reply
            0
            • T ThatsAlok

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

              cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You

              CPalliniC Offline
              CPalliniC Offline
              CPallini
              wrote on last edited by
              #6

              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. :-D

              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]

              In testa che avete, signor di Ceprano?

              T 1 Reply Last reply
              0
              • CPalliniC CPallini

                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. :-D

                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]

                T Offline
                T Offline
                ThatsAlok
                wrote on last edited by
                #7

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

                cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You

                CPalliniC 1 Reply Last reply
                0
                • T ThatsAlok

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

                  cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You

                  CPalliniC Offline
                  CPalliniC Offline
                  CPallini
                  wrote on last edited by
                  #8

                  Actually it is a good habit and proves helpful when dealing with TCHARs. :-D

                  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]

                  In testa che avete, signor di Ceprano?

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups