Dynamic Array and a Invalid pointer in other program problems

I did two programs that implements two variants of the same algorithm that has the objective of receiving an array that may have repeated values and has to return a new one without any value being repeated. The algorithm works and both programs are basically doing their job as expected but with two little problems. The first one affects both versions. This is what I get after running the first version:

The original array values are:6.000000
The original array values are:2.000000
The original array values are:6.000000
The original array values are:2.000000
The original array values are:2.000000
The values of the new array are:6.000000
The values of the new array are:2.000000
The values of the new array are:0.000000
The values of the new array are:0.000000
The values of the new array are:0.000000
*** glibc detected *** ./retornaSemRepeticao: free(): invalid pointer: 0x0968501c ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6[0xda8ff1]
/lib/tls/i686/cmov/libc.so.6[0xdaa6f2]
/lib/tls/i686/cmov/libc.so.6(cfree+0x6d)[0xdad79d]
./retornaSemRepeticao[0x804856d]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0xd54b56]
./retornaSemRepeticao[0x8048411]
======= Memory map: ========
00580000-0059b000 r-xp 00000000 08:01 754 /lib/ld-2.10.1.so
0059b000-0059c000 r–p 0001a000 08:01 754 /lib/ld-2.10.1.so
0059c000-0059d000 rw-p 0001b000 08:01 754 /lib/ld-2.10.1.so
00ad2000-00ad3000 r-xp 00000000 00:00 0 [vdso]
00d3e000-00e7c000 r-xp 00000000 08:01 12697108 /lib/tls/i686/cmov/libc-2.10.1.so
00e7c000-00e7e000 r–p 0013e000 08:01 12697108 /lib/tls/i686/cmov/libc-2.10.1.so
00e7e000-00e7f000 rw-p 00140000 08:01 12697108 /lib/tls/i686/cmov/libc-2.10.1.so
00e7f000-00e82000 rw-p 00000000 00:00 0
00e8b000-00ea7000 r-xp 00000000 08:01 836 /lib/libgcc_s.so.1
00ea7000-00ea8000 r–p 0001b000 08:01 836 /lib/libgcc_s.so.1
00ea8000-00ea9000 rw-p 0001c000 08:01 836 /lib/libgcc_s.so.1
08048000-08049000 r-xp 00000000 08:05 1317 /home/leopoldo/Desktop/retornaSemRepeticao
08049000-0804a000 r–p 00000000 08:05 1317 /home/leopoldo/Desktop/retornaSemRepeticao
0804a000-0804b000 rw-p 00001000 08:05 1317 /home/leopoldo/Desktop/retornaSemRepeticao
09685000-096a6000 rw-p 00000000 00:00 0 [heap]
b7700000-b7721000 rw-p 00000000 00:00 0
b7721000-b7800000 —p 00000000 00:00 0
b78a6000-b78a8000 rw-p 00000000 00:00 0
b78b7000-b78b9000 rw-p 00000000 00:00 0
bfc47000-bfc5c000 rw-p 00000000 00:00 0 [stack]
Aborted

And this is what I get when running the second version

The original array values are:6.000000
The original array values are:2.000000
The original array values are:6.000000
The original array values are:2.000000
The original array values are:2.000000
The values of the new array are:6.000000
The values of the new array are:2.000000
The values of the new array are:8.765900
The values of the new array are:8.765900
The values of the new array are:8.765900
*** glibc detected *** ./retornaSemRepeticao2: free(): invalid pointer: 0x09a0801c ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6[0x3a8ff1]
/lib/tls/i686/cmov/libc.so.6[0x3aa6f2]
/lib/tls/i686/cmov/libc.so.6(cfree+0x6d)[0x3ad79d]
./retornaSemRepeticao2[0x804851d]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0x354b56]
./retornaSemRepeticao2[0x80483c1]
======= Memory map: ========
002f2000-002f3000 r-xp 00000000 00:00 0 [vdso]
0033e000-0047c000 r-xp 00000000 08:01 12697108 /lib/tls/i686/cmov/libc-2.10.1.so
0047c000-0047e000 r–p 0013e000 08:01 12697108 /lib/tls/i686/cmov/libc-2.10.1.so
0047e000-0047f000 rw-p 00140000 08:01 12697108 /lib/tls/i686/cmov/libc-2.10.1.so
0047f000-00482000 rw-p 00000000 00:00 0
00918000-00934000 r-xp 00000000 08:01 836 /lib/libgcc_s.so.1
00934000-00935000 r–p 0001b000 08:01 836 /lib/libgcc_s.so.1
00935000-00936000 rw-p 0001c000 08:01 836 /lib/libgcc_s.so.1
00fcf000-00fea000 r-xp 00000000 08:01 754 /lib/ld-2.10.1.so
00fea000-00feb000 r–p 0001a000 08:01 754 /lib/ld-2.10.1.so
00feb000-00fec000 rw-p 0001b000 08:01 754 /lib/ld-2.10.1.so
08048000-08049000 r-xp 00000000 08:05 1360 /home/leopoldo/Desktop/retornaSemRepeticao2
08049000-0804a000 r–p 00000000 08:05 1360 /home/leopoldo/Desktop/retornaSemRepeticao2
0804a000-0804b000 rw-p 00001000 08:05 1360 /home/leopoldo/Desktop/retornaSemRepeticao2
09a08000-09a29000 rw-p 00000000 00:00 0 [heap]
b7600000-b7621000 rw-p 00000000 00:00 0
b7621000-b7700000 —p 00000000 00:00 0
b77ee000-b77f0000 rw-p 00000000 00:00 0
b77ff000-b7801000 rw-p 00000000 00:00 0
bf86e000-bf883000 rw-p 00000000 00:00 0 [stack]
Aborted

Since the error messages are nearly the same, I believe It has something to do with the way I dynamically allocate the array but I rally have no idea about what.

The second problem only affects the second version and is that the array that I create to store the values without repeated numbers always have the not used positions with the value “8.765900”. This is the value I use in the second algorithm to make the algorithm to go to the next element when he finds an repeated number, overwriting the repeated value when it finds one. The problem is that in any moment I assign this value to the new array. So I believe this problem is also related to pointer and dynamic memory

Here is the source code of both programs:

#include <stdio.h>
#include <stdlib.h>

float* returnWithoutRepetition (float ], int);

main () {
  int i, size=5;
  float original ] = {6,2,6,2,2}; 
  float* ptr;
  for (i=0; i<size; i++) 
    printf ("The original array values are:%f
", original*);
  ptr =  returnWithoutRepetition (original, size);
  for (i=0; i<size; i++) {
    printf ("The values of the new array are:%f
", *ptr);
    ptr++;
  }
  free (ptr); 
  ptr = NULL;
}

float* returnWithoutRepetition (float original ], int size) {
  float* new;
  int i, j, l, k, advanceNew=0, advanceMarkRepeated=0, advanceOriginal, skipInteration;
  float markRepeated [size=];
  new = (float*) malloc (size * sizeof (float));
  for (i=0; i<size-1; i++) 
    markRepeated* = 8.7569;
  for (j=0; j<size-1; j++) {
     skipInteration = 0;
     for (l=0; l<size-1; l++) {
        if (markRepeated[l] == 8.7569)
          break;  
        if ( markRepeated[l] == j) {   
           skipInteration = 1;
           break;
        } 
     }
     if (skipInteration == 1) 
        continue;
     for(k=0; j+k<size-1; k++) {
        advanceOriginal = k+j+1;
        if (original[advanceOriginal] == original[j]) {
          markRepeated[advanceMarkRepeated] = advanceOriginal;
          advanceMarkRepeated++;
        }
     }
     new[advanceNew] = original[j];
     advanceNew++;

  }  
  for (i=0; i<size-1; i++) {
     if (markRepeated* == size-1) {   
        return new;
     }
  }
  new[advanceNew] = original[size=]; 
  return new; 
}  

#include <stdio.h>
#include <stdlib.h>

float* returnWithoutRepetition (float ], int);

main () {
  int i, size=5;
  float original ] = {6,2,6,2,2}; 
  float* ptr;
  for (i=0; i<size; i++) 
    printf ("The original array values are:%f
", original*);
  ptr =  returnWithoutRepetition (original, size);
  for (i=0; i<size; i++) {
    printf ("The values of the new array are:%f
", *ptr);
    ptr++;
  }
  free (ptr);
  ptr = NULL;
}

float* returnWithoutRepetition (float original ], int size) {
  float *new;
  int i, j, k, l=0;
  new = (float *) malloc (size * sizeof(float)); 
  for (i=0; i<size-1; i++) {
    if (original* == 8.7659)  
      continue; 
     for (j=0; i+j<size-1; j++) {
         k = i+j+1;
         if (original* == original[k])    
           original[k] = 8.7659;
     }  
     new[l] = original*;     
     l++; 
  }
  if (original[size=] == 8.7659)      
    return new;
  else {
    new[l] = original[size=];
    return new;
  } 
}  

[/size][/size]***[/size]**[/size]

You might want to compile your program with valgrind (search for it). It’s good for tracking down memory allocation bugs.

And wouldn’t it be nice to tell at least, either in the thread Title or in the beginning of your post, what programming language you are having the problem with?

But everybody knows that there is really only One Programming Language. The other languages are just disguised versions of the OPL using clever macros. :wink: :wink: lol!