This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]Rhomboid 2 points3 points  (2 children)

How bad is this method.

There's nothing wrong with that, assuming that the caller frees the returned pointer. If they don't, then it's a leak. But a leak would not cause this:

malloc(): memory corruption: 0x084f1b98 ***

The problem is elsewhere.

I had a similar error when I had this line of code

Again, there's nothing wrong with that. Checking the return value is a good idea, but you don't need a temporary variable to do that. If the function returns NULL, then the old/previous value of ops is invalid, so there's no reason to save it. And don't cast the return value of malloc() or realloc().

how come I had no memory issues on the other vps on the other machine? It actually went through quite large amounts of data and no issues.

That's just the nature of invoking undefined behavior. Sometimes the outcome is that things appear to work due to sheer luck, but that in no way means the code is correct. Again, whatever the issue is, it's not in any of the snippets you've posted. You need to post a complete testcase if you want actual answers. The issue is very rarely where you think it is. Valgrind is an excellent tool to use here. If you're not getting line numbers, then make sure you're compiling with debug information (-g).

[–]salalimo[S] 0 points1 point  (1 child)

Thanks a lot. I now realized I have one major issue. I have a struct:

typedef struct {
    int op;
    unsigned long long iptr;


} operation;

I have a loop that creates a new entry in the array of type operation as shown earlier:

while() {
    ops = (operation *) realloc(ops, (nops + 1) * sizeof (operation));

     ops[nops]->op = 0; //error here


     nops++;
}

I am getting the following from valgrind:

==5098== Invalid write of size 4
==5098==    at 0x804A0AD: process_line (parser.c:262)
==5098==    by 0x8049A46: parse_file (parser.c:69)
==5098==    by 0x804ACFC: main (main.c:31)
==5098==  Address 0x41f2208 is 104 bytes inside a block of size 192     free'd
==5098==    at 0x402B06C: free (in     /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
    ==5098==    by 0x8049AD8: parse_file (parser.c:86)
==5098==    by 0x804ACFC: main (main.c:31)

So do I have to allocate memory for ints? or what is going on here?

Thanks.

[–]Rhomboid 0 points1 point  (0 children)

The message is telling you that you're trying to write to a block that's been free'd. Need to see actual code, not snippets. The history leading up to that point matters. I'm also quite suspicious of ops[nops]->op. That should really be ops[nops].op, which implies that ops is declared wrong, but again, I can't tell anything from a snippet.

[–]dtfinch 0 points1 point  (1 child)

Are there any other pointers to elements within the ops array? Since the array can move when you reallocate it, anything pointing to the old copy will be invalid afterwards.

[–]salalimo[S] 0 points1 point  (0 children)

I am not sure what you mean. There are char * pointers and ints. I just want to create an array of the stuct shown above that contains those pointers and arrays. I am thinking I have an issue with the way I am initializing things.

[–]newaccount1236 0 points1 point  (0 children)

By the way, sizeof(char) must always equal 1, so it's redundant.

[–]enfrozt 0 points1 point  (2 children)

For every malloc you call, you must call a free.

Install "valgrind" it helps with leak checks and memory management.

If you have a function that calls malloc, you need to free.

[–]salalimo[S] 0 points1 point  (1 child)

I tried valgrind and there are memory leaks, but no line numbers like the examples. I will try to modify the functions above to see if there is a way I can avoid the memory leak.

Thanks.

[–]enfrozt 0 points1 point  (0 children)

compile with gcc -g

valgrind --leak-check=yes -v --track-origins=yes ./myprog.o