Re: malloc for members of a structure and a segmentation fault



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
.



Relevant Pages

  • Re: confusion: casting function pointers
    ... pointer from the 'actual/other modules' that takes arguments of type ... list to types of void *). ... int main{ ... without a prototype, a number of special "promotion" rules take ...
    (comp.lang.c)
  • Re: invalid pointer adress
    ... >> pointer causes the program to exit with a core dump. ... struct s2{void *p;}; ... int leseExterneHinweise_masch_storno ... typedef struct s_AusdatFeldbeschreibung ...
    (comp.lang.c)
  • Should io(read|write)(8|16|32)_rep take (const|) volatile u(8|16|32) __iomem *addr?
    ... the destination pointer on user-space ... @src: ... const void *data, int bytelen); ...
    (Linux-Kernel)
  • Re: function pointer help!
    ... //the return void and input prameters are defined in the manual... ... void MyProjectView::CallHandler(int,unsigned int, unsigned int, void*) ... You are attempting to use a C++ member function as the callback, but the callback is defined in terms or C, not C++. ... The underlying problem is that C++ functions receive a hidden parameter, the 'this' pointer, so their signature is incompatible with C definitions. ...
    (microsoft.public.vc.mfc)
  • Re: how to add at the end of the linked list
    ... Just pass the pointer, no need to pass it by reference. ... > int prev; ... C++ Faq: http://www.parashift.com/c++-faq-lite ... http://www.josuttis.com -- C++ STL Library book ...
    (comp.lang.cpp)