views:

726

answers:

5

Hi,

  1. I have this function which is called about 1000 times from main(). When i initialize a pointer in this function using malloc(), seg fault occurs, possibly because i did not free() it before leaving the function. Now, I tried free()ing the pointer before returning to main, but its of no use, eventually a seg fault occurs.
  2. The above scenario being one thing, how do i initialize double pointers (*ptr) and pointer to array of pointers (ptr[])?
  3. Is there a way to copy a string ( which is a char array) into an array of char pointers. char arr[]; (Lets say there are fifty such arrays) char *ptr_arr[50]; Now i want point each such char arr[] in *ptr_arr[] How do i initialize char *ptr_arr[] here?
  4. What are the effects of uninitialized pointers in C?
  5. Does strcpy() append the '\0' on its own or do we have to do it manually? How safe is strcpy() compared to strncpy()? Like wise with strcat() and strncat().

Thanks.

+1  A: 

From your question, my advice would be to google "pointers in C" and read some tutorials to get an understanding of what pointers are and how to use them - there's a lot that would need to be repeated in an SO answer to get you up to speed.

The top two hits are here and here.

Jason Williams
But the point is that this is the site when people google for a programming problem. If every answer is "Google it" we are not building the knowledge base of the site. That said, the fact that the op is asking 5 related questions in one does not help.
Ed Swangren
Many problems can be solved by googling, but sometimes you really do need to ask someone else who has "already done it" to help. And sometimes you just don't know what to enter into the search box. But I think it's reasonable to expect people to try to research something for a couple of minutes before asking the SO community to type in the content of those websites here. Also, I can't explain pointers much better than the million and one websites that already have descriptions, so I think "look over here" is a perfectly reasonable initial response.
Jason Williams
+1  A: 

It's hard to answer your first question without seeing some code -- Segmentation Faults are tricky to track down and seeing the code would be more straightforward.

Double pointers are not more special than single pointers as the concepts behind them are the same. For example...

char * c = malloc(4);
char **c = &c;

I'm not quite sure what c) is asking, but to answer your last question, uninitialized pointers have undefined action in C, ie. you shouldn't rely on any specific result happening.

EDIT: You seem to have added a question since I replied...

strcpy(..) will indeed copy the null terminator of the source string to the destination string.

Andrew Song
+1  A: 

for part 'a', maybe this helps:

void myfunction(void) {
  int * p = (int *) malloc (sizeof(int));
  free(p);
}

int main () {
  int i;
  for (i = 0; i < 1000; i++)
    myfunction();

  return 0;
}

Here's a nice introduction to pointers from Stanford.

bbg
+4  A: 
  1. Segfault can be caused by many things. Do you check the pointer after the malloc (if it's NULL)? Step through the lines of the code to see exactly where does it happen (and ask a seperate question with more details and code)

  2. You don't seem to understand the relation of pointers and arrays in C. First, a pointer to array of pointers is defined like type*** or type**[]. In practice, only twice-indirected pointers are useful. Still, you can have something like this, just dereference the pointer enough times and do the actual memory allocation.

  3. This is messy. Should be a separate question.

  4. They most likely crash your program, BUT this is undefined, so you can't be sure. They might have the address of an already used memory "slot", so there might be a bug you don't even notice.

Tamás Szelei
A: 

You've added an additional question about strcpy/strncpy.

strcpy is actually safer. It copies a nul terminated string, and it adds the nul terminator to the copy. i.e. you get an exact duplicate of the original string.

strncpy on the other hand has two distinct behaviours:

  • if the source string is fewer than 'n' characters long, it acts just as strcpy, nul terminating the copy
  • if the source string is greater than or equal to 'n' characters long, then it simply stops copying when it gets to 'n', and leaves the string unterminated. It is therefore necessary to always nul-terminate the resulting string to be sure it's still valid:
    char dest[123];
    strncpy(dest, source, 123);
    dest[122] = '\0';
Jason Williams