Home
Sunday, November 19, 2017
8:30:53 AM
Users online: 0   You are here >> Home > Programming

Forums | Programming Forums search
Forum FAQ
   
  1  
Changing global variable at runtime
Comrade 
30/8/08 10:25:25 AM
Serf

Hi, I have a 1D array declared outside all the functions, and I want to assign its length at runtime.
How do I go about doing this?
Any help is appreciated, thanks.

-----
Intel C2Q 6600,Asus P5K-pro,4GB DDR2 RAM,AsusG8800GT, WindowsXP Pro, Ubuntu7.10

spielentwickler 
30/8/08 11:41:01 AM
Guru

What language are you using?

-----
http://www.last.fm/user/spielentwickler/

Comrade 
30/8/08 12:14:08 PM
Serf

C

-----
Intel C2Q 6600,Asus P5K-pro,4GB DDR2 RAM,AsusG8800GT, WindowsXP Pro, Ubuntu7.10

kikz 
30/8/08 1:11:54 PM
Immortal

Assign it at the start of main. Assign elsewhere and something might call it before it's initialized.

-----
Q6600@3.2Ghz | 8Gb PC6400 | 2 x 500Gb RAID 0 + 2 x 320 Gb RAID 0 | 19" Benq FP591 + 24" Samsung 245B + 19" Dell | 8800GTS 640 Mb + 8400GS 256 Mb | Gigabyte GA-P35-DS3P | Antec P182 | Corsair HX-620 | Thermalright 120 Extreme | Vista x64

Comrade 
30/8/08 1:29:59 PM
Serf

But the length of the array is fixed when it was declared.

-----
Intel C2Q 6600,Asus P5K-pro,4GB DDR2 RAM,AsusG8800GT, WindowsXP Pro, Ubuntu7.10

freespace 
30/8/08 1:43:32 PM
Hero
Titan


kikz: he wants to make a global array at run time :)

comrade: learn about malloc, pointers, and how to create an array at runtime. Then simply do it in global scope.

-----
By perseverance the snail reached the ark.

http://www.shuningbian.net - blog
http://anonshare.pictorii.com - share files anonymously
http://dailydiscovery.b3ta.org - learn something new
its f reespace damn it!

Meeko 
30/8/08 2:05:21 PM
Titan

Create the pointer, use malloc or calloc when you know the size, make sure you check if it points to NULL everywhere you use it.

-----

spielentwickler 
30/8/08 3:29:15 PM
Guru

malloc will allocate a chunk of memory of any size.

If you know the size of the elements in the array (sizeof(int); for example) you can say:

int * dynamicArray = (int *) malloc(sizeof(int) * numberOfElements);

-----
http://www.last.fm/user/spielentwickler/

kikz 
30/8/08 4:56:40 PM
Immortal

Quote by Comrade
But the length of the array is fixed when it was declared.



Quote by freespace
kikz: he wants to make a global array at run time :)



:S

-----
Q6600@3.2Ghz | 8Gb PC6400 | 2 x 500Gb RAID 0 + 2 x 320 Gb RAID 0 | 19" Benq FP591 + 24" Samsung 245B + 19" Dell | 8800GTS 640 Mb + 8400GS 256 Mb | Gigabyte GA-P35-DS3P | Antec P182 | Corsair HX-620 | Thermalright 120 Extreme | Vista x64

Comrade 
30/8/08 5:08:05 PM
Serf

So I declare a *int or *float in the global scope and malloc in the function when I know the size of the array I want to create, then just fill it up. Is that how it works?

-----
Intel C2Q 6600,Asus P5K-pro,4GB DDR2 RAM,AsusG8800GT, WindowsXP Pro, Ubuntu7.10

Meeko 
30/8/08 9:38:41 PM
Titan

Yes. The malloc/calloc will return a pointer to the first element of the array. You assign this to your global pointer so that your global pointer now points to the first element of the array.

Out of curiosity, this makes me wonder - if we declare an array inside of a block, so it's allocated on the stack, and then the program falls out of that block, the array is returned to the stack, isn't it? But what if we assign it to the global pointer while it's inside the block? After we fall out of the block, the array is still returned to the stack and the global pointer now points at a dangerous bit of memory, doesn't it?

-----

freespace 
30/8/08 11:11:10 PM
Hero
Titan


Quote by Meeko
Out of curiosity, this makes me wonder - if we declare an array inside of a block, so it's allocated on the stack, and then the program falls out of that block, the array is returned to the stack, isn't it? But what if we assign it to the global pointer while it's inside the block? After we fall out of the block, the array is still returned to the stack and the global pointer now points at a dangerous bit of memory, doesn't it?

Yes, this is a mistaken often made by beginners: returning a pointer to memory which is out of scope.

-----
By perseverance the snail reached the ark.

http://www.shuningbian.net - blog
http://anonshare.pictorii.com - share files anonymously
http://dailydiscovery.b3ta.org - learn something new
its f reespace damn it!

freespace 
30/8/08 11:15:10 PM
Hero
Titan


Quote by spielentwickler
int * dynamicArray = (int *) malloc(sizeof(int) * numberOfElements);

Don't cast malloc's return value, there is no need and it is potentially dangerous.

See http://c-faq.com/malloc/mallocnocast.html

-----
By perseverance the snail reached the ark.

http://www.shuningbian.net - blog
http://anonshare.pictorii.com - share files anonymously
http://dailydiscovery.b3ta.org - learn something new
its f reespace damn it!

  1  
Forums | Programming