Re: malloc for members of a structure and a segmentation fault
- From: Flash Gordon <spam@xxxxxxxxxxxxxxxxxx>
- Date: Mon, 15 Sep 2008 23:08:40 +0100
jbholman wrote, On 15/09/08 21:44:
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>
This is not only non-standard but also not required for your program.
#include <stdio.h>
#include <string.h>
#include <unistd.h>
This is also non-standard but not required.
#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 */
When you do actually get around to calling execve, *then* your program will be non-standard and require some non-standard headers. At that point you will need to start asking on comp.unix.programmer. For now, however, your question is topical here.
};
int main (void) {
/* function declarations */
struct sample **initialize(struct sample **samplist);
void test (struct sample **samplist);
It would be better to put the above declaration before the start of main. Where they are the compiler is not required to complain if they don't match the function definitions.
/* neener is a pointer to a pointer of type sample* */
struct sample **neener;
neener = initialize(neener);
Why are you passing needer as a parameter? See question 4.8 of the comp.lang.c FAQ at http://c-faq.com/
Note that in the answer it would be better to have "void f(int **ipp)" for the example rather than "void f(ipp) inst **ipp;".
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 *));
You don't need the cast and there is an easier way to use sizeof. Try
samplist = malloc (MAXSAMPLES * sizeof *samplist);
/* set sp to the the first pointer allocated to samplist */
sp = *samplist;
Here is where you start going wrong. sp is set to the current *value* of *samplist (which is not initialised, so this is wrong), but changing it will only change the variable sp, not *samplist. What you are doing is like the following silly example...
int foo;
int barr;
barr = foo;
barr = 10; -- Here you are assuming that foo is changed as well as barr.
/* 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));
This is the point where you expect *samplist to be changed, but of course it isn't because you have not changed it.
<snip>
You probably have other errors. I recommend that you read through al of sections 4, 5, 6 and 7 of the comp.lang.c FAQ as you probably have a lot of other miss-conceptions covered by those sections.
--
Flash Gordon
.
- References:
- malloc for members of a structure and a segmentation fault
- From: jbholman
- malloc for members of a structure and a segmentation fault
- Prev by Date: Re: malloc for members of a structure and a segmentation fault
- Next by Date: Re: Any way to take a word as input from stdin ?
- Previous by thread: Re: malloc for members of a structure and a segmentation fault
- Next by thread: Re: malloc for members of a structure and a segmentation fault
- Index(es):
Relevant Pages
|