Click here to Skip to main content
15,886,632 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to find out the physical address of the command line arguments ( argv[i] (all of them if more than one )) before run time . To be specific , the compiler or OS might have pushed the command line values into the stack before calling main , and passing a pointer to main on execution which points to the argument array ( if my knowledge is rite right) so is it possible to know the physical address before runtime ?
Posted
Updated 4-Oct-12 15:01pm
v2

Well, they could be stored somewhere relative to the address of main, but not generally. main is in the code section, but the arguments are data. I suggest that you print out the addresses of argv and main and see if they maintain a relationship ... but even if so, it could change later.

I got email saying there was a comment but it isn't shown here. Anyway, you can simply do
printf("%ld\n", (char*)argv - (char*)main);
... this is not guaranteed to work by the C standard, but should work in normal implementations. However, I doubt that this will be a fixed value or that it would be useful even if it were.
 
Share this answer
 
v3
No; as you were told on Stack Overflow, it can vary.
 
Share this answer
 
Comments
archies50 4-Oct-12 21:00pm    
okay , lets say its not possible ! Now lets suppose we know the point of program entry , ie the memory address where main is called .Using this can we find out where are the comman line arguments stored ? like x address before the address of main.
Chuck O'Toole 5-Oct-12 1:27am    
What possible use would it be to know this *before runtime*? If you application isn't running, why would you care? Plus, it is so easily discoverable at runtime from argv or CWnd::m_CmdLine so why bother trying to figure out the "unknowable" ahead of time. Windows, Linux, pick your favorite OS and/or hardware platform, they all can do it differently yet they all follow the standard of argv for telling you where they put the args. This just seems like a silly exercise.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900