简体   繁体   中英

How to make a loop which will create variables in C

I am kinda new to the language and I thought of something stupid but really want to do it. I learn C at the moment and I want to try to make a program which will create variables continue-sly. like:

while(1) {
  //here making variables.
}

My teacher said that it stores the variables in an empty space and since i am not reserving the heap what i want to do is reserve a big amount of variables until i take all the storage and my computer crashes. Is this possible?

If I understand you correctly, you want to dynamically create a named variable during run-time. You can't do that: named variables are created at compile time and that's what you are stuck with.

If you want to create those variables dynamically at compile time, then you'll have to list them in your code. I would pick the type that takes the most bytes to store and create a whole bunch of them. Because you should create them with different names, maybe creating a code generator would be a good way to go: just write a program that writes another program.

But if you only want to expend the memory, you can go other ways: declare an array of a fixed size, and make it the largest size you can with the type that takes most bytes to store.

Finally, this really seems suited for using the heap and continuously alloc memory. Why do you say you can't use it?

Standard C offers no way to create variables within a program, if, by “variable,” we mean a named object, except that you can recursively call functions that have variables, and hence each call will create new instances of variables (with the names fixed at compile time).

To create new objects in C within a program, you must allocate storage dynamically and write code to manipulate them. Objects created in this way do not have names; they merely exist in storage that you must write code to manipulate. (The pointers you use to refer to this storage have names, but no names are created for the storage itself.)

In normal hosted environments (programs running in general-purpose operating systems such as Windows, macOS, Linux, and others), allocating excessive memory will not crash your system. The operating system will not give your program so much memory that the system crashes (unless the system itself is defective).

If it's a simple overflow you are looking for, you could try this.

int main(){
    int* i;

    while(1){
        i = (int*) malloc(sizeof(int));
    }

    return 0;
}

The program will crash but your computer will be fine.

Yes, its called a memory leak. You can create one by defining a pointer variable and reinitializing it without freeing it first. If you want it to fill up space fast create a void* pointer, initialize it with malloc() with a reasonable size parameter in your loop.

However modern operating systems somewhat protect themselves a bit so you need to write data into the allocated space to actually physically allocate it. I´d use memset() for that.

This program then fills up its whole virtual address space. This will initially cause your PC to slow to a crawl as the os needs to page your data onto the harddrive. This will continue until both the memory and your pagefile or swap partition fill up to their limit.

The final result will the depend on the behaviour of the os. If it is smart it might terminate your program, if it isn´t it might crash due to the lack of memory space to allocate for itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM