J
jbholman
I am pretty new to C and doing my first project in C. I actually read
almost the entire FAQ, but can't seem to figure out this problem.
I have a structure. I have a list of these structures. Inside each
structure, I have two members: a list of strings, and a string.
I have made a sample program below that exhibits the error I am
having. I also read about Valgrind, and used it to tell me where I
was getting the segmentation fault, which is really helpful. The
Valgrind output is below my sample code.
At line 72 (it is labeled below), I use an unitialized value and then,
I try to write to it. I am not sure what I am doing wrong, because I
think I am initializing the value inside my "initialize" routine. Can
anyone help me figure out where I am going wrong.
/********************************** START SAMPLE CODE
**************************************/
#include <stdlib.h>
#include <alloca.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#define MAXSTRINGS 50
#define MAXSTRING 80
#define MAXSAMPLES 50
/* this is my sample structure. its members are a character poitner,
and a pointer to a character pointer */
struct sample {
char *string; /*substitute will fill in full path_name */
char **stringlist; /*list of arguments to send to execve */
};
int main (void) {
/* function declarations */
struct sample **initialize(struct sample **samplist);
void test (struct sample **samplist);
/* neener is a pointer to a pointer of type sample* */
struct sample **neener;
neener = initialize(neener);
test (neener);
}
struct sample **initialize (struct sample **samplist) {
struct sample *sp;
int i, j;
/* allocate enough space for 50 pointers to sample structures */
samplist = (struct sample **) malloc (MAXSAMPLES * sizeof(struct
sample *));
/* set sp to the the first pointer allocated to samplist */
sp = *samplist;
/* for each of samplist's pointers to sample structures, */
for (i = 0; i < MAXSAMPLES; ++i) {
/* allocate enough space for one pointer to the sample structure
*/
sp = (struct sample *) malloc (sizeof (struct sample));
/* allocate enough space of the for a string */
sp->string = (char *) malloc (MAXSTRING * sizeof (char));
/* allocate enough pointers for the stringlist member's pointer*/
sp->stringlist = (char **) malloc (MAXSTRINGS * sizeof (char *));
/* and for each of stringlists pointers, allocate enough space for
a string*/
for (j = 0; j < MAXSTRINGS; ++j)
*(sp->stringlist + j) = (char *) malloc (MAXSTRING *
sizeof(char));
/* increment sp so it points to samplist's next allocated pointer
*/
++sp;
}
return samplist;
}
void test (struct sample **samplist) {
struct sample *sp;
char *string;
sp = *samplist;
string = "Testing 1 2 3";
*(sp->stringlist + 0) = strcpy(*(sp->stringlist + 0), string); /*
line 72 */
/* segmentation fault */
}
/********************************** END SAMPLE CODE
**************************************/
/********************************** START VALGRIND OUTPUT
*******************************/
==18077== Use of uninitialised value of size 4
==18077== at 0x8048494: test (test.c:72)
==18077== by 0x80483CD: main (test.c:30)
==18077==
==18077== Invalid read of size 4
==18077== at 0x8048494: test (test.c:72)
==18077== by 0x80483CD: main (test.c:30)
==18077== Address 0x4 is not stack'd, malloc'd or (recently) free'd
==18077==
==18077== Process terminating with default action of signal 11
(SIGSEGV)
==18077== Access not within mapped region at address 0x4
==18077== at 0x8048494: test (test.c:72)
==18077== by 0x80483CD: main (test.c:30)
==18077==
==18077== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 11 from
1)
==18077== malloc/free: in use at exit: 214,600 bytes in 2,651 blocks.
==18077== malloc/free: 2,651 allocs, 0 frees, 214,600 bytes allocated.
==18077== For counts of detected errors, rerun with: -v
==18077== searching for pointers to 2,651 not-freed blocks.
==18077== checked 60,224 bytes.
/********************************** END VALGRIND OUTPUT
*******************************/
almost the entire FAQ, but can't seem to figure out this problem.
I have a structure. I have a list of these structures. Inside each
structure, I have two members: a list of strings, and a string.
I have made a sample program below that exhibits the error I am
having. I also read about Valgrind, and used it to tell me where I
was getting the segmentation fault, which is really helpful. The
Valgrind output is below my sample code.
At line 72 (it is labeled below), I use an unitialized value and then,
I try to write to it. I am not sure what I am doing wrong, because I
think I am initializing the value inside my "initialize" routine. Can
anyone help me figure out where I am going wrong.
/********************************** START SAMPLE CODE
**************************************/
#include <stdlib.h>
#include <alloca.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#define MAXSTRINGS 50
#define MAXSTRING 80
#define MAXSAMPLES 50
/* this is my sample structure. its members are a character poitner,
and a pointer to a character pointer */
struct sample {
char *string; /*substitute will fill in full path_name */
char **stringlist; /*list of arguments to send to execve */
};
int main (void) {
/* function declarations */
struct sample **initialize(struct sample **samplist);
void test (struct sample **samplist);
/* neener is a pointer to a pointer of type sample* */
struct sample **neener;
neener = initialize(neener);
test (neener);
}
struct sample **initialize (struct sample **samplist) {
struct sample *sp;
int i, j;
/* allocate enough space for 50 pointers to sample structures */
samplist = (struct sample **) malloc (MAXSAMPLES * sizeof(struct
sample *));
/* set sp to the the first pointer allocated to samplist */
sp = *samplist;
/* for each of samplist's pointers to sample structures, */
for (i = 0; i < MAXSAMPLES; ++i) {
/* allocate enough space for one pointer to the sample structure
*/
sp = (struct sample *) malloc (sizeof (struct sample));
/* allocate enough space of the for a string */
sp->string = (char *) malloc (MAXSTRING * sizeof (char));
/* allocate enough pointers for the stringlist member's pointer*/
sp->stringlist = (char **) malloc (MAXSTRINGS * sizeof (char *));
/* and for each of stringlists pointers, allocate enough space for
a string*/
for (j = 0; j < MAXSTRINGS; ++j)
*(sp->stringlist + j) = (char *) malloc (MAXSTRING *
sizeof(char));
/* increment sp so it points to samplist's next allocated pointer
*/
++sp;
}
return samplist;
}
void test (struct sample **samplist) {
struct sample *sp;
char *string;
sp = *samplist;
string = "Testing 1 2 3";
*(sp->stringlist + 0) = strcpy(*(sp->stringlist + 0), string); /*
line 72 */
/* segmentation fault */
}
/********************************** END SAMPLE CODE
**************************************/
/********************************** START VALGRIND OUTPUT
*******************************/
==18077== Use of uninitialised value of size 4
==18077== at 0x8048494: test (test.c:72)
==18077== by 0x80483CD: main (test.c:30)
==18077==
==18077== Invalid read of size 4
==18077== at 0x8048494: test (test.c:72)
==18077== by 0x80483CD: main (test.c:30)
==18077== Address 0x4 is not stack'd, malloc'd or (recently) free'd
==18077==
==18077== Process terminating with default action of signal 11
(SIGSEGV)
==18077== Access not within mapped region at address 0x4
==18077== at 0x8048494: test (test.c:72)
==18077== by 0x80483CD: main (test.c:30)
==18077==
==18077== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 11 from
1)
==18077== malloc/free: in use at exit: 214,600 bytes in 2,651 blocks.
==18077== malloc/free: 2,651 allocs, 0 frees, 214,600 bytes allocated.
==18077== For counts of detected errors, rerun with: -v
==18077== searching for pointers to 2,651 not-freed blocks.
==18077== checked 60,224 bytes.
/********************************** END VALGRIND OUTPUT
*******************************/