Dynamic memory allocation in stack

Am I wrong

Page 1 of 1

14 Replies - 899 Views - Last Post: 25 August 2009 - 02:54 PM Rate Topic: -----

#1 debjit625  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 51
  • View blog
  • Posts: 446
  • Joined: 06-September 08

Dynamic memory allocation in stack

Post icon  Posted 25 August 2009 - 12:23 PM

Ok here i want you comments on this
See this C code is this correct
#include <stdio.h>

int main(void)
{
   int i=0;
   int a=0;
   printf_s("anything");
   scanf_s("%i",&i);
   int myarray[i]; 
   int b = 0;
   return 0;
}


According to Neumann & LaFayette (members of DIC)it can be done in C
this is the topic where all this started
http://www.dreaminco...h...pid=746242&
also Neumann says that he can increase the size of stack at runtime
by dynamic memory allocation on stack
This is what he said "Dynamic allocation of arrays on the stack has been a part of C standard for a very long time."

According to me it cant work as it have variables on fly and leave that look at the declaration of myarray

Please give a comment on what you think
Thanks
I know Neumann & LaFayette you will comment on this also.

Is This A Good Question/Topic? 0
  • +

Replies To: Dynamic memory allocation in stack

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Dynamic memory allocation in stack

Posted 25 August 2009 - 12:30 PM

Neumann didn't say he was increasing the size of the program stack at runtime. He said he was allocating memory already on the stack for this particular purpose at runtime.


I think there is/was/continues to be miscommunication between you all.
Was This Post Helpful? 0
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Dynamic memory allocation in stack

Posted 25 August 2009 - 12:37 PM

edit: Dammit double post. I was hoping it would auto merge. sorry about that.

It's possible, but your compiler may not support it, using GCC:

#include <stdio.h>

int main(void)
{
   int i=0;
   int a;
   printf("anything\n");
   scanf("%i",&i);
   int myarray[i];
   for(a = 0; a < i; a++){
		 myarray[a] = a+2;
   }
   printf("\n\n");
   for(a = 0; a < i; a++){
		 printf("%d\n", myarray[a]);
   }
   system("pause");
   return 0;
}


This post has been edited by KYA: 25 August 2009 - 12:37 PM

Was This Post Helpful? 0
  • +
  • -

#4 Guest_Neumann*


Reputation:

Re: Dynamic memory allocation in stack

Posted 25 August 2009 - 12:39 PM

Read page 130 of C99 standard - http://www.open-std..../docs/n1124.pdf

Read man page of alloca() - http://www.kernel.or...3/alloca.3.html

