Memory Access Violation - C Programming
Hi All,
I have problem to access a memory location in 2 dimensional array. code snippet, function (char **strArray) { while (strArray != NULL) { printf ("%s\n", *strArray); strArray++; } } consider strArray holds 5 string value. while (strArray != NULL) not terminating after parsing 5 string values. What was the best check to terminate while loop after parsing 5 string values? Is there any memory validation method available? Thank you. -Mr S |
It is better practice to keep track of the number of elements in your array with a separate variable, and then pass both into the function.
|
Quote:
Quote:
I'm only guessing at the context of your problem, but based on that guess, I think you should modify the original source of the array of pointers so that it always allocates one more than the number of actual pointers and always makes sure that extra one (at the end) is NULL. If the array is static or global, the compiler/linker/loader will make sure the extra is NULL as long as you allocate one more than you initialize. But if the array is stack or heap, you need run time code to set the extra pointer to NULL. |
Try printing just one string
Did you get a segmentation fault when you tried to run it? That is what it looks like you would get. Have you tried to print just one string in the same manner without the loop? I think that you will get a segmentation fault too. Give that a try!
Post the initialization code too. |
Thanks orgcandman,
But, that won't suite best for me. Would you please suggest me more. Thanks. -Mr S |
Thanks Johnsfine,
For string array, I can add NULL end, OK. Think about int array. -1 will be a value, I can't add it at end. Please let me know how can find array is ended. Thanks |
Hi KarlatLQ,
I can print single string without any error. Initialization snippet is, char **strArray; strArray = (char**) malloc (5 * sizeof(char*)); for (i = 0; i < 5; i++) { *strArray = (char*) malloc (5 * sizeof (char)); strArray++; } |
Code:
while (strArray != NULL) /* wrong */ Code:
char **strArray; |
Quote:
I understand you do not want to pass two variables when passing the array. How about: to allocate it Code:
int* my_array = malloc( (desired_size+1)*sizeof(int) ); Code:
free(my_array-1); Code:
function( int* array ) { Quote:
|
Code:
#include <stdio.h> Code:
> gcc file.c |
Thank you guys.
I added NULL at end for string array and -1 at the end for int array. |
It seems to me that you want to use an array with program arguments argv[].?
An array of arguments is defined like this: Code:
char *arg[] = {"progname", "op1", (char)0 }; Code:
while( arg[i] != 0 ) And if not, 0 terminated strings are c-strings and will always be handled right by the system ! With NULL there is expressed the need of a char with value 0 and not the character '0' (with value 48). |
What compiler are you using?
Could you post the code where you set the string values to the array? Could you post a test code to print out just one of the arrays in the string? Quote:
Code:
printf("\nstrArray = %X", strArray); |
Quote:
Example: Quote:
Quote:
|
Quote:
And linux system always needs (char) 0 terminated strings like char *st="hallo" is one, and linux needs also the array to be 0 terminated. NULL only means that it is a pointer to its self and if NULL is used for a call, instead of a function, some POPs and a ret will be added after. So every NULL you use instead of a different function type will be a different NULL ! |
All times are GMT -5. The time now is 12:27 PM. |