Problem with DMA inside a struct pointer

This is a discussion on Problem with DMA inside a struct pointer within the C Programming forums, part of the General Programming Boards category; Hi, I'm working on a project at the moment and i have run into some trouble. I have a struct ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    3

    Problem with DMA inside a struct pointer

    Hi,

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

    I have a struct defined as such:

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

    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
    Posts
    5,006
    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
    Posts
    3
    Can you give me an example? Not quite sure what you mean.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,752
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    And see http://faq.cprogramming.com/cgi-bin/...&id=1043284351 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
    Posts
    3
    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-19-2006, 12:23 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  3. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 02: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, 12:50 PM

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