char ** and argv memory question

This is a discussion on char ** and argv memory question within the C Programming forums, part of the General Programming Boards category; hi there i am super confused, i am writing a program to launch processess and on my travels i was ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    13

    char ** and argv memory question

    hi there i am super confused, i am writing a program to launch processess and on my travels i was getting some wierd results from command line arguments that i was attempting to parse ( none of that here ) but the wierdest thing ever happens below....???

    i have removed all of the other code, all other functions etc i have a blank utility.c folder ( where i keep all functions that i call in main, i deleted everything except for 1 line comment),i have removed any function calls from main .... what you see below is all that is running ??

    note that there is no malloc for the pointer char **progNameAndArgs1;, nor is there a malloc for an element within that ie progNameAndArgs1[0] = malloc ....


    Code:
    int main(int argc, char *argv[])
    {
    
    	int test1 = 0;
    	int numberOfProgs;
    	int i =0;
    	
    	char **progNameAndArgs1;
    	char **progNames;
    	char *progNames2[2];
    	
    	char **progNameAndArgs2 = malloc(sizeof(char*)*(2));
    	
    	
    	printf(" main progNameAndArgs1 %s \n ",progNameAndArgs1[0]);
    	printf(" main progNameAndArgs1 %s \n ",progNameAndArgs1[1]);
    so my question is how do i get the output

    Code:
     main progNameAndArgs1 ./driver
      main progNameAndArgs1 ./foo
    when i pass ./driver ./foo at the command line ????

    from my thinking

    1 . char **progNameAndArgs1 is pointing to the same memory as argv ??? is this standard??


    couple of questions

    1 when a program completes, and then i run it again am i accessing the same area of memory ??

    2 if i create a string array and add an entry

    Code:
      char **stringArray = malloc(sizeOf(char*)*2)
      stringArray[0] = malloc(sizeof(char)*5)
      strcpy(stringArray[0],"ted");
    if i do the right thing then when i finish up i free(stringArray) or the individual elements....

    but if i don't free it up is it left somehow in memory ??

    so if i ran my program for a 2nd time but changed the code to

    Code:
      char **stringArray ;
    
      printf(" %s \n",stringArray[0] )
    and then printed stringArray[0] would i get ted again ?????

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,622
    Quote Originally Posted by simo_mon View Post
    hi there i am super confused, i am writing a program to launch processess and on my travels i was getting some wierd results from command line arguments that i was attempting to parse ( none of that here ) but the wierdest thing ever happens below....???

    i have removed all of the other code, all other functions etc i have a blank utility.c folder ( where i keep all functions that i call in main, i deleted everything except for 1 line comment),i have removed any function calls from main .... what you see below is all that is running ??

    note that there is no malloc for the pointer char **progNameAndArgs1;, nor is there a malloc for an element within that ie progNameAndArgs1[0] = malloc ....


    Code:
    int main(int argc, char *argv[])
    {
    
    	int test1 = 0;
    	int numberOfProgs;
    	int i =0;
    	
    	char **progNameAndArgs1;
    	char **progNames;
    	char *progNames2[2];
    	
    	char **progNameAndArgs2 = malloc(sizeof(char*)*(2));
    	
    	
    	printf(" main progNameAndArgs1 %s \n ",progNameAndArgs1[0]);
    	printf(" main progNameAndArgs1 %s \n ",progNameAndArgs1[1]);
    so my question is how do i get the output

    Code:
     main progNameAndArgs1 ./driver
      main progNameAndArgs1 ./foo
    when i pass ./driver ./foo at the command line ????
    Answer:
    The output is undefined. You may get garbage. You may get a crash. You may even get the same data which you passed as command line.
    There's no real explanation as to what happened. Just don't do it.

    from my thinking

    1 . char **progNameAndArgs1 is pointing to the same memory as argv ??? is this standard??
    No. Compiler dependant or chance. Behavior is undefined.

    couple of questions

    1 when a program completes, and then i run it again am i accessing the same area of memory ??
    Maybe. Maybe not. It's up to the OS to handle that.
    Sometimes you do end up with the same memory address, sometimes not.

    2 if i create a string array and add an entry

    Code:
      char **stringArray = malloc(sizeOf(char*)*2)
      stringArray[0] = malloc(sizeof(char)*5)
      strcpy(stringArray[0],"ted");
    if i do the right thing then when i finish up i free(stringArray) or the individual elements....
    free(stringArray[0]);
    free(stringArray);
    Every pointer you allocate via malloc must have a free.
    You can't just try to free the entire array and expect it to work.

    but if i don't free it up is it left somehow in memory ??
    Yes, until the app exits (but then again, this is also OS dependant, so don't do it).

    so if i ran my program for a 2nd time but changed the code to

    Code:
      char **stringArray ;
    
      printf(" %s \n",stringArray[0] )
    and then printed stringArray[0] would i get ted again ?????
    Again, undefined. See the first example.
    Last edited by Elysia; 08-16-2008 at 02:43 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    > char **progNameAndArgs2 = malloc(sizeof(char*)*(2));
    A rough guess, the run-time code did the same thing to generate an argv, then freed that memory before calling main.

    As a result, the memory you allocate looks remarkably like an argv as well.

    Try it with another compiler, I'm sure you'll get a different answer.
    I just get a core file for my trouble.

    Which is pretty much what you would expect, what with all the uninitialised data.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Salem View Post
    > char **progNameAndArgs2 = malloc(sizeof(char*)*(2));
    A rough guess, the run-time code did the same thing to generate an argv, then freed that memory before calling main.

    As a result, the memory you allocate looks remarkably like an argv as well.

    Try it with another compiler, I'm sure you'll get a different answer.
    I just get a core file for my trouble.

    Which is pretty much what you would expect, what with all the uninitialised data.
    Eh, no. The OP prints progNameAndArgs1.Since the pointer is never initialized, it points at some arbitrary "random" location in memory, which in this case happens to be a copy of argv - there are possobly copies of argv on the stack when main is called. But add an "int x" in front of your progNameAndArgs1 variable, and it will [highly likely] contain something else, which may well lead to a crash.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  2. Issue with Linked Listing
    By DKING89 in forum C Programming
    Replies: 1
    Last Post: 04-07-2008, 10:12 AM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  4. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 11:25 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21