0

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?

Nick Gr
  • 105
  • 10
  • 1
    It used to be possible, but not any more. Nowadays, all you can do is crash your own program. The OS will protect itself. – user3386109 Mar 22 '18 at 21:27
  • 1
    Call `malloc()` in the loop, eventually you'll run out of memory. But since modern operating systems use virtual memory, you just run out memory in that process, not the whole computer. – Barmar Mar 22 '18 at 21:31
  • @user3386109 could you give an example of it. –  Mar 22 '18 at 21:32
  • 1
    "creating variables" != "allocating space", so it's unclear what exactly you are talking about. – vgru Mar 22 '18 at 21:37
  • You can have as many variables of the same name as memory permits, with different values, if you create a recursive function with a local variable of that name. The kind of memory used for this is described as "stack", though C standard does not actually define something like that. The kind of memory described as "heap" can be used like described by the answers mentioning "malloc()". Crashing the computer that way is the second level goal and probably not possible anymore, as already mentioned in comments and answers. – Yunnosch Mar 22 '18 at 21:40
  • C is a compiled language. Every "variable" in the language is created at compile time--you cannot create them as the program runs. You can allocate memory at runtime, but you'll have to refer to that memory using the same variables your program started with. – Lee Daniel Crocker Mar 22 '18 at 22:12
  • @LeeDanielCrocker you can actually create variables during runtime because some scopes are created during runtime, eg. function automatic variables. – Ajay Brahmakshatriya Mar 22 '18 at 23:10
  • I wouldn't consider that creating a variable (i.e. a named value), that's allocating memory and re-assigning an existing compile-time-declared variable to the new location (and then back when the stack unwinds). – Lee Daniel Crocker Mar 23 '18 at 00:59

4 Answers4

0

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?

Alex Pinto
  • 162
  • 1
  • 7
0

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).

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
0

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.

Witnessthis
  • 136
  • 4
0

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.

Nefrin
  • 338
  • 3
  • 11