Click here to Skip to main content
13,257,667 members (71,536 online)
Rate this:
Please Sign up or sign in to vote.
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 4-Oct-12 15:53pm
Updated 4-Oct-12 16:01pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

No; as you were told on Stack Overflow, it can vary.
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.171114.1 | Last Updated 5 Oct 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100