Sponsored Content
Top Forums Programming Parameters placement on stack in C Post 303035998 by migurus on Wednesday 12th of June 2019 02:21:00 AM
Old 06-12-2019
Parameters placement on stack in C

I am trying to illustrate the reverse order of parameters on the stack when passed to a function in C:


Code:
#include <stdio.h>

void  p(int p1, int p2, double p3)
{
        printf("params:\n"
                        "1) %p offset = %li\n"
                        "2) %p offset = %li\n"
                        "3) %p\n",
                        (void *)&p1,    (void *)&p1 - (void *)&p2,
                        (void *)&p2,    (void *)&p2 - (void *)&p3,
                        (void *)&p3);
}

int main(int argc, char *argv[])
{
  struct A a = {10,11,12, {1,2,3}};

        p(-1, 0, +1);
        return(0);
}

Result is:
Code:
params:
1) 0x7ffe2f20afac offset = 4
2) 0x7ffe2f20afa8 offset = 8
3) 0x7ffe2f20afa0

This is as expected on the 64 bit system (Ubuntu 19.04)


When I pass a structure as a parameter the stack looks puzzling to me:
Code:
#include <stdio.h>
struct A {
        int     n1;
        int     n2;
        int     n3;
        int     arr[3];
};

void    p(int p1, struct A p2, double p3)
{
        printf("params:\n"
                        "1) %p offset = %li\n"
                        "2) %p offset = %li\n"
                        "3) %p\n",
                        (void *)&p1,    (void *)&p1 - (void *)&p2,
                        (void *)&p2,    (void *)&p2 - (void *)&p3,
                        (void *)&p3);
}

int main(int argc, char *argv[])
{
  struct A a = {10,11,12, {1,2,3}};
        p(-1, a, +1);
        return(0);
}

Now result is:
Code:
params:
1) 0x7ffee5f7ddbc offset = -20
2) 0x7ffee5f7ddd0 offset = 32
3) 0x7ffee5f7ddb0

Parameter 3 is close to 1 and parameter 2 is not between 1 and 3? offsets look wrong to me.Please shed some light here, thanks in advance.
The compiler is gcc 7.4.0
 

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

code placement

I'm sorry if something like this has already been posted, but I didn't find anything like it. I'm using ksh The code that I've come up with will initially print what I want, but will then keep repeating the second number. I'm just trying to teach myself and can't seem to find the code to work.... (16 Replies)
Discussion started by: scott78
16 Replies

2. AIX

Aixterm icon placement?

Hi, I'm using xterm and aixterm with Mwm on AIX, and having trouble controlling icon placement when minimising windows. Basically when I run an xterm or aixterm I want to be able to specify where the icon will be if minimised. The man page mentions the "#geometry Geometry" option to aixterm,... (1 Reply)
Discussion started by: cunningdavid
1 Replies

3. UNIX Desktop Questions & Answers

Controlling icon placement?

Hi, I'm using xterm and aixterm with Mwm on AIX, and having trouble controlling icon placement when minimising windows. Basically when I run an xterm or aixterm I want to be able to specify where the icon will be if minimised. The man page mentions the "#geometry Geometry" option to aixterm,... (1 Reply)
Discussion started by: cunningdavid
1 Replies

4. Programming

what is stack winding and stack unwinding

helo can u tell me what do you mean by stack winding and stack unwinding Regards, Amit (2 Replies)
Discussion started by: amitpansuria
2 Replies

5. Solaris

Data placement in SVM

Is it possible to place the data in inner or outer edge with SVM ( solaris volume manager ) or VxVM like we can do in AIX LVM ? (1 Reply)
Discussion started by: fugitive
1 Replies

6. UNIX for Dummies Questions & Answers

Kernel Stack vs User Mode Stack

Hi, I am new to the linux kernel development area. I want to know what is the difference between kernel mode stack and user mode stack? Does each process has a user mode stack and a kernel mode stack?? Or Each process has a user mode stack and there is only one kernel mode stack that is shared by... (4 Replies)
Discussion started by: saurabhkoar
4 Replies

7. Cybersecurity

Snort/NTOP Placement

I have been asked to place 2 (1 NTOP & 1 SNORT) boxes within our network as part of our tool kit for network monitoring and Intrusion detection. Out network is very simplistic and it layed out like this: internet | | Cisco 1811 Router (8x Layer 2 switch ports) ... (0 Replies)
Discussion started by: metallica1973
0 Replies

8. UNIX for Advanced & Expert Users

Unix linked-list placement

Hi, I am programming in kernel, and I want to use a double linked list that holds infos that every process could access and modify THIS list. So, I suppose it is a 'global' variable since every process(thread) can reach it, I am wondering where to put it? by changing some of the kernel files? (1 Reply)
Discussion started by: louisTan
1 Replies
DOFILEREAD(9)						   BSD Kernel Developer's Manual					     DOFILEREAD(9)

NAME
dofileread, dofilereadv, dofilewrite, dofilewritev -- high-level file operations SYNOPSIS
#include <sys/file.h> int dofileread(struct lwp *l, int fd, struct file *fp, void *buf, size_t nbyte, off_t *offset, int flags, register_t *retval); int dofilewrite(struct lwp *l, int fd, struct file *fp, const void *buf, size_t nbyte, off_t *offset, int flags, register_t *retval); int dofilereadv(struct lwp *l, int fd, struct file *fp, const struct iovec *iovp, int iovcnt, off_t *offset, int flags, register_t *retval); int dofilewritev(struct lwp *l, int fd, struct file *fp, const struct iovec *iovp, int iovcnt, off_t *offset, int flags, register_t *retval); DESCRIPTION
The functions implement the underlying functionality of the read(2), write(2), readv(2), and writev(2) system calls. They are also used throughout the kernel as high-level access routines for file I/O. The dofileread() function attempts to read nbytes of data from the object referenced by file entry fp into the buffer pointed to by buf. The dofilewrite() function attempts to write nbytes of data to the object referenced by file entry fp from the buffer pointed to by buf. The dofilereadv() and dofilewritev() functions perform the same operations, but scatter the data with the iovcnt buffers specified by the members of the iov array. The offset of the file operations is explicitly specified by *offset. The new file offset after the file operation is returned in *offset. If the FOF_UPDATE_OFFSET flag is specified in the flags argument, the file offset in the file entry fp is updated to reflect the new file offset, otherwise it remains unchanged after the operation. The file descriptor fd is largely unused except for use by the ktrace framework for reporting to userlevel the process's file descriptor. Upon successful completion the number of bytes which were transferred is returned in *retval. RETURN VALUES
Upon successful completion zero is returned, otherwise an appropriate error is returned. CODE REFERENCES
The framework for these file operations is implemented within the file sys/kern/sys_generic.c. SEE ALSO
file(9) BSD
December 20, 2005 BSD
All times are GMT -4. The time now is 08:26 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy