[C] Simple pointer question

Status
Not open for further replies.
Does this look right?
Code:
int main(int argc, char** argv, char** envp)
{
  int status;
  int pid = fork();  
  char* command[2];  

  command[0] = "cat";
  command[1] = "project1a.c";
  command[2] = NULL;

  if(pid == 0)
    { 
      printf("Child %i is executing the command\n\n", getpid());
      execvp(command[0], command);                         
    }
  else
    { /* In parent process */
      waitpid(pid, &status, WUNTRACED);                   
      printf("\nParent %i has finished waiting for its child process to complete\n\n", getpid());
    }

  return 0;
}

Program compiles fine with gcc and runs fine too, but the command[3] still bothers me. Isn't command supposed to be two char pointers?

I tried a similar program on VS2010 and it gave me runtime errors - specifically: variable arr was corrupted.
Code:
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv, char** envp)
{
	char* arr[3];
	int i = 0;
	arr[0] = "test";
	arr[1] = "test2";
	arr[2] = "testing";
	arr[3] = NULL;

	for(; i < 3; ++i)
		printf("%s\n", arr[i]);

	return 0;
}

Which leads me to believe the first code was indeed incorrect. But that's how the TAs taught it to us.. :confused:
And if the first piece of code is indeed wrong, how do I set it up to pass it to execvp?
 

topham

Ars Legatus Legionis
13,756
Subscriptor
You are correct in that they are both wrong.

An array may contain the number of elements as in its definition. char* command[2]; is a 2 element array. element 0, and element 1. There is no element 2, however it is being referenced, and explicitly set to null.

Your second example, being otherwise equivalent is an array of 3 elements, 0 to 2.

I've seen, and argued this point multiple times in the past. The gotcha in C is that you can compile and run this on most older compilers without error. GNU GCC is not an exception in this.
 
Horatio":vka4to6c said:
Well, in your 2nd example, you're writing to the 4th element of arr, which is out of range.

Isn't it out of range in the first example as well?
Both programs compile and run fine with gcc under Linux (goes to show how safe C is).


Thanks topham, I suspected as much. The thing is without this statement:
Code:
command[2] = NULL;

This one produces some weird output.
Code:
execvp(command[0], command);

It's as if execvp was trying to fetch more data than what's on the array. So the TAs told us to just set it to NULL.
 

topham

Ars Legatus Legionis
13,756
Subscriptor
There is a quirk in the definition allowing you to retrieve the address 1 element beyond the array.

&command[2] is valid, however modifying the data at that location is not. (retrieving it shouldn't be either, however without full bounds checking it won't trip any notifications).

Increase the number of elements in the array by 1 if you want the last entry to be null.
 

Jonathon

Ars Legatus Legionis
17,645
Subscriptor
helios01":2ezlmy8g said:
This one produces some weird output.
Code:
execvp(command[0], command);

It's as if execvp was trying to fetch more data than what's on the array. So the TAs told us to just set it to NULL.
execvp takes a null terminated sequence of strings; that's how it tells how long the sequence is. You're not coding in Java; C doesn't retain any information along with the array to tell you how long it is at runtime. Without the null at the end, it won't stop until it either reaches a preset maximum number of elements, it happens to hit a null somewhere at random, or it crashes after trying to dereference a bad pointer.

C-strings themselves work exactly the same way... it's a null-terminated sequence of strings. The compiler hides this from you when you create string literals, so this bit of code:

char *myString = "Hello";

comes out looking like this in memory:

myString = char array of length 6: ['H', 'e', 'l', 'l', 'o', 0]

Remember that, in both cases (the sequence of strings and the sequence of characters), the length of the array always has to include your null element; otherwise, you're going to trash whatever unlucky word of data happens to immediately follow your data and cause bad things to happen.
 

technophile

Ars Legatus Legionis
23,768
helios01":1sgziynv said:
It's as if execvp was trying to fetch more data than what's on the array. So the TAs told us to just set it to NULL.
:( :( :( :(


1. They're teaching you C
2. TAs are idiots.*

This is why comp sci degrees aren't worth the bits generated in their acquisition.

* To be fair, they're probably paid well below idiot payscale and certainly not being paid enough to be actually teaching you anything, so I don't blame them for being idiot TAs, however intelligent they might be in other pursuits.
 

Arakageeta

Ars Scholae Palatinae
729
Subscriptor++
technophile":37u4cb74 said:
To be fair, they're probably paid well below idiot payscale and certainly not being paid enough to be actually teaching you anything, so I don't blame them for being idiot TAs, however intelligent they might be in other pursuits.
Are you sure it couldn't just be a failure to communicate? Few of my TAs ever used better English than Zero Wing.
 
Status
Not open for further replies.