0

I'm trying to resize an array to contain values with length of "newlen". If newlen is less than the original array length, the end of the array is discarded. If newlen is greater, the new integers should be zero. I came up with this function, but when I test it, I get segmentation fault. I also don't know how to add the zeroes to the end, how can I keep track of the index so that I can add int zeroes at the end? I did the first case (where newlen is less than len), but don't know how to do the other case.

Here's the structure for the array:

typedef struct {
  int* data;
  unsigned int len;
} intarr_t;

My function:

array_size( intarr_t* p, unsigned int newlength )

{

  int i,m = 0;

  if (newlength < len) 
        int *tmp;
    tmp = realloc(p->data, (p->newlen)* sizeof *p->data);
    if(tmp)

  {
    for (i; newlength < p->len; i++)
        {  for (m; newlength < p->len; m++)

    {
        p->data[i] = p->data[m];
    }
        }

  }

}
3
  • this code is one big mistake, i is not initialized, len is unknown variable, tmp is not used, newlen is not used, double infinite for... This code should not compile at all Commented Apr 3, 2015 at 23:52
  • Make sure newlength isn't 1. better yet make sure it is a power of two Commented Apr 3, 2015 at 23:55
  • This code doesn't compile. Before trying to solve runtime errors such as segmentation faults: enable compiler errors and warnings, fix all reported things, and make sure to post your exact code. Commented Apr 4, 2015 at 0:15

2 Answers 2

1

The pointer supplied to realloc() (p->data) is not changed.

Usage of realloc() normally needs to be something like this

tmp = realloc(old_pointer, new_number * sizeof(*tmp));
if (tmp == NULL)
{
      /*   an error occur and old_pointer remains unchanged */
      /*   need to recover */
}
else
{
     old_pointer = tmp;
     length = new_number;
}

I'm assuming tmp and old_pointer are the same type in the above.

Then you can initalise the additional elements of old_pointer (assuming the array size is being increased).

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

Comments

1

As you are not initializing i anywhere before using it in the loop may be it's because of this.

Also for the code

for (m; newlength < p->len; m++)

where are you re initializing this value for second and other passes of the for loop.Also both the loops are infinite loops.

To implement the second case you can do something like this

if(newlength > p->len)
{
    int old_length = p->len;
    p = realloc(p->data, newlength * sizeof(p->data));
    if(p)
        memset(p + old_length,0,(newlength - old_length) * sizeof(p->data));
}

Your entire working function will be like this

array_size( intarr_t* p, unsigned int newlength ) {
    intarr_t * tmp;
    if(newlength < p->len) {
        tmp = realloc(p->data, newlength * sizeof(p->data)); //realloc will automatically discard elements after new length
        if(tmp) {
            p = tmp;
            p->len = newlength;
        }
    }
    else if(newlength > p->len)
    {
        int old_length = p->len;
        tmp = realloc(p->data, newlength * sizeof(p->data));
        if(tmp){
            p = tmp;
            p->len = newlength;
            memset(p + old_length,0,(newlength - old_length) * sizeof(p->data));//for setting trailing element to 0
        }   
    }
}

3 Comments

Hello, sorry for my incomplete function, I forgot to put the main file. I tested this code, it doesn't resize the array to the right len. Maybe because it doesn't add the elements for the first case?
What is the difference between size that you are getting
Hmm, I think it's because you're not considering the case where the len is 0. Difference is by 1.

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.