+ 4

Pointer and dynamic memory in c++

Hi, community .Please can anyone explain to me, a c++ newbie, what pointers are for. In general, dynamic memory. yes, it's a stupid question, but I can't figure it out

23rd Feb 2022, 8:24 PM
ᙢᖇ.ᚪ ᚱ ع ℰ 爪 Ꭿ ກ
ᙢᖇ.ᚪ ᚱ ع ℰ 爪 Ꭿ ກ - avatar
5 Respuestas
+ 6
First of all there are no stupid questions if they will teach you new things! A pointer is a variable in C++ that holds the address of another variable. They have data type just like variables, for example an integer type pointer can hold the address of an integer variable and a character type pointer can hold the address of char variable (for instance, let's say you have an integer pointer, you can imagine it as an arrow that points to the location of a specific normal int variable, the pointer itself doesn't have the value of that variable but it has the value of the variable's location). Regarding dynamic memory allocation: In many cases all memory needs are determined before program execution by defining the variables needed. But there may be cases where the memory needs of a program can only be determined during runtime. For example, when the memory needed depends on user input (for instance, the size of an array that is determined by what the user enters (in runtime)). On these cases, programs need to dynamically allocate memory, meaning that the memory will be allocated during the running of the program and not before it runs, for which the C++ language integrates the operators new and delete.
23rd Feb 2022, 9:04 PM
WildGeese
WildGeese - avatar
+ 3
WildGeese , Thank you very much, but I don't quite understand how to use the pointers themselves. Theoretically, I went through it, I used it in the code, but only by forcibly introducing them
23rd Feb 2022, 10:00 PM
ᙢᖇ.ᚪ ᚱ ع ℰ 爪 Ꭿ ກ
ᙢᖇ.ᚪ ᚱ ع ℰ 爪 Ꭿ ກ - avatar
+ 3
G'day _ᙢǾሊỢȡΘŬ_ I think WildGeese gave a great answer! I can add to that. Using a pointer also saves needing to copy all of the variable data into a function, remember that a variable has scope & that when sending something like an array to a function you actually copy all of the data so it is available in the scope of that function. When you send a pointer to a function, you only send the few bytes that hold the address that the pointer points to. You can then use pointer arithmetic to traverse the array. Eg char arr[25][256]; char *arrptr=&arr[0][0]; When you send arr to a function you send 25*256*sizeof(char) But if you send *arrptr you send sizeof(char) (I'm still a learner, and I'm trying to master C before moving on to C++. I don't think I said anything wrong, but I'm only 95% confident 😎) EDIT: sizeof(char) is one byte. sizeof(arrptr) is either 4 bytes (32 bit system) or 8 bytes (64 bit system). sizeof(arr[25][256]) is 6400 bytes.
23rd Feb 2022, 10:02 PM
HungryTradie
HungryTradie - avatar
+ 3
To traverse the array arr[25][256] with pointer arithmetic: After setting up the array and the pointer for (i=0 ;i<25 ;i++){ for (j=0 ;j<256 ;j++){ putchar(*(arrptr+i*256 +j)); } } 🔨You may need to change putchar() to a C++ output... Maybe cout << This will use i=0 for the first 255 of j iterations, then when i=1 j=0 then i*256 +j becomes 256 i=1 j=1 accesses the 257th char stored in the array. All this is done without copying the 25*256*sizeof(char) into the scope of the function.
23rd Feb 2022, 10:16 PM
HungryTradie
HungryTradie - avatar
+ 3
I think I understand where you are finding it complicated, You need to understand how to use the * and & symbols and what does each one do here is an example: #include<stdio.h> int main() { int *p; // This is an integer pointer (* indicates that this is a pointer) int x; // this is a normal integer variable x=52; p=&x; // the pointer p gets the address of x printf("%d\n", p); /* Address of x */ printf ("%d\n", &x); /* Address of x */ printf("%d\n", *p); /* value of x */ printf ("%d\n", x); /* value of x */ return 0; } so in the line that says p = &x , the & symbol returns the address of x instead of it's value, and we did that here because p is a pointer so it has to get an address of a variable and not it's value (so always remember & is address) In the first printf you are printing the value of the pointer p which is as I have mentioned an address value (the address of x), in the second one you are printing &x which means the address of x, so this will print pretty much the same thing as the first printf. In the 3rd printf, we are using the * symbol, which means (go to the address that the pointer p has in it, and grab the value stored in that address which is the value of x (we have the address of x in p), so it will print the value of x, and the 4th printf is obviously the same thing (prints the value of x which is 52). One more thing to not get confused with, that the * in the declaration of the pointer p in the first line, doesn't have the meaning of the * that I have explained (of getting the value in the address), it just indicates that the variable p is a pointer and not a normal variable. Hopefully this helped a little bit, I recommend you to try and mess around with pointers and use the & * symbols and try to do some quizzes regarding pointers. Good Luck!
23rd Feb 2022, 10:18 PM
WildGeese
WildGeese - avatar