C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 03-20-2009, 05:12 AM   #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?
simo_mon is offline   Reply With Quote
Old 03-20-2009, 05:50 AM   #2
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,334
Quote:
Originally Posted by simo_mon View Post

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

Quote:
is that a string literal
yes

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

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


Quote:
is the stuff on the stack in ram ?
yes

Quote:
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)

Quote:
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)
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Old 03-20-2009, 07:11 AM   #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?
Mortissus is offline   Reply With Quote
Old 03-20-2009, 07:32 AM   #4
Mysterious C++ User
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 14,785
Because:
http://apps.sourceforge.net/mediawik..._be_const_char
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 03-20-2009, 07:46 AM   #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
Mortissus is offline   Reply With Quote
Old 03-20-2009, 08:00 AM   #6
DL1
Registered User
 
Join Date: Jun 2008
Location: Somewhere in Europe
Posts: 90
Quote:
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?
DL1 is offline   Reply With Quote
Old 03-20-2009, 08:00 AM   #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...
simo_mon is offline   Reply With Quote
Old 03-20-2009, 08:02 AM   #8
Mysterious C++ User
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 14,785
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.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
heap vs stack memory question donglee C++ Programming 4 01-23-2009 04:34 PM
Pointer memory question Edo C++ Programming 5 01-21-2009 03:36 AM
Memory question John_L Tech Board 8 06-02-2008 10:06 PM
Another Dynamic Memory Question SirCrono6 C++ Programming 6 03-02-2005 12:10 PM
Is it necessary to write a specific memory manager ? Morglum Game Programming 18 07-01-2002 01:41 PM


All times are GMT -6. The time now is 04:46 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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