\Device\NUL
Array parameters will decay into pointer
\Device\NUL
void func(type arr[]) { printf("%zu\n", sizeof(arr)); } will print size of pointer, not size of array that passed to the function
Hussein
I was talking about passing a parameter using a pointer is the right solution
\Device\NUL
\Device\NUL
this one is not
Really ? I said array parameters will turn into pointers
\Device\NUL
https://t.me/programminginc/456047
Hussein
Really ? I said array parameters will turn into pointers
according to the intel x86 calling conventions: when you pass parameters to a function, the register rax ( eax on 32bit machines) will contain the first parameter then the next parameter will be in rcx register then the third is stored in rdx register and so on so if an array is passed to a function then each element in the array will be pushed on the stack and the rax register will point to it (in case that the array was the first parameter). but if you use a pointer then the rax register will point to the address of the original array in memory whether it is on the stack or the heap or even on a different memory page if you are sharing memory between multiple programs
Hussein
assume that you have an array that is 30 kb in size, then when it is pushed on the stack, you will end up with two instances of the same array which increase the memory consumption to 60 kb rather than 30 which is the size of the original array
Pavel
void* but std::any is better in any sense std::variant if there's a limited set of possible types
Hussein
you have to cast it to the data type you want then use it like any regular variable if I’m not mistaken
\Device\NUL
Afaik, i never see calling convention like that. Example, in windows that Microsoft x64 calling convention. First argument is passed to rcx. In System V x86-64 userspace calling convention, first argument passed to rdi
\Device\NUL
which x86 calling convention is this? where is that written?
Originally i told him that such of array parameters will decay into pointer but he don't believe it. https://t.me/programminginc/456044
Alviro Iskandar
because when you pass a parameter to a function you are copying it’s content somewhere on the memory (usually the stack but sometimes cpu registers if the value is small enough) so if you have a big array then you pass it as a parameter to a function. you will end up with two instances on memory of the same array, this is why more memory will be required not mentioning that copying the content of a huge array might take sometime and will slow your program
There is no such a thing. Array arguments in C don't exist. Compilers accept it for various bad historical reasons and since array arguments don't actually exist in C, it is the size of the pointer, not the array. This is just a pointer copy, you don't copy the array elements via arguments. For ex, this will print the size of a pointer, not int [100]. void f(int arr[100]) { printf("%zu\n", sizeof(arr)); }
Alviro Iskandar
And that pointer will just be in %rdi on System V ABI x86-64 calling convention.
Jayaprakash
I wanna read the image into pointer and do transpose without using opencv function
Jayaprakash
Can any one help
Hussein
those get pushed on the stack
Hussein
There is no such a thing. Array arguments in C don't exist. Compilers accept it for various bad historical reasons and since array arguments don't actually exist in C, it is the size of the pointer, not the array. This is just a pointer copy, you don't copy the array elements via arguments. For ex, this will print the size of a pointer, not int [100]. void f(int arr[100]) { printf("%zu\n", sizeof(arr)); }
I understand that arrays are an abstraction of blocks of memory pointed accessed by a pointer to its address and you where right they are not pushed on the stack which make a lot more sence but what do you mean by historical reasons ? isn’t it considered as one of the features of high level languages to provide such abstraction?
M.
hi guys, new here
M.
Plz could someon teach me how to understand these kind of value in C prog lang: like in unsigned int a = 0x00010400L
Alviro Iskandar
Alviro Iskandar
That means... this way you can pass an array by value: struct x { int arr[100]; }; void f(struct x a) { // the "a" is on the stack }
️Skill
what's wrong here? std::wofstream& operator<<(std::wofstream &os, const Book &obj) { os << obj.name(); return os; } i got an error Invalid operands to binary expression
Anonymous
hello, anyone know how to make a stack whose limits are based on the weight of the items in the stack? I only know the stack based on the number of items in it
Bashir
i have some stupid question. I want to increase a string by multiplying by a number, but it seems that in C is not possible to do this printf("some text" * 3) in order to myltiply this line 3 times. But I'm getting an error. Is there any way to do this. PS. Im started learn C, before i learn python and i can make it with this lines of code print("text"*3)
Hussein
It is easier to open python shell and do print( "hello world" * 3 ) Then simply copy the output in your C code But if you are repeating the text by unknown number of times that your program will decide at runtime then yeah you should loop
Hussein
But try to do it outside other loops as much as possible
Bashir
@X_Techno_Pro @igorcmelo Thank you 🙏🙏😎
Anonymous
How can i access an element in an array. C program need help plz
Alviro Iskandar
How can i access an element in an array. C program need help plz
plz give some effort to learn it before dropping your question to the group!! it is very very very basic programming in C there are a lot of harder things outside there and you DO NOT make questions for all of them, you can't make a question for each line in your code, so learn, this trivial thing shouldn't be missed when you start learning
\Device\NUL
those get pushed on the stack
Not all program use cdecl or stdcall calling convention
Yuuji
Hello, can anyone help with a C++ problem? 🥺
Peter
Hello, can anyone help with a C++ problem? 🥺
Can u just write your issue. I am sure that someone understands or faced earlier
Anonymous
Please use an formatting page like pastebin
Anonymous
Pastebin not here
Gee
Can anyone help me make this code functional. I want the user to be able to access all the choices like weekly menu, contact details... Here's the link to the code https://pastebin.com/fmRn1ZY6 Thank you in advance
conko
Why invalid ?
std::memset fills an array by bytes. If your data type is larger than a byte, filling every byte with 0 will definitely set every element of the array to 0. However, for other values like 1, you'll get different result. For example, memset 1 to a std::int32_t will get 16843009.
Iwan
@fathurrohman26 what is english of "gercep"? 😁
Anonymous
"score++" 😀
Hussein
Not all program use cdecl or stdcall calling convention
Yeah but most of them uses the calling convention of System V ABI Also it makes easier to integrate C programs with assembly But compilers like Clang and GCC on optimisation level -O3 will not follow it precisely and practically they don't usually speed up your program that much (depending on the program)
三体183号
I want to use QT to place a window inside. The widget in this window has a title bar and border. Do you know some solutions
Hussein
How can i access an element in an array. C program need help plz
You do it with: array[ index ] or you can use a pointer: int array[3] = { 1 , 2 , 3 }; int * array_ptr = array; Then access it with: *( array_ptr ) //will return 1 *( array_ptr + 1 * sizeof(int) ) //will return 2 *( array_ptr + 2 * sizeof(int) ) //will return 3
Hussein
I have done that my tests are still failing 😞
What error message are you getting?
Anonymous
He assumes arays are 3 but 4 indices.Is that what you mean?
No. Assuming int is 4 bytes long, he is accessing array elements at position 4 and 8 which is Undefined Behavior as the original array is just 3 elements long (meaning only positions 0,1 and 2 are valid).
Anonymous
Isnt that a defined Array size he declared there? Its allowed
What are you talking about? His array size is 3. So you can access only elements arr[0], arr[1] and arr[2]. In his code he is doing something like *(array_ptr+1*sizeof(int)) which is equivalent to arr[4] assuming integers are 4 bytes long. This is wrong. *(array_ptr+2*sizeof(int)) accesses the element arr[8] which is also wrong.
Hussein
No. Assuming int is 4 bytes long, he is accessing array elements at position 4 and 8 which is Undefined Behavior as the original array is just 3 elements long (meaning only positions 0,1 and 2 are valid).
The number of elements of the array is 3 multiplied by the the size of int for each element Which makes the total 12 bytes not 3 bytes(like strings) and I'm accessing at offset of 8 bytes which put me at the start of the 9th byte and my array is 12 bytes in size
Anonymous
The number of elements of the array is 3 multiplied by the the size of int for each element Which makes the total 12 bytes not 3 bytes(like strings) and I'm accessing at offset of 8 bytes which put me at the start of the 9th byte and my array is 12 bytes in size
No you are wrong. Try this code: #include <iostream> int main(){ int a[3]={99,999,9999}; std::cout << *(a+2*sizeof(int)); } See if it prints 9999. Now try this instead #include <iostream> int main(){ int a[3]={99,999,9999}; std::cout << *(a+2); } See if this prints 9999 And then understand why you are wrong.
Hussein
No you are wrong. Try this code: #include <iostream> int main(){ int a[3]={99,999,9999}; std::cout << *(a+2*sizeof(int)); } See if it prints 9999. Now try this instead #include <iostream> int main(){ int a[3]={99,999,9999}; std::cout << *(a+2); } See if this prints 9999 And then understand why you are wrong.
Alright.. you were right But I'm still confused if you increment an address by 1 why would it access the forth byte instead of the second byte in memory I've written a fair amount of assembly to know that in fact we are accessing the 5th byte in memory
Anonymous
Alright.. you were right But I'm still confused if you increment an address by 1 why would it access the forth byte instead of the second byte in memory I've written a fair amount of assembly to know that in fact we are accessing the 5th byte in memory
It is because when you do a+2, the compiler knows that a is a pointer to int and that you are trying to access an integer two places away from the current int. So it automagically increments the pointer to point to the right integer. This saves us from using sizeof calculations. So basically if you have something like this: int* ptr = <...>; *(ptr+2) = <...>; The compiler does something that is the equivalent of *((int*)(((char*)ptr)+2*sizeof(int))) = <...>
Anonymous
Isn't it just an address how the compiler can tell how I'm gonna be using my memory? How about if wanted to implement let's say a strange hash function that needs to access each byte in an array of integers Do I really need to cast the address to an char* then cast it back to int * each time?
>>>> Do I really need to cast the address to an char* then cast it back to int * each time? >>>> Yes. If you need to manipulate individual bytes, you will have to use a char or a std::byte pointer. Pointer arithmetic on pointers will be automagically translated by the compiler to move past objects that are being pointed to rather than individual bytes. Only for chars and byte (or a custome type that is of size 1) they will actually be individual bytes because sizeof(char) and sizeof(byte) == 1 by definition.
Hussein
Sorry 😅 I guess I went too low level on that, that I started doing the job of the compiler itself😂
Vlad
This explains why the compiler would need to know the type of the data type accessed by any pointer
It's more complicated than this. For example floating point numbers could have separate registers. So types must be compatible
Alex
hi guys, i'm working on a project where i have to do the TCP's Selective Repeat algorithm with sockets in C. i know how to do it, but the problem is handling the timers.. any tips? i think i can't use the SIGALRM because it provides only 1 timer, but i need more timers.
Alex
my idea is using 3 Threads: T1: reads from file and writes the data on a circular array. T2: reads from the circular array and sends it via socket. T3: reads from socket waiting for the ACKs. when ACKs are received, T1 can read the next part of the file.
Alex
but every time T2 sends a message, he has to to start a timer. if the timer expires before the ACK is received, he has to send that message again.