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. Iam getting memory access violation error in VC

Iam getting memory access violation error in VC

Scheduled Pinned Locked Moved C / C++ / MFC
helpperformancequestion
16 Posts 7 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.
  • H Offline
    H Offline
    harshadha
    wrote on last edited by
    #1

    Hai, can anyone tell me what is the real problem in implementing malloc here bmpsrc = (U8 *) malloc(bmpsize); memset(bmpsrc, 0xff, 5); Thanks Harshadha

    B C J J P 6 Replies Last reply
    0
    • H harshadha

      Hai, can anyone tell me what is the real problem in implementing malloc here bmpsrc = (U8 *) malloc(bmpsize); memset(bmpsrc, 0xff, 5); Thanks Harshadha

      B Offline
      B Offline
      benjymous
      wrote on last edited by
      #2

      You should probably check that bmpsrc != NULL - your memory allocation could fail if there's not enough memory (or if there isn't a single chunk of memory big enough for the allocation)

      H 1 Reply Last reply
      0
      • H harshadha

        Hai, can anyone tell me what is the real problem in implementing malloc here bmpsrc = (U8 *) malloc(bmpsize); memset(bmpsrc, 0xff, 5); Thanks Harshadha

        C Offline
        C Offline
        Cedric Moonen
        wrote on last edited by
        #3

        What is the size of bmpsize ? If it is under 5, then you are writing outside the boundaries of your allocated area. BTW, do you really want to code in C ? Why not use new instead of malloc ?


        Cédric Moonen Software developer
        Charting control [v1.1]

        1 Reply Last reply
        0
        • H harshadha

          Hai, can anyone tell me what is the real problem in implementing malloc here bmpsrc = (U8 *) malloc(bmpsize); memset(bmpsrc, 0xff, 5); Thanks Harshadha

          J Offline
          J Offline
          joelgarabedian
          wrote on last edited by
          #4

          It's hard to know for sure without knowing what bmpsize is, but I would guess that bmpsize is less than 5. When you call memset, you are setting the five bytes starting at the address of bmpsrc to 0xff. If you have allocated less than five bytes, you will be writing to memory you do not own :)

          1 Reply Last reply
          0
          • H harshadha

            Hai, can anyone tell me what is the real problem in implementing malloc here bmpsrc = (U8 *) malloc(bmpsize); memset(bmpsrc, 0xff, 5); Thanks Harshadha

            J Offline
            J Offline
            jhwurmbach
            wrote on last edited by
            #5

            harshadha wrote:

            bmpsrc = (U8 *) malloc(bmpsize); memset(bmpsrc, 0xff, 5);

            Did you check that bmpsrc was not NULL? bmpsize is checked for being greater than 5? maybe memset(bmpsrc, 0xff, max( 5, bmpsize)) would be better? Other than that, the code snippet looks fine.


            "We trained hard, but it seemed that every time we were beginning to form up into teams we would be reorganised. I was to learn later in life that we tend to meet any new situation by reorganising: and a wonderful method it can be for creating the illusion of progress, while producing confusion, inefficiency and demoralisation." -- Caius Petronius, Roman Consul, 66 A.D.

            1 Reply Last reply
            0
            • H harshadha

              Hai, can anyone tell me what is the real problem in implementing malloc here bmpsrc = (U8 *) malloc(bmpsize); memset(bmpsrc, 0xff, 5); Thanks Harshadha

              P Offline
              P Offline
              prasad_som
              wrote on last edited by
              #6

              What is U8 ? What is value of bmpsize ?

              Prasad Notifier using ATL | Operator new[],delete[][^]

              H 1 Reply Last reply
              0
              • H harshadha

                Hai, can anyone tell me what is the real problem in implementing malloc here bmpsrc = (U8 *) malloc(bmpsize); memset(bmpsrc, 0xff, 5); Thanks Harshadha

                K Offline
                K Offline
                kakan
                wrote on last edited by
                #7

                What does your debugger tell you?

                Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson

                H 1 Reply Last reply
                0
                • B benjymous

                  You should probably check that bmpsrc != NULL - your memory allocation could fail if there's not enough memory (or if there isn't a single chunk of memory big enough for the allocation)

                  H Offline
                  H Offline
                  harshadha
                  wrote on last edited by
                  #8

                  void example() { U32 bmpsize; //this is an integer datatype U8 *bmpsrc=0x00FC0; //this is the character datatype //i want to use malloc only ..... bmpsrc =malloc(bmpsize); memset(bmpsrc, 0x00FC0, 0); .. .. .. } this is my actual coding ..... i dont know how to change its memory allocation Harshadha

                  B 1 Reply Last reply
                  0
                  • K kakan

                    What does your debugger tell you?

                    Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson

                    H Offline
                    H Offline
                    harshadha
                    wrote on last edited by
                    #9

                    The instruction at "0x006aoef8" referenced memory at "ox00000000".The memory could not be read.

                    K 1 Reply Last reply
                    0
                    • P prasad_som

                      What is U8 ? What is value of bmpsize ?

                      Prasad Notifier using ATL | Operator new[],delete[][^]

                      H Offline
                      H Offline
                      harshadha
                      wrote on last edited by
                      #10

                      void example() { U32 bmpsize; //this is an integer datatype U8 *bmpsrc=0x00FC0; //this is the character datatype //i want to use malloc only ..... bmpsrc =malloc(bmpsize); memset(bmpsrc, 0x00FC0, 0); .. .. .. } this is my actual coding ..... i dont know how to change its memory allocation

                      P 1 Reply Last reply
                      0
                      • H harshadha

                        void example() { U32 bmpsize; //this is an integer datatype U8 *bmpsrc=0x00FC0; //this is the character datatype //i want to use malloc only ..... bmpsrc =malloc(bmpsize); memset(bmpsrc, 0x00FC0, 0); .. .. .. } this is my actual coding ..... i dont know how to change its memory allocation

                        P Offline
                        P Offline
                        prasad_som
                        wrote on last edited by
                        #11

                        harshadha wrote:

                        bmpsrc =malloc(bmpsize);

                        What is value of bmpsize ?

                        harshadha wrote:

                        memset(bmpsrc, 0x00FC0, 0);

                        Why you are passing last parameter as 0, it should be less than or equal to bmpsize.

                        Prasad Notifier using ATL | Operator new[],delete[][^]

                        H 1 Reply Last reply
                        0
                        • H harshadha

                          The instruction at "0x006aoef8" referenced memory at "ox00000000".The memory could not be read.

                          K Offline
                          K Offline
                          kakan
                          wrote on last edited by
                          #12

                          That's not a text from your debugger. That's Windows error message. By using the debugger you have, you can set a break point in your source code. When the program reaches the break point, the debugger will stop the execution of youe program so you can (amongst other things) inspect your variables and find out the cause of the problem. You should learn how to use the debugger. It's not very hard to do and the debugger will definately save you heaps of time when you are looging for problems with your code.

                          Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson

                          H 1 Reply Last reply
                          0
                          • K kakan

                            That's not a text from your debugger. That's Windows error message. By using the debugger you have, you can set a break point in your source code. When the program reaches the break point, the debugger will stop the execution of youe program so you can (amongst other things) inspect your variables and find out the cause of the problem. You should learn how to use the debugger. It's not very hard to do and the debugger will definately save you heaps of time when you are looging for problems with your code.

                            Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson

                            H Offline
                            H Offline
                            harshadha
                            wrote on last edited by
                            #13

                            unhandled exception in fa.exe:cx0000005 access violation

                            1 Reply Last reply
                            0
                            • P prasad_som

                              harshadha wrote:

                              bmpsrc =malloc(bmpsize);

                              What is value of bmpsize ?

                              harshadha wrote:

                              memset(bmpsrc, 0x00FC0, 0);

                              Why you are passing last parameter as 0, it should be less than or equal to bmpsize.

                              Prasad Notifier using ATL | Operator new[],delete[][^]

                              H Offline
                              H Offline
                              harshadha
                              wrote on last edited by
                              #14

                              the bmpsize value is taken equal to the data type value(U32 bmpsize that is),it is taken as 2bytes. My real problem is i want to create a buffer to draw a bmp file... for that i have to use malloc then i need bmpsrc that is the memory allocated area and the size.....

                              P 1 Reply Last reply
                              0
                              • H harshadha

                                the bmpsize value is taken equal to the data type value(U32 bmpsize that is),it is taken as 2bytes. My real problem is i want to create a buffer to draw a bmp file... for that i have to use malloc then i need bmpsrc that is the memory allocated area and the size.....

                                P Offline
                                P Offline
                                prasad_som
                                wrote on last edited by
                                #15

                                harshadha wrote:

                                the bmpsize value is taken equal to the data type value(U32 bmpsize that is),it is taken as 2bytes.

                                In this case, third parameter to memset in your OP should be bmpsize. Where as in your OP, you was passing 5, that was causing problem.

                                harshadha wrote:

                                i have to use malloc then i need bmpsrc that is the memory allocated area and the size.....

                                May be you can be little clear, what you are trying to say.

                                Prasad Notifier using ATL | Operator new[],delete[][^]

                                1 Reply Last reply
                                0
                                • H harshadha

                                  void example() { U32 bmpsize; //this is an integer datatype U8 *bmpsrc=0x00FC0; //this is the character datatype //i want to use malloc only ..... bmpsrc =malloc(bmpsize); memset(bmpsrc, 0x00FC0, 0); .. .. .. } this is my actual coding ..... i dont know how to change its memory allocation Harshadha

                                  B Offline
                                  B Offline
                                  benjymous
                                  wrote on last edited by
                                  #16

                                  I think you've got your initial variable setup the wrong way round. bmpsize doesn't seem to have a value, and you're setting the value of your pointer to 0x00fc0, which I guess is meant to be the actual size. If that's the case, you've also got the second and third arguments of memset the wrong way around. I hope you'll remember to free that pointer when you're done with it.

                                  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