global variables and dynamic allocation


 
Thread Tools Search this Thread
Top Forums Programming global variables and dynamic allocation
# 1  
Old 04-09-2009
global variables and dynamic allocation

Hi,
is it possible in C to allocate dynamically a global variable??
# 2  
Old 04-09-2009
Depends on your meaning. You can't suddenly decide for a global integer named 'bill' to start existing when you never declared one at compile-time. For that matter, any variable of any sort in C must be declared at compile-time. But you can make a global pointer and allocate memory for it to point to later.
# 3  
Old 04-09-2009
ok, can i for example declare a pointer to integer and allocate memory to it inside any function? how can i deallocate memory?
# 4  
Old 04-09-2009
Quote:
Originally Posted by littleboyblu
ok, can i for example declare a pointer to integer and allocate memory to it inside any function? how can i deallocate memory?
Yes you can allocate memory inside any function and deallocate it with free...though sample code would help understand what you are trying to do.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Global variables in perl

hi all, i need a help for the following query. Thanks in advance for your valuable time. i have a main.pl file which has a global variable declared as below. our myVar=0; call first.pl script from the main.pl script. print the value of myVar (the value is still 0 and not 10.) i have a... (1 Reply)
Discussion started by: hemalathak10
1 Replies

2. Programming

Dynamic Memory Allocation

Hello Guys I have a small confusion in the dynamic memory allocation concept. If we declare a pointer say a char pointer, we need to allocate adequate memory space. char* str = (char*)malloc(20*sizeof(char)); str = "This is a string"; But this will also work. char* str = "This... (2 Replies)
Discussion started by: tene
2 Replies

3. Solaris

How to access ENV variables of non global zones in global zone???

Hi Guys, My requirement is I have file called /opt/orahome/.profile in non global zone. PATH=/usr/bin:/usr/ucb:/etc:/usr/sbin:/usr/local/bin:/usr/openwin/bin:. export PATH PS1="\${ORACLE_SID}:`hostname`:\$PWD$ " export PS1 EDITOR=vi export EDITOR ENV=/opt/orahome/.kshrc export ENV... (1 Reply)
Discussion started by: vijaysachin
1 Replies

4. Programming

dynamic allocation vs static allocation in c

i wrote a tiny version of tail command using a large buffer statically allocated but, in a second time, i found another version in which i use a bidimensional array dynamically allocated. here is the first version /*my tiny tail, it prints the last 5 line of a file */ #include<stdio.h>... (4 Replies)
Discussion started by: lucasclaus
4 Replies

5. Programming

array dynamic allocation

Hi, I have the following problem: i must allocate a dynamic array from a subroutine which should return such array to main function. The subroutine has already a return parameter so i thought of pass the array as I/O parameter. I tried the following program but it doesn't work (segmentation... (11 Replies)
Discussion started by: littleboyblu
11 Replies

6. UNIX for Dummies Questions & Answers

global variables

Hi, I hav created a script that calls a sub-script. In both the scripts i called the configuration file. Now i wanted to use a variable that should be used in both script and sub-script files. Actually, i wanted to return a file name and the return status to the script file from the sub-script.... (6 Replies)
Discussion started by: Swapna173
6 Replies

7. Programming

Dynamic memory allocation

Hi, I am trying to process line by line of a file. But I should not be allocating static allocation for reading the contents of the file. The memory should be dynamically allocated. The confusion here is how do I determine the size of each line, put it into a buffer with the memory allocated... (11 Replies)
Discussion started by: naan
11 Replies

8. Shell Programming and Scripting

dynamic global script

Hi, I have to create a global dynamic script which should ask for the env or some other variables and then create the soft links. let's say that I have to create ten soft links and the path for these soft links is different for each env for e.g: WDEV: /d02/app/applmgr/wdev/appl/CDCRM/bin... (2 Replies)
Discussion started by: isingh786
2 Replies

9. Shell Programming and Scripting

perl global variables

Can someone give me "the lecture" on why you shouldn't make all your varables global when programming in perl. I have been doing this but I have heard that it is not a good practice. (3 Replies)
Discussion started by: reggiej
3 Replies

10. Programming

Static variables memory allocation

Hi I want to know when and where memory for static variables are allocated in a C program. If it allocates during compilation will memory be allocated for the variable "i" during compilation itself. int count(); int main(){ printf("%d", count()); return 0; } int count() { ... (8 Replies)
Discussion started by: nathanmca
8 Replies
Login or Register to Ask a Question