You are not logged in.
Pages: 1
I am trying to create an array, delete the array, and create a new one with the same name but a different size...
#include <stdlib.h>
int main() {
int *ptr = malloc(3 * sizeof (int));
free (ptr);
int *ptr = malloc(5 * sizeof (int));
return 0;
}
When I try to compile with gcc, I get...
test.c: In function 'main':
test.c:6: error: redefinition of 'ptr'
test.c:4: error: previous definition of 'ptr' was here
Is it possible to do what I am trying to do with C?
Last edited by tony5429 (2008-09-18 05:22:01)
Offline
you're declaring *ptr twice....
I'm kind of a noob with C but doesn't free just delete whatever the pointer is pointing at? not the pointer itself. I think basically taking away the 'int' declaration the second time 'round would solve this
Last edited by Stythys (2008-09-18 05:24:08)
[home page] -- [code / configs]
"Once you go Arch, you must remain there for life or else Allan will track you down and break you."
-- Bregol
Offline
#include <stdlib.h>
int main() {
int *ptr = malloc(3 * sizeof (int));
free (ptr);
*ptr = malloc(5 * sizeof (int));
return 0;
}
Should make it compile correctly I think, but I don't know C at all. (I'm a java programmer).
Offline
#include <stdlib.h> int main() { int *ptr = malloc(3 * sizeof (int)); free (ptr); *ptr = malloc(5 * sizeof (int)); return 0; }
Should make it compile correctly I think, but I don't know C at all. (I'm a java programmer).
Actually, ptr is declared as (int *) - a pointer to an integer. When we point it to the newly allocated space it should be written as "ptr = malloc(5 * sizeof (int));" (i.e. without the asterisk).
@tony5429: You could also look at realloc(3) or another data structure except arrays. It all depends on what you're trying to do.
Offline
This is what you need to do.
#include <stdlib.h>
int main() {
// Declare ptr. The * is part of the declaration. You also initialize it to the return
// value of the malloc call, which will be some address in memory. As a side-effect,
// malloc allocates some memory at that address.
int *ptr = malloc(3 * sizeof (int));
// Here you free the memory allocated. The value of ptr is not changed -- it still
// points to the same address. All you're doing is telling the memory manager that
// the memory reserved by the malloc call above can be used for something else.
free (ptr);
// Here you call malloc again to allocate a different bit of memory. As before,
// malloc returns the address of the memory it has reserved for you.
ptr = malloc(5 * sizeof (int));
return 0;
}
The error in your original was attempting to declare ptr twice in the same scope. You seem to be mixing up declaration including initial values with plain old assignments to variables.
The error in Ruckus's suggestion was putting *ptr instead of ptr. If you step through his code you'll see that:
1. ptr is declared and initialized to malloc's return value (an address).
2. All claim to memory reserved by malloc is given up.
3. Now we allocate some more memory, but instead of storing the address in ptr, we store it in *ptr—that's the memory location indicated by ptr. But ptr is still pointing to that first address—the one we just told the system we were done with. Rather than reassigning ptr, we just wrote some random number to an address we don't have a right to mess with.
Offline
I'm not a C programmer, so I never should have posted. I'm know java, and hence that's all I could come up with. I've always wanted to learn C/C++. I really need to buy a book and just do it.
Offline
It works. Thanks everyone!
Offline
wouldn't this be better?
#include <stdlib.h>
int main() {
int **ptr = malloc(3 * sizeof (int));
free (ptr);
ptr = malloc(5 * sizeof (int));
return 0;
}
doing so you can access the array in the form prt[1] for example(you declare a pointer to a pointer, and an array is really a pointer to a memory zone)
-$: file /dev/zero
/dev/zero: symbolic link to '/dev/brain'
Offline
wouldn't this be better?
#include <stdlib.h> int main() { int **ptr = malloc(3 * sizeof (int)); free (ptr); ptr = malloc(5 * sizeof (int)); return 0; }
doing so you can access the array in the form prt[1] for example(you declare a pointer to a pointer, and an array is really a pointer to a memory zone)
Maybe I've forgotten my C a little bit now, but I used int** mostly as an 2D array, and when I wanted to have one dimensional array, I used int*.
In what way accessing in the way you shown this is better?
I tried, I failed, no matter. Try again, fail again, fail better.
Offline
wouldn't this be better?
#include <stdlib.h> int main() { int **ptr = malloc(3 * sizeof (int)); free (ptr); ptr = malloc(5 * sizeof (int)); return 0; }
No, you've made a pointer to a pointer to an int.
Offline
ptr[1] seems to work with 1 asterisk - it doesn't seem to need two.
Offline
Maybe I've forgotten my C a little bit now, but I used int** mostly as an 2D array, and when I wanted to have one dimensional array, I used int*.
In what way accessing in the way you shown this is better?
true, I think I should rest from 2D arrays
-$: file /dev/zero
/dev/zero: symbolic link to '/dev/brain'
Offline
Bringing the school work to the forums? Lol
Offline
Bringing the school work to the forums? Lol
Not exactly, more like bringing hobby to the forums
I tried, I failed, no matter. Try again, fail again, fail better.
Offline
Pages: 1