Click here to Skip to main content
11,642,855 members (72,937 online)
Rate this: bad
Please Sign up or sign in to vote.
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 14:53pm
Edited 4-Oct-12 15:01pm
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 at 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 at 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.
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.

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

  Print Answers RSS
0 DamithSL 318
1 Sergey Alexandrovich Kryukov 250
2 OriginalGriff 240
3 180
4 Afzaal Ahmad Zeeshan 155
0 Mika Wendelius 485
1 DamithSL 457
2 OriginalGriff 365
3 Afzaal Ahmad Zeeshan 258
4 Sergey Alexandrovich Kryukov 254

Advertise | Privacy | Mobile
Web03 | 2.8.150731.1 | Last Updated 5 Oct 2012
Copyright © CodeProject, 1999-2015
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