stack vs heap performance

This is a discussion on stack vs heap performance within the C Programming forums, part of the General Programming Boards category; Hi everyone, I'm developing an application that will measure connectivity and throughput over a wireless network between embedded systems, and ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    7

    stack vs heap performance

    Hi everyone,
    I'm developing an application that will measure connectivity and throughput over a wireless network between embedded systems, and i just had a question about the performance issues associated with dynamically allocating a chunk of memory on the heap rather than just declaring it and having it be on the stack.

    I already have quite a number of things on the stack, and there is an area of code im now developing that needs to remember a value for a very small amount of time, use it once, and then forget it. would it be wise to just declare a variable for it, say an int, or do a malloc(sizeof(int)) and use bitwise operations on it? thanks.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,167
    Using the stack is definitely faster than allocating memory from the heap. The only overhead in using the stack is adjusting a CPU register. Heap management requires quite a bit more to happen behind the scenes (not the least of which is at least one system call). Things might be different for embedded systems, but that's my experience.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >needs to remember a value for a very small amount of time, use it once, and then forget it
    That screams stack to me.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    7
    many thanks friends

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  2. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 09:12 PM
  3. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 05:27 PM
  4. heap question
    By mackol in forum C Programming
    Replies: 1
    Last Post: 11-30-2002, 04:03 AM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21