C Pointers Problems

  • 14 years ago
    I am having very weired, and most probably very simple problems (but I am too blind to see it so far) with pointers indirection levels, memory allocation and freeing,
    things like in one file:
      mystructtype * p = NULL;

      initStruct (&p);

    then the function is:
    void initStruct (mystructtype * * p {
      (*p) = (mystructtype *) malloc(sizeof(mystructtype)); // it blows up here
        if ((*p) == NULL)
            printf ("couldn't create struct\n");
        // I do other initliazation for the structure elements, like other pointers

    and mystructtype is
    typedef struct tag_estruct  {
      long elscalar;
      unsigned long eulscalar;
      unsigned long * eularray;
    } estruct ;

    typedef struct tag_mystructtype{
      long lscalar;
      long * lpscalar;
      unsigned long ulscalar;
      unsigned long * ulparray; 
      estruct * epstruct; 
    } mystructtype;

    It is actually working in a multhreaded/multiprocess program, and I needed to test it in isolation with other things in a sequential program, but with the full data structure around it, and it is blowing up now,
    I have another similar problem, about allocating memory for an array encabsulated in dynamically allocated structure as well, this time the structure is allocated fine, but then it blows up when I allocate array of 5 integers for example, work most of the time, and then blow up with some test cases,

    for example:

    myrecType * myrec;

    myrec = (myrecType *) malloc (sizeof(myrecType));

    myrec->arrayofinterger  = NULL; // I tried removing this one, but it is still blows up
    myrec->arrayofinterger = (int *) malloc (sizeof(int) * 5); // it blows up here

    Also, so many problems with blowing up when freeing memory, I removed some of the free statements, to keep going, but I think that's why I have problems, even on smaller isolated tests, I still get these,
    I heard before about structure hacking in C, and not sure if this is related, but I can not find online references about this kind of memory allocations and passing arguments by several levels of indirection to allocate somewhere, and keep using the same memory location without copying to temporaries,

    I try on several gcc compiler, and not sure if the compiler version is what causes it, but 2 compilers now have given me these:

    gcc (GCC) 4.1.1 20060525 (Red Hat 4.1.1-1)
    gcc (GCC) 3.4.6 20060404 (Red Hat 3.4.6-3)

    I appreciate any help I can get to sort this out, like sample code that can do similar things, or links to tutorials that describe similar functions, not basic pointers exmaples,

  • 14 years ago
    Couldn't you just use the new keyword? That should allocate a structure. Either way, both methods work for me, so I don't know what you're doing wrong. Also, you forgot a ')' at the definition of 'initStruct'

Post a reply

Enter your message below

Sign in or Join us (it's free).


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Beware of bugs in the above code; I have only proved it correct, not tried it.” - Donald Knuth