Re: memory



Jon Slaughter wrote:
"Bill Cunningham" <nospam@xxxxxxxxxxxxx> wrote in message

In C I have never really used malloc to alloc memory from the
heap. Now when using C's data types you are using the stack if
I am correct. In programming requirements are there times when
you want memory allocated from either parts of memory?

int mem[100]; 100 ints from stack
int *pi=malloc(100); 100 ints from heap

Obviously?

Suppose you want to return an array from a method? If the method
allocates the array on the stack then it won't be valid after
the method returns.

Don't confuse him. Bill uses C, which doesn't have C++ methods.

In addition "pi = malloc(100);" returns storage for 100 chars, not
ints. To get 100 items use the normal malloc call of "pi =
malloc(100 * sizeof *pi);", which will follow the type of pi.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
.



Relevant Pages

  • Re: Please Explain where will the struct be stored if it is declared inside the Class
    ... forget about structs for a second. ... can be stored either on the stack, or on the heap. ... First, think about the stack. ... A struct would act exactly the same as any of these decimals and ints. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: BMP/JPG/GIF library in Ada?
    ... I've chosen a constrained representation inheriting from Controlled. ... The internal array of data is ... or having to do the heap work in that outer type. ... What worries me is that even if using the heap I evade the stack ...
    (comp.lang.ada)
  • Re: Memory Limit
    ... What is the maximum size of the array that I can create? ... Is Heap or Stack based variable declaration just affects the speed? ... Since Windows uses a virtual memory model, ...
    (microsoft.public.vc.language)
  • Re: StackOverflowException.
    ... stores the byte array as a property in a Packet class. ... The array should be held on the heap, ... >> I know that locally declared byte arrays are stored on the stack. ...
    (microsoft.public.dotnet.framework)
  • Re: On VLAs and incomplete types
    ... from a stack. ... if the array lives as long as the code block it refers to is executing? ... Is heap and stack actually different in this? ...
    (comp.lang.c)