Admit that you are completely wrong, (don't worry I will accept your eye-watering apology) and move the fuck on.
Was This Post Helpful? 0

#5 debjit625  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 51
  • View blog
  • Posts: 446
  • Joined: 06-September 08

Re: Dynamic memory allocation in stack

Posted 25 August 2009 - 12:50 PM

No KAY if that he would said i should have no problem as it true but when i asked him that

Quote

you are saying that you can increase the size of stack ?

he said

Quote

"Um, yeah. Ever heard about a stack pointer? Ever wondered why that stack pointer is moving? That stack pointer indicates how much address space the stack is taking up. I know.... shocking.


Thanks for the reply
Was This Post Helpful? 0
  • +
  • -

#6 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Dynamic memory allocation in stack

Posted 25 August 2009 - 12:53 PM

I misunderstood then. The above snippet does not increase the program's default stack size, it merely makes space for the array on the stack.
Was This Post Helpful? 0
  • +
  • -

#7 debjit625  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 51
  • View blog
  • Posts: 446
  • Joined: 06-September 08

Re: Dynamic memory allocation in stack

Posted 25 August 2009 - 01:00 PM

View PostNeumann, on 26 Aug, 2009 - 12:09 AM, said:

Read page 130 of C99 standard - http://www.open-std..../docs/n1124.pdf

Read man page of alloca() - http://www.kernel.or...3/alloca.3.html

Admit that you are completely wrong, (don't worry I will accept your eye-watering apology) and move the fuck on.



I have seen all those link you see those stuff and understand properly
Increasing stack space using Dymnamic allocation i think you must have gone out of your brain visit a doctor(soon).
After that learn C atleast try

And to KYA i will try using GCC thanks

and this is to Neumann

Quote

Yea, dynamic memory allocation is usually referred to heap. I used a made-up term "dynamic stack allocation", because I don't know how else to call it

Oh i dont knew that you made C terms

This post has been edited by debjit625: 25 August 2009 - 01:03 PM

Was This Post Helpful? 0
  • +
  • -

#8 Guest_Neumann*


Reputation:

Re: Dynamic memory allocation in stack

Posted 25 August 2009 - 01:12 PM

Quote

I have seen all those link you see those stuff and understand properly


Then you understood that "fly variables" and VLAs are part of C Standard? Good. I accept your apology.

As for the size of the stack that you're talking about. The maximum size of the stack is indeed fixed and is platform-dependent. The actual contents of the stack are being popped/pushed at runtime. And that's what happens with VLAs, the current stack pointer simply being moved at runtime to accommodate for the new size.

That's what I meant by stack size - the size of the actual content of the stack. You should've realized it when I said that the size of the stack increases when the stack pointer moves.

Quote

After that learn C atleast try

Oooh, nice one. I can do this too. After you see your gynecologist, try learning Assembly, basics of computer architecture, basics of C, and maybe, just maybe, you can learn some English on the way. Just try.

This post has been edited by Neumann: 25 August 2009 - 01:21 PM

Was This Post Helpful? -1

#9 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Dynamic memory allocation in stack

Posted 25 August 2009 - 01:18 PM

@debjit625

Just as an FYI: Microsoft Visual Studio does not support variable length arrays (an extension in C99 as previously noted).


What ever IDE/compiler you're using try this and you'll see if you have support or not:

#include <cstdio>
int main(){
	#if __STDC_VERSION__ >= 199901L
		printf("This compiler has C99 support\n");
	#else 
		printf("This compiler does not have full C99 support\n");
	#endif
	return 0;
}




Visual Studio does not have full C99 support, for example.



edit: List of what C99 features GCC supports can be found here.

This post has been edited by KYA: 25 August 2009 - 01:21 PM

Was This Post Helpful? 0
  • +
  • -

#10 Guest_Neumann*


Reputation:

Re: Dynamic memory allocation in stack

Posted 25 August 2009 - 01:30 PM

Screenshot of me compiling your code with GCC.

Attached image(s)

  • Attached Image

Was This Post Helpful? 0

#11 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6037
  • View blog
  • Posts: 23,432
  • Joined: 23-August 08

Re: Dynamic memory allocation in stack

Posted 25 August 2009 - 01:42 PM

OK, time to simmer this down. Enough of the ad hominems.
Was This Post Helpful? 0
  • +
  • -

#12 debjit625  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 51
  • View blog
  • Posts: 446
  • Joined: 06-September 08

Re: Dynamic memory allocation in stack

Posted 25 August 2009 - 02:31 PM

Hey after finding the implementation details of Variable Length Arrays, in C99(C99 a C standard) i found what was going on
see this code
void MyFunc(int size)
{
int myarray[size];
}



the size is not constant but it will compile ,but the size of the array will be unknown by the compiler at compilation time their will be no problem but where this array is stored? it is stored in stack at runtime.We all know that stack are limited atleast more then heaps,So VLAs just push the array in stack with out knowing is their any more space avaliable in stack and what happen when the size is too big the address pointed by the vlas is to an invalid address and your program crashes.So it is not used because it can cause bugs and this can be quite difficult to debug.

Any now Neumann you cant change your words , that
you have didnt said "Um,yeah i can increase the size of stack"

And yes i have got VLAs but you didnt got about "variables on fly" they are different stuff

Quote

Oooh, nice one. I can do this too. After you see your gynecologist, try learning Assembly, basics of computer architecture, basics of C, and maybe, just maybe, you can learn some English on the way. Just try.


I think you dont know english look with your glasses ALL THESE TEXTS ARE IN ENGLISH .

Quote

As for the size of the stack that you're talking about. The maximum size of the stack is indeed fixed and is platform-dependent. The actual contents of the stack are being popped/pushed at runtime. And that's what happens with VLAs, the current stack pointer simply being moved at runtime to accommodate for the new size.


yes but before when i told you this you said "debjit learn C or any things".Why you always change your words.

Ok why i am doing this.This is for just the stuff said by Neumann

Quote

If you ever programmed in Assembly you would realize what kind of nonsense you just said. Allocating memory on the stack is as simple as moving a stack pointer, I can assure you that manipulating pointers can be done at runtime.

Allocating memory! you are just pointing to an allocated memory using stack pointers.
Was This Post Helpful? 1
  • +
  • -

#13 Guest_Neumann*


Reputation:

Re: Dynamic memory allocation in stack

Posted 25 August 2009 - 02:50 PM

You have successfully compiled your code, even though you said it was impossible to do so. In other words, you admit you were completely wrong this whole time. Cool, it's nice to know that you have learned something today. Just don't forget to wipe that failsauce off your chin when you get up.

/thread

This post has been edited by Neumann: 25 August 2009 - 02:52 PM

Was This Post Helpful? 0

#14 debjit625  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 51
  • View blog
  • Posts: 446
  • Joined: 06-September 08

Re: Dynamic memory allocation in stack

Posted 25 August 2009 - 02:53 PM

Cant read english.How do you code in C
Was This Post Helpful? 0
  • +
  • -

#15 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6037
  • View blog
  • Posts: 23,432
  • Joined: 23-August 08

Re: Dynamic memory allocation in stack

Posted 25 August 2009 - 02:54 PM

Done.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1