Click here to Skip to main content
Rate this: bad
good
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 15:53pm
Edited 4-Oct-12 16:01pm
v2
Rate this: bad
good
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.
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 398
1 DamithSL 235
2 OriginalGriff 230
3 Zoltán Zörgő 149
4 Dave Kreskowiak 120
0 OriginalGriff 7,575
1 DamithSL 5,529
2 Sergey Alexandrovich Kryukov 5,279
3 Maciej Los 4,961
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web04 | 2.8.141223.1 | Last Updated 5 Oct 2012
Copyright © CodeProject, 1999-2014
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