Thread: Dynamic memory Allocation

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    10

    Dynamic memory Allocation

    Hi All

    Code:
    struct abc {
    int a;
    double b;
    unsigned int c;
    }
    
    struct abc* data;
    
    main int {
    ...
    data = (struct data*)malloc(5*sizeof(struct data));
    ...
    }
    So can data include 5 sets of a,b,c correctly?

    Seems I get segmentation fault, when I include dynamic allocation instead of fixed array.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You probably should get a few compile errors instead. Post the smallest and simplest compilable program that demonstrates the segmentation fault.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    10
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    struct abc {
    int a;
    double b;
    unsigned int c;
    }
    
    struct abc* data;
    int i;
    
    int main () {
       data = (struct abc*)malloc(5*sizeof(struct abc));
       for (i =0; i <5; i++) {
          data[i].a = i; data[i].b = i; data[i].c = 0.1;
      }   
    for (i =0; i <5; i++) {
          printf("%d %d %f\n", data[i], data[i].b, data[i].c);
      }   
      return 0;
      }
    it gives me an error in line of "struct abc* data;": two or more data type declaration.

    Why?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ah, you forgot to end your struct definition with a semi-colon, e.g.,
    Code:
    struct abc {
        int a;
        double b;
        unsigned int c;
    };
    By the way, avoid global variables and indent your code better.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    10
    Thank you~

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Memory Allocation
    By slash.hack in forum C Programming
    Replies: 9
    Last Post: 09-30-2011, 04:31 AM
  2. Dynamic Memory Allocation
    By schifers in forum C Programming
    Replies: 12
    Last Post: 05-14-2008, 01:49 PM
  3. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 05:04 PM
  4. Dynamic Memory Allocation?
    By motocross95 in forum C++ Programming
    Replies: 11
    Last Post: 12-03-2002, 08:52 PM
  5. dynamic memory allocation - Please help
    By Space_Cowboy in forum C++ Programming
    Replies: 5
    Last Post: 11-13-2002, 05:20 PM