What exactly are you trying to do with p? Tell us.
Or rather, what do you WANT it to do?
Printable View
What exactly are you trying to do with p? Tell us.
Or rather, what do you WANT it to do?
I already said:
Like any memory allocating.Quote:
If the user's input is Chris so 6 bytes are used instead of 8.
If the user's input is Matt so 5 bytes are used instead of 8.
btw,
what is the heap and what is the stack?
How do you want x and p to relate to each other? Currently they are completely separate entities, and unrelated.
--
Mats
No.Quote:
I thought (or still think if I'm right) that it allocates memory for the x variable although p isn't "related" to x in anyway.
If the user's input is Chris so 5 bytes are used instead of 8.
If the user's input is Matt so 4 bytes are used instead of 8.
that's what actually happening, right?
An array of eight characters, like an array of any other object, is actually eight individual variables that happen to be stored in adoining memory.
It doesn't matter if you use what you have or not, really. It will still be there and there if you need it. In fact, the problem is often inverted: arrays are usually not big enough.Code:+--------------------------------------+
|'a' |'r' |'r' |'a' |'y' |'s' |'\0'| |
| | | | | | | | |
+--------------------------------------+
You can't do that, if I'm understanding it correctly. You allocate a fixed set amount of bytes, in this case 8. So you have 8 bytes of memory. Although, you only use 5 bytes if you enter "Matt."
EDIT: What the heck is the stack and the heap? WTF? You use malloc and you have no idea what it does, do you?
That won't work. I suggest you read up on your programming skills because you're misusing malloc and don't know what the heap and stack are.
No, that's not what I meant. Can you describe WHAT YOU WANT TO DO [in a bigger picture, ignore x, p and any other part of your current code].
--
Mats
In short: The stack and the heap are two different storage areas. The stack is small storage area usually limited to around 1 MB. When you do char x[8] you allocate a variable of the stack which is 8 bytes. If you do char* p = malloc(sizeof(char) * 8) you allocate 8 bytes on the heap.
Let me make this clear to you:
Since you are allocating your variable on the stack, you do not need to allocate additional memory on the heap, because your x array can already hold 8 bytes of information.
I guess I should start reading a good book instead of messing around here, confusing myself more and more.
well, thank you all, I'm gonna start reading the book I wanted to read a day ago hehe.
BTW,
How's my English? it's important to me since there's a huge strike in my school (middle school-high school)! we (and every school in Israel except for elementary schools) haven't learned for a month and a week, lol.
Sorry for telling you this lol, I'm bored, these are the affects of the strike :D
BTW 2,
In 1 heap there are few stacks, right?
Your English is pretty good. Yes, I do very much recommend you start learning the language properly. Especially pointers, the stack and the heap, since that's what seems to be troubling you here. Oh, maybe arrays too. Or just everything. Can't hurt.
There is one stack per thread - usually there's only one thread too.
There CAN be multiple heaps, but normally there is only one.
Of course, with multiple threads, the stack may well be allocated from the heap - there is nothing particularly magical about the stack - it's just one area of memory that the OS has assigned to the thread to use as the stack. With the risk of confusing you furhter, you can even, with a simple piece of assembler code, move to another stack - just remember where you came from, otherwise you'll never return back to main!
Of course, one task has one heap, another task will use another heap, and each process/task has at least one thread -> one stack, so there'll be many of each in the whole fo a Windows or Linux system.
--
Mats