Thread: Problem with DMA inside a struct pointer

  1. #1
    Registered User
    Join Date
    Apr 2007

    Problem with DMA inside a struct pointer


    I'm working on a project at the moment and i have run into some trouble.

    I have a struct defined as such:

    typedef struct{
    	int points;
    	float *time, *dhdt, *drate, *diff;
    } simData;
    then in my main function I have this code:

    simData *data;
    data->points = 1800;
    data->time = (float *)malloc(points*sizeof(float));
    data->dhdt = (float *)malloc(points*sizeof(float));
    data->drate = (float *)malloc(points*sizeof(float));
    data->diff = (float *)malloc(points*sizeof(float));
    Everytime I try and run this code, the program crashes. Can anyone help me unearth why this is happening?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Have you tried allocating data?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Apr 2007
    Can you give me an example? Not quite sure what you mean.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    As in
    data = malloc ( sizeof *data );

    before doing any data->member things.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    And see about not casting malloc.

    > simData *data;
    data is a pointer of simData type that points to nothing (in this case, it points anywhere - including potential memory which you don't own), where do you expect the variables you're setting to go? You haven't allocated any memory for them...

  6. #6
    Registered User
    Join Date
    Apr 2007
    Yes I realised this last night. I feel really stupid for forgetting something as simple as that. Thanks everybody.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  4. Bi-Directional Linked Lists
    By Thantos in forum C Programming
    Replies: 6
    Last Post: 12-11-2003, 10:24 AM
  5. Struct inside a Struct inside a Struct?!?
    By tegwin in forum C++ Programming
    Replies: 8
    Last Post: 05-02-2003, 11:50 AM