question about memory

This is a discussion on question about memory within the C Programming forums, part of the General Programming Boards category; i have some general questions about c and memory if i go Code: char *buff = "timmy"; is that a ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    13

    question about memory

    i have some general questions about c and memory

    if i go

    Code:
    char *buff = "timmy";
    is that a string literal and do string literals and ints go on the memory stack and mallocs on the heap?

    ie

    Code:
    /*stack*/
    
    int i =0;
    char* buff = "timmy";
    char *ptr = NULL;
    
    /*heap*/
    
    ptr = malloc(sizeof(char)*50);
    is the stuff on the stack in ram ?
    is the difference between the stack and the heap is that the stack is ordered and heap is potentially fragmented ( sorry but i gotta know )?


    last question

    when i call a function in main called loadFile

    Code:
    int main()
    {
       
       FILE *fp;
       int someInt;
    
       loadFile(fp,"somefile",&someInt);
    
    }
    is loadfile now on the stack, and say that loadfile changes the value of fp and someint,

    Code:
    loadfile(FILE *fp,char *,int * x)
    {
       *x=5;
    ...
    
    }
    now fp anf someint are also on the stack and the value at that memory location is
    changed?

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by simo_mon View Post

    Code:
    char *buff = "timmy";
    should be const char*

    is that a string literal
    yes

    and do string literals and ints go on the memory stack
    variable buff - a pointer - goes to the stack (takes 4 bytes there probably)
    string itself is stored elsewhere taking strlen("timmy") + 1 byte there

    and mallocs on the heap?
    pointer is stored on the stack and memory itis pointing on the heap


    is the stuff on the stack in ram ?
    yes

    is the difference between the stack and the heap is that the stack is ordered and heap is potentially fragmented ( sorry but i gotta know )?
    have no idea, why bother? the main difference - stack is limited by compiler settings with values like 1 or 2 M heap is limited by the computer virtual memory size available to one process (2G on XP 32 bit if I'm not mistaken)

    when i call a function in main called loadFile

    Code:
    int main()
    {
       
       FILE *fp;
       int someInt;
    
       loadFile(fp,"somefile",&someInt);
    
    }
    is loadfile now on the stack, and say that loadfile changes the value of fp and someint,

    Code:
    loadfile(FILE *fp,char *,int * x)
    {
       *x=5;
    ...
    
    }
    now fp anf someint are also on the stack and the value at that memory location is
    changed?
    the code of the function is never on the stack,

    parameters of the function like fp, pointer to "somefile", pointer to someIntfp are placed on the stack before function call

    local variables of the function are allocated on the stack as well after the call to it

    after function exits - stack pointer returns where it was before function call making all local variables unavailable (values are still there but this space is noted as free and could be used any moment for example by the next function call)
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    Quote Originally Posted by vart View Post
    should be const char*
    Why?

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    So obvious... I am a little rusted.

    Thanks ;D

  6. #6
    DL1
    DL1 is offline
    Registered User
    Join Date
    Jun 2008
    Location
    Somewhere in Europe
    Posts
    99
    Quote:
    and do string literals and ints go on the memory stack

    variable buff - a pointer - goes to the stack (takes 4 bytes there probably)
    string itself is stored elsewhere taking strlen("timmy") + 1 byte there
    But if buff were global or static surely it wouldn't go on the stack?

  7. #7
    Registered User
    Join Date
    Aug 2008
    Posts
    13
    awesome response_s thanks very much for answering


    its slowly coming together

    its actually really helpful to get a handle on what's actually happening in c with memory

    and i didn't know that

    Code:
    char *buffer = "timmy";
    is in read only memory

    and that

    Code:
    
    char buffer[] = "timmy";
    lets you write over the memory for the same amount of space...

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Quote Originally Posted by DL1 View Post
    But if buff were global or static surely it wouldn't go on the stack?
    It wouldn't. The actual pointer is stored in some other space reserved for static and global variables.
    Still, the memory allocated with malloc would still be stored on the heap and the pointer only stored in the static and global storage.

    Note, however, that is not certain. The C Standard does not mention anything about this. So it's all up to the OS itself.
    String literals are very often read-only, but may not be, depending on the OS.
    The different storage sections are also different depending on OS.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. heap vs stack memory question
    By donglee in forum C++ Programming
    Replies: 4
    Last Post: 01-23-2009, 03:34 PM
  2. Pointer memory question
    By Edo in forum C++ Programming
    Replies: 5
    Last Post: 01-21-2009, 02:36 AM
  3. Memory question
    By John_L in forum Tech Board
    Replies: 8
    Last Post: 06-02-2008, 10:06 PM
  4. Another Dynamic Memory Question
    By SirCrono6 in forum C++ Programming
    Replies: 6
    Last Post: 03-02-2005, 11:10 AM
  5. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM

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