0

i am starting homework about dynamic array, first, I have a 2 dimensional array :

int initializeInfo[3][4] ={{77,68,0,0},{96,87,89,78},{70,90,86,0}};

and use pointer to store it:

int **ptr = (int**)malloc(3*sizeof(int));
int size = 0;
for(int i =0;i<3;i++){
    addInitiazeInfo(ptr,initializeInfo[i],size);
}

here is function addInitiazeInfo:

void addInitiazeInfo(int**& ptr, int arr[],int& size){
    ptr[size] = (int*)malloc(4*sizeof(int));
    if(ptr[size] == NULL){
        return;
    }
    ptr[size] = arr;
    size++;
}

It's run OK! The 2 dimensional array is store by ptr pointer.enter image description here

And I want to add new row, I think realloc is needed, then I try:

int arr[] = {3,4,5,6};
size++;            
ptr = (int**)realloc(ptr,size * sizeof( int ) );
ptr[size-1] = (int*)malloc(4*sizeof(int));
ptr[size-1] = arr;

But I think this is my trouble, the output make me hard to know how it happend: enter image description here

please help me, thanks everyone

Joe
  • 7,378
  • 4
  • 37
  • 54
famfamfam
  • 396
  • 3
  • 8
  • 31
  • I dont think you realize what `ptr[size] = arr;` is doing – Karthik T Jun 26 '13 at 09:00
  • 3
    `int **ptr = (int**)malloc(3*sizeof(int));` is wrong (and so is the realloc)) . BTW: C does not have references. Maybe you mean C++ ? – wildplasser Jun 26 '13 at 09:02
  • thank you wildplasser,can you tell my more about my problem.I code according to my syllabus. And no one teach me about this problem – famfamfam Jun 26 '13 at 09:06
  • Tagging a realloc/malloc question C++ makes me, well, *sigh* – PlasmaHH Jun 26 '13 at 09:08
  • 1
    The defined type for ptr is `int **ptr;`, so it points to a pointer, (or to the first member of an array of pointers) So, you should allocate `3 * sizeof (int*)` chars for it. AND: mixing C and C++ _really_ is a bad habit. – wildplasser Jun 26 '13 at 09:15
  • thanks you T^T. May be i am a bit stupid, my problem is why my output is not successful as I think, can you explain more about my problem – famfamfam Jun 26 '13 at 09:30
  • When your program contains `**&` it doesn't matter whether you make it work or not, your program is unreadable either way. – Sebastian Redl Jun 26 '13 at 09:43

1 Answers1

2

When you do

ptr[size] = arr;

You are essentially assigning the address of arr, to ptr[size]. This means that the memory you just allocated is lost and successfully leaked.

You want to manually copy element by element or use something like memcpy. It is likely this might fix your issue, depending on the rest of your code.

Karthik T
  • 31,456
  • 5
  • 68
  • 87
  • thanks you for your ideal, I am done this homework by using 2 dimension array or struct ( not use pointer), but I want to improve my skill so I take myself to something that more harder ( It is my opinion), I will note your ideal. And so you can tell my some more about my problem, that is I do not know why my pointer can not add new array by using realloc. kaka – famfamfam Jun 26 '13 at 09:28
  • @fukemyfukemy take a look at http://stackoverflow.com/questions/8287109/how-to-copy-one-integer-array-to-another for details on how to copy arrays, which is what you want to do. Can you tell me if this fixes it? I think we need to see more of your code to see why it fails – Karthik T Jun 26 '13 at 09:31
  • Ok i reading this page, here is full code, and i am very appologize for forgotting comment some code T^T. http://www.mediafire.com/download/jyd997ku1tt676d/myCode.txt – famfamfam Jun 26 '13 at 09:41
  • 1
    @fukemyfukemy yes, as i guessed, your new array is within the function, so the pointer is invalid once you leave that function, hence you are getting junk (officially undefined behaviour). You should fix your code to do a copy as I have linked. – Karthik T Jun 26 '13 at 09:46