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. Why does this simple dynamic array experiment crash?

Why does this simple dynamic array experiment crash?

Scheduled Pinned Locked Moved C / C++ / MFC
questioncssdata-structuresperformance
7 Posts 3 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.
  • C Offline
    C Offline
    Cristoff
    wrote on last edited by
    #1

    Any ideas why this code crashes with 799 elements or more but it works with 699 or less? I haven't found yet the limit.

    #include #include int main() { /*1000 elements for the array*/ int n = 699; /* Dynamic array */ int* array = (int*) malloc(sizeof(char) * n); if (!array) { printf("Out of memory"); return 1; } for (int i = 0; i < n; i++) { array[i] = i; } for (int i = 0; i < n; i++) { printf("%d, ", array[i]); } return 0; }

    -- modified at 2:51 Thursday 30th March, 2006

    R T 2 Replies Last reply
    0
    • C Cristoff

      Any ideas why this code crashes with 799 elements or more but it works with 699 or less? I haven't found yet the limit.

      #include #include int main() { /*1000 elements for the array*/ int n = 699; /* Dynamic array */ int* array = (int*) malloc(sizeof(char) * n); if (!array) { printf("Out of memory"); return 1; } for (int i = 0; i < n; i++) { array[i] = i; } for (int i = 0; i < n; i++) { printf("%d, ", array[i]); } return 0; }

      -- modified at 2:51 Thursday 30th March, 2006

      R Offline
      R Offline
      RockyJames
      wrote on last edited by
      #2

      Hey when i tried it is working up to n=980. anything more than 980 application is crashing.

      C 1 Reply Last reply
      0
      • R RockyJames

        Hey when i tried it is working up to n=980. anything more than 980 application is crashing.

        C Offline
        C Offline
        Cristoff
        wrote on last edited by
        #3

        What compiler? I'm using mingw on WinXP Home right now but I could try it later on VS2005 and GCC under Linux but I have to reboot. This is a text book example. Well I've change it a little bit but it is basically the same.

        1 Reply Last reply
        0
        • C Cristoff

          Any ideas why this code crashes with 799 elements or more but it works with 699 or less? I haven't found yet the limit.

          #include #include int main() { /*1000 elements for the array*/ int n = 699; /* Dynamic array */ int* array = (int*) malloc(sizeof(char) * n); if (!array) { printf("Out of memory"); return 1; } for (int i = 0; i < n; i++) { array[i] = i; } for (int i = 0; i < n; i++) { printf("%d, ", array[i]); } return 0; }

          -- modified at 2:51 Thursday 30th March, 2006

          T Offline
          T Offline
          Taka Muraoka
          wrote on last edited by
          #4

          Your array variable is a pointer to int's but you're only allocating enough memory for n char's. char's are smaller than int's so you're running past the end of the memory you've allocated.


          0 bottles of beer on the wall, 0 bottles of beer, you take 1 down, pass it around, 4294967295 bottles of beer on the wall. Awasu 2.2 [^]: A free RSS/Atom feed reader with support for Code Project.

          C 2 Replies Last reply
          0
          • T Taka Muraoka

            Your array variable is a pointer to int's but you're only allocating enough memory for n char's. char's are smaller than int's so you're running past the end of the memory you've allocated.


            0 bottles of beer on the wall, 0 bottles of beer, you take 1 down, pass it around, 4294967295 bottles of beer on the wall. Awasu 2.2 [^]: A free RSS/Atom feed reader with support for Code Project.

            C Offline
            C Offline
            Cristoff
            wrote on last edited by
            #5

            :) damn't it :) sorry for the stupid question!

            1 Reply Last reply
            0
            • T Taka Muraoka

              Your array variable is a pointer to int's but you're only allocating enough memory for n char's. char's are smaller than int's so you're running past the end of the memory you've allocated.


              0 bottles of beer on the wall, 0 bottles of beer, you take 1 down, pass it around, 4294967295 bottles of beer on the wall. Awasu 2.2 [^]: A free RSS/Atom feed reader with support for Code Project.

              C Offline
              C Offline
              Cristoff
              wrote on last edited by
              #6

              :) Damn't it :) Sorry for the stupid question! I'm ashamed!

              T 1 Reply Last reply
              0
              • C Cristoff

                :) Damn't it :) Sorry for the stupid question! I'm ashamed!

                T Offline
                T Offline
                Taka Muraoka
                wrote on last edited by
                #7

                Cristoff wrote:

                orry for the stupid question!

                Hey, we've all done it. Just be grateful you did here instead of when iterating through thousands of bank account balances :-)


                0 bottles of beer on the wall, 0 bottles of beer, you take 1 down, pass it around, 4294967295 bottles of beer on the wall. Awasu 2.2 [^]: A free RSS/Atom feed reader with support for Code Project.

                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