I will be studying C/C++ so I am trying to learn a little more about it. I have been using Python and Java for a while now. My first question is when do you need to devote system memory and when dont you?
C itself basically works memory-oriented. There is a lot of stuff you can learn: pointers, memory alignment, various ways of memory allocation, shared memory (etc. Linux) etc. If you are just starting, focus on single pointers at first. Most people don't even get them right. Then try to understand double, triple etc. pointers. If you have that, you have learned quite a lot. Apart from that just try to understand how to use malloc, calloc, realloc and free correctly. If you can handle that, you understand a big portion. If you then want to go deeper, you can look at stuff like memory alignment, various ways of memory allocation and garbage collection. Both of them can help you a lot if you are going to build something bigger or need a correct ABI. Here is a question for you:
How big is this struct measured in bytes on a 64 bit system?
struct S {
char a;
long b;
char c;
int d;
long e;
};
I would guess it would be the cumulative memory allocation for each primitive type in that struct. After some reading it appears that by default the system devotes memory to the primitives when declared and initialized and will write over when it goes out of scope. The alternative is to use malloc and free.
Yes, but how many bytes is it in size, given: char = 1, int = 4, long = 8 bytes ?
Is it 22 bits?
I have some other questions?
can you assign a pointer like this?
int *tem = 5;
if you have a function this this
foo(int num){
does stuff
}
can I pass this through it?
int *num = 22;
foo(*num);
No. That's why I asked. It is actually 32 bytes in size.
Due to memory alignment (the C compiler does that for you) the struct looks like this:
struct S {
char a;
char padding_1[7];
long b;
char c;
int d;
char padding_2[3];
long e;
};
Maybe you want to have a look: https://en.wikipedia.org/wiki/Data_structure_alignment
You could optimize that struct for memory usage like this:
struct S {
char a;
char c;
int d;
char padding[2];
long b;
long e;
};
Now it is only 24 bytes in size. If you need correct ABIs to pass data, such things get important. But that's quite advanced stuff. If you are just starting to learn C, this shouldn't bother you. I just wanted to give you that to illustrate that C programming is a lot about how a computer actually works and how memory looks like. Coming from a high-level language that's important. A lot of people try to mimic Java or Python in C which is bad since C is completely different.
If you are more into it, you can actually look for functional and OO programming in C. You can even do that. In fact C can handle every paradigm. You just need to be aware that you don't have a class or function (first-level data type) as a language feature.
Most important: Try to understand C as an architecture independent assembly language - because that's why it was created. If you understand state machines and maybe a bit of the instruction set of you PC, you can even see the assembly while looking at C.
My prof told us the same thing how C can do OO stuff. I imagine that when it gets more complicated you can use big O notation for memory usage. I have also studied some digital logic stuff and state machines so that helps.
When school starts again I will be taking a course over C/C++ and another course over Computer Architecture that will involve assembly language. This will be a good preview for those courses to come.
I am still wondering about this because I am working on a basic project right now to just get used to it.
Leads to UB (undefined behaviour), it might work or it might not, it's platform, compiler dependant.
Long story short, it's not legal to do that.
The correct way to do it, would be:
int *tem = malloc(sizeof(int));
*tem = 5;
Will work if *num is correctly initialized (like I did above), won't work otherwise.
So my compiler might assign memory or it may not. But conventionally I should always use malloc.
What about passing a pointer to a function that expects an int?
int *tem = 5;
Won't assign any memory to it. The pointer *tem doesn't point to any memory location and yet you assign a value to it, resulting in a crash or a segmentation fault when you try to access *tem.
It can be done by using the unary operator *, it performs the "dereferencing" operation on a pointer; it looks at the address stored in the pointer, and goes to that address and returns the value.