1

hello friends :) i'm practicing C programming. in this program i have a task to make array of string. i have no idea what's wrong here...probably something about realloc, error i get is _crtisvalidheappointer

#define _CRT_SECURE_NO_WARNINGS
#define MAX 100
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void readString(char **s)
{
int i = 0;
char c;
printf("\nInput string:     ");
while ((c = getchar()) != '\n')
{
    i++;
    *s = realloc(*s, i*sizeof(char*));
    if (*s == NULL) { printf("Memory allocation failed!"); exit(1); }
    (*s)[i - 1] = c;
}
*s = realloc(*s, (i + 1)*sizeof(char));
if (*s == NULL) { printf("Memory allocation failed!"); exit(1); }
(*s)[i] = '\0';
}


char **load_words()
{
int cnt=0,wordcnt=0,i=0;
char **words = NULL, *input = NULL; 
readString(&input);
while (input[cnt] != '\0' && cnt < strlen(input))
{
    words = realloc(words, ++wordcnt);//errors in second repeat of the loop
    words[wordcnt] = malloc(MAX);
    i = 0;
    while (input[cnt] != ' ')
    {
        words[wordcnt][i++] = input[cnt++];
    }
    words[wordcnt][i] = '\0';
    realloc(words[wordcnt], (i + 1)*sizeof(char));
}
realloc(words, wordcnt);
free(input);
return words;
}

void main()
{
 int i;
 char **words = NULL;
 words = load_words();
 scanf("%d", &i);
}

can someone help me and tell me what did i do wrong here? this function should return array of strings but array should be double pointer(string matrix)

2
  • void readString(char **s) works properly Commented Jun 9, 2016 at 4:03
  • 2
    *s = realloc(*s, i*sizeof(char*)); and *s = realloc(*s, (i + 1)*sizeof(char));: char* vs char, suspicious. Commented Jun 9, 2016 at 4:05

3 Answers 3

3

You need to change

words = realloc(words, ++wordcnt);

to

words = realloc(words, ++wordcnt * sizeof(*words));

Otherwise you are not allocating enough memory.


words[wordcnt] = malloc(MAX);

This also is not correct, you should access words[wordcnt-1].

Sign up to request clarification or add additional context in comments.

Comments

1

You are using realloc but you're not saving its return value anywhere. This means the pointers you have still point to the memory that was freed and the newly allocated memory is leaked.

Look at the working function and you'll see how to use it properly.

Comments

0

One thing to realize when reallocating a double-pointer is that the size of type to realloc is always the sizeof (a pointer). It will be the same on any given system no matter the data type at issue. You can generically reallocate a double-pointer as follows:

/** realloc array of pointers ('memptr') to twice current
 *  number of pointer ('*nptrs'). Note: 'nptrs' is a pointer
 *  to the current number so that its updated value is preserved.
 *  no pointer size is required as it is known (simply the size
 *  of a pointer)
 */
void *xrealloc_dp (void *ptr, size_t *n)
{
    void **p = ptr;
    void *tmp = realloc (p, 2 * *n * sizeof tmp);
    if (!tmp) {
        fprintf (stderr, "xrealloc_dp() error: virtual memory exhausted.\n");
        exit (EXIT_FAILURE);    /* or break; to use existing data */
    }
    p = tmp;
    memset (p + *n, 0, *n * sizeof tmp); /* set new pointers NULL */
    *n *= 2;
    return p;
}

note: the memset call is optional, but useful if you have initialized all non-assigned pointers to NULL (such as when using NULL as a sentinel)

note2: you are free to pass a parameter setting the exact number of pointers to increase (elements to add) or change the multiplier for the current allocation as needed for your code.

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.