You are not logged in.

#1 2008-09-18 05:16:55

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Arrays in C

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

#2 2008-09-18 05:22:51

Stythys
Member
From: SF Bay Area
Registered: 2008-05-18
Posts: 878
Website

Re: Arrays in C

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

#3 2008-09-18 06:04:11

Ruckus
Member
Registered: 2007-02-17
Posts: 204

Re: Arrays in C

#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

#4 2008-09-18 06:48:40

foutrelis
Developer
From: Athens, Greece
Registered: 2008-07-28
Posts: 705
Website

Re: Arrays in C

Ruckus wrote:
#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

#5 2008-09-18 06:57:28

pauldonnelly
Member
Registered: 2006-06-19
Posts: 776

Re: Arrays in C

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

#6 2008-09-18 14:43:17

Ruckus
Member
Registered: 2007-02-17
Posts: 204

Re: Arrays in C

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

#7 2008-09-18 18:24:59

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: Arrays in C

It works. Thanks everyone!

Offline

#8 2008-09-18 20:55:07

ornitorrincos
Forum Fellow
From: Bilbao, spain
Registered: 2006-11-20
Posts: 198

Re: Arrays in C

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

#9 2008-09-18 21:20:22

kamituel
Member
From: Poland
Registered: 2006-09-14
Posts: 47
Website

Re: Arrays in C

ornitorrincos wrote:

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

#10 2008-09-18 23:42:51

pauldonnelly
Member
Registered: 2006-06-19
Posts: 776

Re: Arrays in C

ornitorrincos wrote:

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

#11 2008-09-19 03:58:39

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: Arrays in C

ptr[1] seems to work with 1 asterisk - it doesn't seem to need two.

Offline

#12 2008-09-19 09:27:27

ornitorrincos
Forum Fellow
From: Bilbao, spain
Registered: 2006-11-20
Posts: 198

Re: Arrays in C

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

#13 2008-09-19 16:48:02

freakcode
Member
From: São Paulo - Brazil
Registered: 2007-11-03
Posts: 410
Website

Re: Arrays in C

Bringing the school work to the forums? Lol

Offline

#14 2008-09-19 18:38:34

kamituel
Member
From: Poland
Registered: 2006-09-14
Posts: 47
Website

Re: Arrays in C

freakcode wrote:

Bringing the school work to the forums? Lol

Not exactly, more like bringing hobby to the forums tongue


I tried, I failed, no matter. Try again, fail again, fail better.

Offline

Board footer

Powered by FluxBB