Realloc Invalid Pointer in C [closed]
Asked Answered
B

3

0

This is a homework assignment so I don't want to post any code, but I'm pretty stumped with the bug that I have.

Currently I have a array that has been malloced and am copying the pointer to the array. Now, I can memcpy and memmove with this array and that works fine.

However, when I do a realloc with it an invalid pointer error comes up - and I have absolutely no idea why.

Can anyone help?

Bartels answered 23/10, 2011 at 22:38 Comment(10)
Only if you show the code for realloc and friends.Wolford
Add some code... we cannot get what is the problem.Tug
Yah post the code... +1 for adding homework tag!Melancholic
What pointer are you copying to the array? The pointer that was malloced? Isnt that the pointer to the array itself. If you realloc something that was malloced, the address of the data may well move.Antispasmodic
Does the error occur during or after you call realloc?Cowlick
Since this is for an assignment and you don't want to post your code, are you able to produce some similar code that will give the same error? It's hard to find errors in your code without any code.Wrinkle
it's difficult for me to provide code, but what I'm doing is I have an array in a struct, I want to increase the memory allocated to this array. I copy the pointer to this array into another variable and make a realloc call on that. Before that, while the array is not full I simply do a series of memcpy callsBartels
Is this similar to what you're doing? This causes an invalid pointer error for me: typedef struct { int something; int array[5]; } t; t *error_struct; int *p_array = error_struct.array; realloc(p_array, 10);Wrinkle
Code formatting doesn't work completely in comments, this is what I wanted to post that contains the same error: pastebin.com/AanSSkwq Is that similar to your code from what you described?Wrinkle
Arrays and pointers are different beasts. I suggest you read section 6 of the comp.lang.c faq.Windflower
A
1

you said 'I copy the pointer to this array into another variable'. The problem is as soon as you do a realloc, the original pointer is no longer valid. I dont see the reason to copy the pointer to a variable?

Antispasmodic answered 23/10, 2011 at 22:53 Comment(4)
it was just to help my thinking - I've now taken that out and am calling realloc directly onto the array- it still crashesBartels
so basically you do void *ptr = malloc(100); void *next_ptr = realloc(ptr, 200); and you get invalid pointer? Id say print out some values at points through your code, unless you have a debugger handy. See what value you have back from malloc then see whats passed in to realloc. I would presume they are different. Look at the value of the variable at various points until you narrow down what line is changing itAntispasmodic
ok I was looking at the memory address of the array on which memcpy is done and on which the realloc is done and they are exactly the sameBartels
And that's the same as the one returned from malloc. If that's the case then you have probably memcpy over the end of the array. Double check the sizesAntispasmodic
S
5

realloc() only works if the pointer you pass it was one returned earlier from malloc(), calloc() or realloc() (or is NULL, in which case it works exactly like malloc()).

You can not call it on arrays. If you have a struct like this:

struct foo {
    char a;
    int x[5];
    double z;
};

then you cannot use realloc() to increase the size of x. You must instead switch to using entirely dynamic allocation: the struct member changes to a pointer:

struct foo {
    char a;
    int *x;
    double z;
};

...and when you create an instance of the struct, you call malloc() for the initial allocation:

struct foo f;

f.x = malloc(5 * sizeof f.x[0]);

Now you can call realloc() on f.x, but the cost is that you must also call free() on f.x when the struct is no longer needed.

Somniferous answered 24/10, 2011 at 0:45 Comment(0)
W
4

When realloc or free result in an invalid pointer error or segfault, and you are sure that you are passing a valid pointer obtained from malloc, then a good possibility is that you corrupted your memory: You wrote to the area where malloc stores the book keeping of the memory blocks. In your case this is a memcpy or memmove call.

You can use the valgrind memory error detector to help you find these kind of errors.

Wenn answered 24/10, 2011 at 8:46 Comment(0)
A
1

you said 'I copy the pointer to this array into another variable'. The problem is as soon as you do a realloc, the original pointer is no longer valid. I dont see the reason to copy the pointer to a variable?

Antispasmodic answered 23/10, 2011 at 22:53 Comment(4)
it was just to help my thinking - I've now taken that out and am calling realloc directly onto the array- it still crashesBartels
so basically you do void *ptr = malloc(100); void *next_ptr = realloc(ptr, 200); and you get invalid pointer? Id say print out some values at points through your code, unless you have a debugger handy. See what value you have back from malloc then see whats passed in to realloc. I would presume they are different. Look at the value of the variable at various points until you narrow down what line is changing itAntispasmodic
ok I was looking at the memory address of the array on which memcpy is done and on which the realloc is done and they are exactly the sameBartels
And that's the same as the one returned from malloc. If that's the case then you have probably memcpy over the end of the array. Double check the sizesAntispasmodic

© 2022 - 2024 — McMap. All rights reserved.