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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Strange memory allocation

Strange memory allocation

Scheduled Pinned Locked Moved C / C++ / MFC
algorithmsdata-structuresperformancehelpquestion
7 Posts 5 Posters 1 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.
  • B Offline
    B Offline
    boon kian
    wrote on last edited by
    #1

    Hi, I am a newbie in visual c and my "hello world" program in vc is in 2002 !! I desperately needed some help from big bro in vc regarding memory allocation. I have created a algorithm using extensive recusive functions. I needed 3 dynamic array inside my functions and so i happily used the "new" operator to help me. ( the array size is ard 100 integers) To my dismay :omg: , the array address was allocated so close together that changing some elements in one array will affect my the other array !! I am at a loss of wat to do to avoid this (and in the first place why this happen !!) Can any kind soul explain to me why such thing happen and what alternative do i have ?? Thank you. A desperate newbie X|

    C A T M 4 Replies Last reply
    0
    • B boon kian

      Hi, I am a newbie in visual c and my "hello world" program in vc is in 2002 !! I desperately needed some help from big bro in vc regarding memory allocation. I have created a algorithm using extensive recusive functions. I needed 3 dynamic array inside my functions and so i happily used the "new" operator to help me. ( the array size is ard 100 integers) To my dismay :omg: , the array address was allocated so close together that changing some elements in one array will affect my the other array !! I am at a loss of wat to do to avoid this (and in the first place why this happen !!) Can any kind soul explain to me why such thing happen and what alternative do i have ?? Thank you. A desperate newbie X|

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      can you show us the memory allocation code you're using?


      A man is like a rusty wheel on a rusty cart, He sings his song as he rattles along and then he falls apart. -- Richard Thompson

      image effects!

      B 1 Reply Last reply
      0
      • B boon kian

        Hi, I am a newbie in visual c and my "hello world" program in vc is in 2002 !! I desperately needed some help from big bro in vc regarding memory allocation. I have created a algorithm using extensive recusive functions. I needed 3 dynamic array inside my functions and so i happily used the "new" operator to help me. ( the array size is ard 100 integers) To my dismay :omg: , the array address was allocated so close together that changing some elements in one array will affect my the other array !! I am at a loss of wat to do to avoid this (and in the first place why this happen !!) Can any kind soul explain to me why such thing happen and what alternative do i have ?? Thank you. A desperate newbie X|

        A Offline
        A Offline
        Anonymous
        wrote on last edited by
        #3

        The allocation of memory side by side, is a natural phenomenon. This is something the compiler and the run time system would handle. I dunno if you can change it. I dont think it is possible . And how does the allocation of the memory affect your code. IMO you should try and change it in a way that it is not affected by memory allocation structure. HTH

        1 Reply Last reply
        0
        • B boon kian

          Hi, I am a newbie in visual c and my "hello world" program in vc is in 2002 !! I desperately needed some help from big bro in vc regarding memory allocation. I have created a algorithm using extensive recusive functions. I needed 3 dynamic array inside my functions and so i happily used the "new" operator to help me. ( the array size is ard 100 integers) To my dismay :omg: , the array address was allocated so close together that changing some elements in one array will affect my the other array !! I am at a loss of wat to do to avoid this (and in the first place why this happen !!) Can any kind soul explain to me why such thing happen and what alternative do i have ?? Thank you. A desperate newbie X|

          T Offline
          T Offline
          Todd Smith
          wrote on last edited by
          #4

          It sounds like your using a pointer incorrectly. If you paste some code I'm sure we can pin-point the problem. Otherwise it's to hard to diagnose. Todd Smith

          1 Reply Last reply
          0
          • B boon kian

            Hi, I am a newbie in visual c and my "hello world" program in vc is in 2002 !! I desperately needed some help from big bro in vc regarding memory allocation. I have created a algorithm using extensive recusive functions. I needed 3 dynamic array inside my functions and so i happily used the "new" operator to help me. ( the array size is ard 100 integers) To my dismay :omg: , the array address was allocated so close together that changing some elements in one array will affect my the other array !! I am at a loss of wat to do to avoid this (and in the first place why this happen !!) Can any kind soul explain to me why such thing happen and what alternative do i have ?? Thank you. A desperate newbie X|

            M Offline
            M Offline
            markkuk
            wrote on last edited by
            #5

            Did you remember that if you allocate an array with 100 elements, the indexes will be 0 - 99 ?

            B 1 Reply Last reply
            0
            • C Chris Losinger

              can you show us the memory allocation code you're using?


              A man is like a rusty wheel on a rusty cart, He sings his song as he rattles along and then he falls apart. -- Richard Thompson

              image effects!

              B Offline
              B Offline
              boon kian
              wrote on last edited by
              #6

              Hi all, really thanx a lot for you help !! my code goes like this: int *header, *data ; bool *checkarray; int *connectcomponent; width = height = 9; connectcomponent = new int [width*height/9]; checkarray = new bool [width*height]; data = abc.getBitmapData (); // getBitmapData returns a pointer to a int array which is the same size as checkarray; I dun see anything wrong but when i run the program... so of the values in data was changed even though i did not issue any command to do so. when I check the address of checkarray and data, i found out that there is only a diff by 0x30 !! bk

              1 Reply Last reply
              0
              • M markkuk

                Did you remember that if you allocate an array with 100 elements, the indexes will be 0 - 99 ?

                B Offline
                B Offline
                boon kian
                wrote on last edited by
                #7

                yes i do .. anyway to add to my previous note .. I did not change the values of array that *data point to but i constantly change the value of the array that *checkarray point to .. however when i print out the values of array that *data point to .. those values at the begining of the array are changed !! Thanx a lot for your help bk

                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