-6

I have a struct with two pointers and an int variable. For some reason I am getting a segmentation fault at the line ptr->i=0;. Why is that? I'm pointing to something in the memory, i is not a pointer so it should be legal. Can anyone please explain what is going on with this? I did create memory for the struct and the two char pointers.

struct A_ {
char *a;
char *b;
int i;
};

typdef struct A_ StructA;

And then in my main() I have the following:

StructA *ptr=malloc(sizeof(StructA));
ptr->a=malloc(sizeof(char));
ptr->b=malloc(sizeof(char));

ptr->i=0;
krsteeve
  • 1,794
  • 4
  • 19
  • 29
  • Because `ptr` isn't referencing memory that you've allocated. So your statement is trying to modify some random place in memory. You need to assign `ptr` to a structure of type `A_`. – Jim Mischel Sep 17 '13 at 20:19
  • 1
    `ptr` is uninitialized. You are invoking UB. All hell breaks loose. – syam Sep 17 '13 at 20:19
  • If people are going to downvote the question, please at least post a comment like the people above. – Cloud Sep 17 '13 at 20:21
  • @Dogbert Funny, I didn't downvote... yet. But I prolly should (no research effort AND not useful). – syam Sep 17 '13 at 20:23
  • 2
    The problem with the question, as it is, is that there isn't enough code to provide an answer. The user stated in a comment to another answer that he allocated memory for the structure, so showing us more code may be useful. – Alberto Moriconi Sep 17 '13 at 20:25
  • I don't see how we can help if you don't provide all the relevant code. It could be an invalid malloc size or some memory coruption between the time the malloc is done and ptr->i=0; – Benoit Catherinet Sep 17 '13 at 20:25
  • Actually I'll just VTC: Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance. – syam Sep 17 '13 at 20:25
  • i malloc() the struct, StructA *ptr=malloc(sizeof(StructA)); so shouldnt it be pointing to memory? – user2778481 Sep 17 '13 at 20:26
  • `StructA *ptr = (StructA*)malloc(sizeof(StructA));` is what you should do if you're going to use malloc. Instead, why not use new? You do have the C++ tag, is this a C question? –  Sep 17 '13 at 20:27
  • sorry yes it is a c question... – user2778481 Sep 17 '13 at 20:28
  • 1
    We still need to see a little more. Is there anything more in your `main`? You should show us the smallest program that compiles AND causes the error. You also wrote `typdef` instead of `typedef` in your question; i think this is just a typo or the program wouldn't compile at all. – Alberto Moriconi Sep 17 '13 at 20:35
  • @all above: I agree completely. I just wish more people would provide a comment rather than just downvoting alone. – Cloud Sep 17 '13 at 20:40

3 Answers3

3

You are not pointing to anything in memory, as ptr has not been initialized.

Edit: You're doing something wrong elsewhere in your code. The below compiles and runs to completion. You should model your code off the following:

#include <stdio.h>

typedef struct A {
    int i;
} A;

int main(void) {
    A *a = malloc(sizeof(A));
    a->i = 42;
    printf("%d", a->i);
    free(a);
    return 0;
}

More importantly, I'd suggest taking the time to read about C, memory management, and investing some time with a debugger of your choice.

  • i did malloc memory the sizeof structA for the struct and i also did malloc memory for the two pointers. – user2778481 Sep 17 '13 at 20:20
  • 1
    This isn't clear from your question. I'd recommend you edit your question to include a [SSCE](http://sscce.org/) of your problem. –  Sep 17 '13 at 20:23
1

You are creating a pointer-to-struct, not a struct itself, and you are accessing an uninitialized pointer's member. If you insist on using a pointer-to-struct, you must first create an actual structure for it to point to.

Do this:

StructA myStruct;
StructA* ptr = &myStruct;
ptr->i=0;

All fixed.

Edit:


You don't appear to be familiar with statically allocated memory (my example above) and dynamically allocated memory (ie: using malloc, calloc, etc).

Another method would be:

StructA* ptr = malloc(sizeof(StructA));
if (ptr == NULL) {
    printf("Memory allocation error!\n");
} else {
    /* Malloc succeeded */
    ptr->i=0;
}

Please refer to this post for more info:

Difference between declared string and allocated string

Community
  • 1
  • 1
Cloud
  • 18,753
  • 15
  • 79
  • 153
1

The pieces you provided fit together nicely, with just a little more work:

#include <stdlib.h> /* stdlib.h contains declaration for malloc */

struct A_ {
    char *a;
    char *b;
    int i;
};

typedef struct A_ StructA; /* fixed a typo */

int main(void)
{
    StructA *ptr = malloc(sizeof(StructA));
    ptr->a = malloc(sizeof(char));
    ptr->b = malloc(sizeof(char));

    ptr->i = 0;

    return 0;
}

There probably is an error somewhere else in your program; have you assigned another value to ptr before the ptr->i = 0; line?

Alberto Moriconi
  • 1,645
  • 10
  • 17