defining array size at runtime

This is a discussion on defining array size at runtime within the C Programming forums, part of the General Programming Boards category; Hi, I've read in several places now that you can't define an array size at runtime - you need to ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    5

    defining array size at runtime

    Hi, I've read in several places now that you can't define an array size at runtime - you need to use pointers and malloc/free to dynamically allocate arrays. The thing is I found this after I'd written a program that does just this, & works fine
    i.e. declaring integer variables, reading those variables from a configuration file and defining both 1 and 2d array sizes with those variables. What gives? Why's it working? is this a platform/compiler specific feature that isn't ANSI, and is there any reason to change the program & use pointers to dynamically allocate the array size, as I said it (surprisingly for me ) works well as it is. Any insight gratefully received

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    can u show some code?
    what compiler?
    I think some compilers do allow you to do this but most do not allow it.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789

    Re: defining array size at runtime

    Originally posted by Sanglier
    Hi, I've read in several places now that you can't define an array size at runtime - you need to use pointers and malloc/free to dynamically allocate arrays. The thing is I found this after I'd written a program that does just this, & works fine
    i.e. declaring integer variables, reading those variables from a configuration file and defining both 1 and 2d array sizes with those variables. What gives? Why's it working? is this a platform/compiler specific feature that isn't ANSI, and is there any reason to change the program & use pointers to dynamically allocate the array size, as I said it (surprisingly for me ) works well as it is. Any insight gratefully received
    Please port your code...more than likely if it is working you are running over in memory...and that will normally lead to a crash, ot unpredicted results...even then, your compiler should tell you.

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    5
    OK, Thanks, I'll try to cut it down to the essentials tomorrow and post it - it's over 1000 lines in all & I'm sure you don't want to see all that ..... I take it this is a bad thing to do then
    I'm not getting any compiler errors & it does work with different array sizes (I've used upto: double arrayname[200][10000]). Compiler is Mingw with Bloodshed-DevC++ 4
    Thanks & I'll get back to you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 08:25 PM
  2. How do you use variable to initialize array size?
    By yougene in forum C Programming
    Replies: 11
    Last Post: 09-04-2007, 03:50 PM
  3. Invalid conversion from 'void*' to 'BYTE' help
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-22-2006, 11:27 AM
  4. Array size
    By tigrfire in forum C Programming
    Replies: 5
    Last Post: 11-14-2005, 08:45 PM
  5. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-24-2004, 12:22 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21