-2

I am trying to print out the information from pID pointer that is located in Objects.h header file. Whatever I try to do I get the "Exception thrown: read access violation. p was 0xCEDECEDF." error in Visual Studio 2017. In other words it is unable to read memory. This is the code part from main.cpp file. The error comes in from the For cycle where I am trying to print out the info. I have debugged the code with no success.

#include "stdio.h"
#include "DateTime.h"
#include "Objects.h"
#include "Headers.h"
#include "Structs.h"
#include "malloc.h"
#pragma warning ( disable : 4996 )  //Disables some errors that might occure

void PrintObjects(Object9 **ppStruct1);
//int InsertNewObject(HeaderA **ppStruct1, char *pNewID, int NewCode);
//Object9* RemoveExistingObject(HeaderA **pStruct1, char *pExistingID);

int main()
{
    Object9 **pStruct1 = (Object9 **)GetStruct1(9, 35);

    Object9 **p = pStruct1;
    int i;
    for (i = 0; i < 35; i++)
    {
        printf("%p\n", *(p + i));
        printf("%s\n", (*(p + i))->pID); //This is the line where the error 
        //occures
    }

    if (pStruct1 == NULL)
        printf("pStrcut on NULL\n");
    else
        printf("Ok\n");
    PrintObjects(pStruct1);

    return 0;
}

This is the part from the Objects.h file.

typedef struct ob9
{  // formatting string for printf is "%s %lu %02d %s %04d\n", the result is 
      for example "Abcde 100 01 Detsember 2010"
   // or "Abcde Fghij 100 01 Detsember 2010"
    char *pID;
    unsigned long int Code;
    Date3 *pDate3; // Declaration of Date3 is in file DateTime.h
    struct ob9 *pNext;
} Object9;

The GetStruct1 function comes from the Structure.h header file. I don't know if this helps to determine the problem or not, but just in case I'm also adding the entire Structs.h file code. I need to use: "void **GetStruct1(int ObjectType, int nObjects);" function.

//
//            Structures: function prototypes
//            -------------------------------

void **GetStruct1(int ObjectType, int nObjects);
HeaderA *GetStruct2(int ObjectType, int nObjects);
HeaderB *GetStruct3(int ObjectType, int nObjects);
HeaderC *GetStruct4(int ObjectType, int nObjects);
HeaderA **GetStruct5(int ObjectType, int nObjects);
HeaderA *GetStruct6(int ObjectType, int nObjects);
HeaderD *GetStruct7(int ObjectType, int nObjects);

/* ObjectType - 1, 2,...10 - type of objects to be inserted into the data 
 structure.
   nTypes -number of objects to be inserted into the data structure.
   ObjectType and nObjects are specified by the instructor.
   Return value - pointer to the first byte of data structure.

   Examples for structure from 25 objects of type 4:
   Object4 **pStruct1 = (Object4 **)GetStruct1(4, 25);
   HeaderA *pStruct2 = GetStruct2(4, 25);
   HeaderB *pStruct3 = GetStruct3(4, 25);
   HeaderC *pStruct4 = GetStruct4(4, 25);
   HeaderA **pStruct5 = GetStruct5(4, 25);
*/

I can print out the address but not the info I need. I know I am doing something wrong but don't know exactly what. Any help would be appreciated.

EDIT: Changed the code a bit.

1 Answers1

0

The problem is that you're overwriting the pointer returned by GetStruct1 with a new pointer from malloc() and this points to uninitialized data, not the array of structures. Get rid of the line:

pStruct1 = (Object9 **)malloc(sizeof(Object9 **));

You also don't need this line:

p = (Object9 *)malloc(sizeof(Object9));

since you immediately overwrite p with:

p = *pStruct1;

You're also using p wrong. pStruct1 is a pointer to an array of pointers. When you set p as above, it's set to the first pointer in the array. Adding to p doesn't get you the next pointer in the array, it points outside the object that p pointed to. You need to set p to pStruct, add to this, and then indirect through that to get the pointer in the array.

Object9 **p = pStruct1;
for (int i = 0; i < 35; i++) }
    printf("%p\n", *(p + i));
    printf("%s\n", (*(p + i))->pID);
}        
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thanks. But now I have this error: "Exception thrown: read access violation. p was 0x1110112." in the same line where I am trying to print it out. – Sander Ts. Nov 15 '17 at 19:48
  • I've updated the answer to show the correct way to use `p`. – Barmar Nov 15 '17 at 19:53
  • p = pStruct1 gives this error C2440: '=': cannot convert from 'Object9 **' to 'Object9 *' – Sander Ts. Nov 15 '17 at 19:58
  • It should be declared `Object9 **`. – Barmar Nov 15 '17 at 19:59
  • The first line now gives this error C2040: 'p': 'Object9 **' differs in levels of indirection from 'Object9 *' – Sander Ts. Nov 15 '17 at 20:02
  • You didn't change the declaration as I said. – Barmar Nov 15 '17 at 20:03
  • I did exactly like you wrote, but still getting errors: Exception thrown at 0x01053BE5 in Project1.exe: 0xC0000005: Access violation reading location 0x00000000. I guess I have to find another ways to do this. I really appreciate the help. – Sander Ts. Nov 15 '17 at 20:28