![]() |
| | #1 |
| Registered User Join Date: Aug 2008
Posts: 13
| question about memory if i go Code: char *buff = "timmy"; ie Code: /*stack*/ int i =0; char* buff = "timmy"; char *ptr = NULL; /*heap*/ ptr = malloc(sizeof(char)*50); 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);
}
Code:
loadfile(FILE *fp,char *,int * x)
{
*x=5;
...
}
changed? |
| simo_mon is offline | |
| | #2 | ||||||
| CSharpener Join Date: Oct 2006
Posts: 5,334
| should be const char* Quote:
Quote:
string itself is stored elsewhere taking strlen("timmy") + 1 byte there Quote:
Quote:
Quote:
Quote:
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 | |
| | #3 |
| Registered User Join Date: Dec 2004 Location: Brazil, Porto Alegre
Posts: 152
| |
| Mortissus is offline | |
| | #4 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,785
|
__________________ 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:
| |
| Elysia is offline | |
| | #5 |
| Registered User Join Date: Dec 2004 Location: Brazil, Porto Alegre
Posts: 152
| So obvious... I am a little rusted. Thanks ;D |
| Mortissus is offline | |
| | #6 | |
| Registered User Join Date: Jun 2008 Location: Somewhere in Europe
Posts: 90
| Quote:
| |
| DL1 is offline | |
| | #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"; and that Code: char buffer[] = "timmy"; |
| simo_mon is offline | |
| | #8 | ||
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,785
| Quote:
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:
| ||
| Elysia is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |