-1

I try to practice generic functions writing this function:

void *scramble(void *arr, int ElemSize, int n, int *indArr); But whenever I run the program with a debugger it crashes on the prototype line, using F11 (Step Into) I see the following:

1.

#else  /* WPRFLAG */
            __initenv = envp;
            mainret = main(argc, argv, envp);
#endif  /* WPRFLAG */

2.

if ( !managedapp )
    exit(mainret);

And then the console message says that: The program '[8108] 1.exe: Native' has exited with code 0 (0x0).

This is my program (please ignore logical coding mistakes as I want to solve it on my own):

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

typedef char BYTE;

void *scramble(void *arr, int ElemSize, int n, int *indArr);

void main()
{
    int indArr[5]={5,4,3,2,1};
    int numbers[5]={1,2,3,4,5};
    char letters[5]={'a','b','c','d','e'};

    int *newNum;
    char *newLet;

    newNum = (int*)scramble(numbers, sizeof(int), 5, indArr);
    newLet = (char*)scramble(letters, sizeof(char), 5, indArr);
}

void *scramble(void *arr, int ElemSize, int n, int *indArr)
{
    int i;
    BYTE *read, *write;
    void *res;

    res = malloc(ElemSize*n);

    write = (BYTE*)res;
    read = (BYTE*)arr;

    for (i = 0; i < n; i++)
    {
        memcpy(write + indArr[i]*ElemSize, read, ElemSize);
        read += ElemSize;
    }

    return res;
}
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
Quaker
  • 1,483
  • 3
  • 20
  • 36

2 Answers2

0

If you look at the memcpy line:

memcpy(write + indArr[i]*ElemSize, read, ElemSize);

the destination is not correct for indArr[0] if inAddr[0] = 5. Think about the memory being a contiguous block, and the pointer write points to the beginning. Adding 5 * ElemSize to write will put it outside the bounds of the memory allocated.

 _______________________
|    |    |    |   |    | 1
 -----------------------
^- write                 ^- write + 5 * ElemSize
Freddie
  • 871
  • 6
  • 10
  • 1
    As I mentioned in the comments, that was a logical mistake and it was fixed already. Even after fixing it the program refuses to run under Visual Studio's debugger. – Quaker Sep 17 '13 at 17:10
  • The debugger shouldn't even point to that line. Try setting a new breakpoint at the first call to scramble. – Freddie Sep 17 '13 at 17:30
  • @Freddie Looks like we've been hit by the anonymous down-voter. – chux - Reinstate Monica Sep 17 '13 at 21:09
  • @chux Indeed. You were first to point out where the program would segfault (if the debugger actually ran!), so I upvoted your answer. – Freddie Sep 17 '13 at 21:27
0

OP was using 1-based indexing rather than 0-based indexing. This caused the first memcpy to write outside bounds. Change code as below.

// int indArr[5]={5,4,3,2,1};
int indArr[5]={4,3,2,1,0};

[Edit]

Further research with @Freddie & I failed to replicate OP's problem. The 2 steps after the "crash" as posted by the OP are normal steps before a normal exit. Either something stated is amiss or OP's debugger/compiler just "knew" it need not bother with those pesky scramble() function calls and could go home early to Bill's house.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256