-1

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?

razlebe
  • 7,134
  • 6
  • 42
  • 57
praks5432
  • 7,246
  • 32
  • 91
  • 156
  • 2
    Only if you show the code for `realloc` and friends. – Jon Oct 23 '11 at 22:40
  • 1
    Add some code... we cannot get what is the problem. – Salvatore Previti Oct 23 '11 at 22:40
  • Yah post the code... +1 for adding homework tag! – Ahmed Masud Oct 23 '11 at 22:40
  • 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. – Adrian Brown Oct 23 '11 at 22:41
  • Does the error occur during or after you call `realloc`? – Marlon Oct 23 '11 at 22:43
  • 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. – AusCBloke Oct 23 '11 at 22:45
  • 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 calls – praks5432 Oct 23 '11 at 22:47
  • 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);` – AusCBloke Oct 23 '11 at 23:00
  • Code formatting doesn't work completely in comments, this is what I wanted to post that contains the same error: http://pastebin.com/AanSSkwq Is that similar to your code from what you described? – AusCBloke Oct 23 '11 at 23:07
  • **Arrays and pointers are different beasts**. I suggest you read section 6 of the [comp.lang.c faq](http://c-faq.com/). – pmg Oct 24 '11 at 08:50

3 Answers3

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.

caf
  • 233,326
  • 40
  • 323
  • 462
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.

schot
  • 10,958
  • 2
  • 46
  • 71
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?

Adrian Brown
  • 456
  • 3
  • 14
  • it was just to help my thinking - I've now taken that out and am calling realloc directly onto the array- it still crashes – praks5432 Oct 23 '11 at 22:55
  • 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 it – Adrian Brown Oct 23 '11 at 23:07
  • 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 same – praks5432 Oct 23 '11 at 23:34
  • 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 sizes – Adrian Brown Oct 24 '11 at 07:01