Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#include<stdarg.h>
#include<stdio.h>
#include<malloc.h>
#include<process.h>
struct Array
{
    unsigned int bytes;
    void * type;
    struct Array *next;
};

class arr
{
    private:
     Array *element;
     Array *index;
     Array *temp;
     unsigned int arr_size;
    public:
     arr(unsigned int,...);
     void enter_element(unsigned int,...);
};


//constructor
arr::arr(unsigned int t,...)
{
    va_list ap;
    int arg;
    temp=index;
    va_start(ap,t);
    while((arg=va_arg(ap,int))!=0)
    {
        index->bytes=t;
        *(int *)index->type=arg;
        index->next=(Array *)0;
        index=index->next;
    }
    va_end(ap);
    index=temp;
    arg=1;
    while(index->next!=(Array *)0)
    {
        arg=arg*(*(int *)index->type);
        index=index->next;
    }
    index=temp;
    arr_size=(unsigned int)arg;
    temp=element;
    while((arg--)>0)
    {
        element->bytes=t;
        element->type=malloc(t*8);
        if(element->type==(void *)0)
            exit(0);
        switch(t)
        {
         case 2 : *(int*)element->type=0;break;
         case 8 : *(double*)element->type=0.00;break;
        }
        element->next=(Array *)0;
        element=element->next;
    }
    element=temp;
}


//method to enter a array
void arr :: enter_element(unsigned int t1,...)
{
    va_list ap;
    temp=element;
    va_start(ap,t1);
    while(element->next!=((Array *)0))
    {
        switch(t1)
        {
            case 2 : *(int *)element->type=va_arg(ap,int);break;
            case 8 : *(double *)element->type=va_arg(ap,double);break;
        }
        element=element->next;
    }
    va_end(ap);
    element=temp;
}
int main()
{
    arr arr1(2,2,3,0);
}


What I have tried:

class array same as java has but for use in c++ program
Posted
Updated 24-Mar-16 8:06am
v2
Comments
ZurdoDev 24-Mar-16 10:56am    
What is the error? And what is your question?
Ravinder Singh 24-Mar-16 10:59am    
abnormal terminations under the gcc compiler.can anyone could debug my code??
ZurdoDev 24-Mar-16 11:07am    
Probably not.
Ravinder Singh 24-Mar-16 11:10am    
i new where error is. its in line where index->bytes=t;
but i dont know why
ZurdoDev 24-Mar-16 11:12am    
Good. Click improve question and add this information and even more importantly, put what the error is.

1 solution

On this line in the constructor of arr:
C++
index->bytes=t;

..you're attempting to de-reference index before it has been set to point at some instance of Array.

You should learn to use a debugger. It's invaluable.
 
Share this answer
 
Comments
Ravinder Singh 26-Mar-16 2:40am    
please provide link for learning debugger
Sascha Lefèvre 26-Mar-16 6:48am    
I've never worked with gcc; don't know if there's a graphical debugger available. If you have a Windows PC then you could use Visual Studio instead:
https://www.visualstudio.com/en-us/products/visual-studio-community-vs.aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900