Segfault writing pointer-to-struct to file.

This is a discussion on Segfault writing pointer-to-struct to file. within the C++ Programming forums, part of the General Programming Boards category; It's something like this: Code: MyStruct *mystructvar; cin << mystructvar->value; file >> mystructvar->value; When the program gets to the iofile ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    4

    Segfault writing pointer-to-struct to file.

    It's something like this:

    Code:
    MyStruct *mystructvar;
    
    cin << mystructvar->value;
    
    file >> mystructvar->value;
    When the program gets to the iofile line, segmentation fault. I was never good at pointers. What am I doing wrong?

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    You must allocate memory to a pointer first (unless you point to it to memory you've already allocated).
    MyStruct* mystructvar = new MyStruct;
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    4
    Man, that's all? Now I'm embarrased that I spent an hour trying to fix this problem before I posted here.

    I knew that. I did, honest.



    Thanks for the help, by the way.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Replies: 2
    Last Post: 05-09-2008, 07:27 AM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  4. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21