0

Problem: I want to allocate memory to the elements of a fixed sized string array, however, I experience a crash 75% of the time.

That is 25% of the time my program runs flawlessly, but the error is one I have not experienced before

#define PACKAGE_COUNT 60
#define MAX_COUNT     5

const char *colors[] = { "blue", "red", "yellow", "purple", "orange" };

char **generatePackage() {


  char **generatedPackage = malloc(PACKAGE_COUNT);
  int randomIndex         = 0;

  for (int i = 0; i <= PACKAGE_COUNT; ++i) {

    randomIndex         = rand() / (RAND_MAX / MAX_COUNT + 1);
    generatedPackage[i] = malloc(sizeof(char) * sizeof(colors[randomIndex])); 
    // ERROR

    strcpy((generatedPackage[i]), colors[randomIndex]);

    // printf("generatePackage - %d: %s \n", i + 1, generatedPackage[i]);
  }

  return generatedPackage;
}

enter image description here enter image description here enter image description here

4
  • Your for loop counts the array entries from 1 to PACKAGE_COUNT but they are indexed between 0 and (PACKAGE_COUNT-1) as is the case with all arrays in C. Changing ++i (which adds one to i and then uses the result) to (i++) which uses your result then increments i will resolve this. Commented May 12, 2019 at 19:22
  • 1
    malloc(sizeof(char) * sizeof(colors[randomIndex])) should be malloc(strlen(colors[randomIndex]) + 1). Additionally, you need to allocate space for the pointers, as @xing points out Commented May 12, 2019 at 19:22
  • Please don't post images of text — post the text. That's unreadable on a mobile device (actually, it's also unreadable on a full-size browser on a Mac, too). Commented May 12, 2019 at 20:00
  • @JonathanLeffler noted, the images were there to assist to show the location of the error on the IDE. Commented May 12, 2019 at 20:39

1 Answer 1

5

You did not take into account the fact that the size of a pointer is not one. Hence, only a fraction of the size was allocated. Thus, some pointers had not enough memory to be allocated, causing the program to crash only when accessing one of those.

#define PACKAGE_COUNT 60
#define MAX_COUNT     5

const char *colors[] = { "blue", "red", "yellow", "purple", "orange" };

char **generatePackage() {
  char **generatedPackage = malloc(PACKAGE_COUNT * sizeof(char*)); // The size of a pointer is not one, so this a multiplicative factor must be applied
  int randomIndex         = 0;

  for (int i = 0; i < PACKAGE_COUNT; ++i) { // the upper bound is i < PACKAGE_COUNT, not i <= PACKAGE_COUNT

    randomIndex         = rand() / (RAND_MAX / MAX_COUNT + 1);
    generatedPackage[i] = malloc(sizeof(char) * (strlen(colors[randomIndex]) + 1)); // You probably want to allocate enough space for the string, rather than enough space for the pointer, so you must use strlen

    strcpy((generatedPackage[i]), colors[randomIndex]);

    // printf("generatePackage - %d: %s \n", i + 1, generatedPackage[i]);
  }

  return generatedPackage;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.