|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hi,
in some C program I need to port to some architecture, I send to a function the parameter char[50000] with predefined values. Inside the function, this data is read and something is calculated. But for some reasons that I can not explain here (too long) the memory is really small and I would need to use the space used by this char[50000]. Then I wonder if it can be deleted or destroyed in some way. Afterwards I need to use malloc and free and I think I would have more memory if I could "delete" this. Any hint ? |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On May 30, 1:44 am, Horacius ReX <horacius....@gmail.com> wrote:
> Hi, > > in some C program I need to port to some architecture, I send to a > function the parameter char[50000] with predefined values. Inside the > function, this data is read and something is calculated. But for some > reasons that I can not explain here (too long) the memory is really > small and I would need to use the space used by this char[50000]. Then > I wonder if it can be deleted or destroyed in some way. Afterwards I > need to use malloc and free and I think I would have more memory if I > could "delete" this. Any hint ? A char[50000] needs not to exist in a C89 implementation. C99 guarantees that objects can be at least 65536 bytes in size. A way to "delete" such object is: { char array[N]; /* do stuff with array */ } /* array no longer exists */ My suggestion is: Split your function to two functions. The first should do the calculations you speak of, such as: { char array[50000]; f1(array); } /* f2(...) */ This may, or may not work. Standard C doesn't guarantee anything. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
"Horacius ReX" <horacius.rex@gmail.com> wrote in message news:52c1179f-3244-4c93-a230-942a0603aa77@f63g2000hsf.googlegroups.com... > Hi, > > in some C program I need to port to some architecture, I send to a > function the parameter char[50000] with predefined values. Inside the > function, this data is read and something is calculated. But for some > reasons that I can not explain here (too long) the memory is really > small and I would need to use the space used by this char[50000]. Then > I wonder if it can be deleted or destroyed in some way. Afterwards I > need to use malloc and free and I think I would have more memory if I > could "delete" this. Any hint ? Where does this 50KB exist: as initialised (static) data, as a local variable as you suggest (that's a big variable), or on the heap? So you call a function with this 50KB array, then you no longer need that array and would like to use it for something else? If it's initialised data, probably you will be able to overwrite with something new, but you can't easily add it to the memory pool of malloc; you would have to make use of the memory explicitly, like setting an array pointer to the start of it (or creating special versions of malloc/free to use that block). The same goes for local variable (or 'stack') data - if you stay in that function. The memory will be recovered if you return from the function. But even then 'stack' and 'heap' (malloc) memory may not be shared so it could just exist as spare stack memory and not extend the heap. Best solution would be to use malloc-ed memory for this 50KB block, provided it can be initialised without the initialisation code/data itself taking 50KB. -- Bartc |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
Horacius ReX wrote:
> Hi, > > in some C program I need to port to some architecture, I send to a > function the parameter char[50000] with predefined values. Inside the > function, this data is read and something is calculated. But for some > reasons that I can not explain here (too long) the memory is really > small and I would need to use the space used by this char[50000]. That's what a union is for. -- pete |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
On May 29, 5:51pm, vipps...@gmail.com wrote:
> { char array[N]; /* do stuff with array */ } > /* array no longer exists */ Since the OP was concerned about actual memory allocated for the local, we should remember that scoped variables are allocated in the enclosing routines activation record, although often as an (effective) union with other parallel scoped definitions. So on most implementations, this is unlikely to accomplish what the OP wants. |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
On May 30, 2:51 am, pete <pfil...@mindspring.com> wrote:
> Horacius ReX wrote: > > Hi, > > > in some C program I need to port to some architecture, I send to a > > function the parameter char[50000] with predefined values. Inside the > > function, this data is read and something is calculated. But for some > > reasons that I can not explain here (too long) the memory is really > > small and I would need to use the space used by this char[50000]. > > That's what a union is for. > > -- > pete could you please tell me what you mean ? |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
my data comes at the beginning from another function:
function do_smt(char thing[50000]){ ... do things here with "thing" and afterwards eliminate it } and this data was not created with malloc vipps...@gmail.com wrote: > On May 30, 1:44 am, Horacius ReX <horacius....@gmail.com> wrote: > > Hi, > > > > in some C program I need to port to some architecture, I send to a > > function the parameter char[50000] with predefined values. Inside the > > function, this data is read and something is calculated. But for some > > reasons that I can not explain here (too long) the memory is really > > small and I would need to use the space used by this char[50000]. Then > > I wonder if it can be deleted or destroyed in some way. Afterwards I > > need to use malloc and free and I think I would have more memory if I > > could "delete" this. Any hint ? > A char[50000] needs not to exist in a C89 implementation. > C99 guarantees that objects can be at least 65536 bytes in size. > A way to "delete" such object is: > > { char array[N]; /* do stuff with array */ } > /* array no longer exists */ > My suggestion is: > Split your function to two functions. The first should do the > calculations you speak of, such as: > > { char array[50000]; f1(array); } > /* f2(...) */ > > This may, or may not work. Standard C doesn't guarantee anything. |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
Horacius ReX wrote:
> On May 30, 2:51 am, pete <pfil...@mindspring.com> wrote: >> Horacius ReX wrote: >>> Hi, >>> in some C program I need to port to some architecture, I send to a >>> function the parameter char[50000] with predefined values. Inside the >>> function, this data is read and something is calculated. But for some >>> reasons that I can not explain here (too long) the memory is really >>> small and I would need to use the space used by this char[50000]. >> That's what a union is for. > could you please tell me what you mean ? A union declaration looks a lot like a struct declaration, but all of a union's members overlap in memory. The lowest addressable byte of each member of a union is the same byte. A union is as large as its largest member, plus whatever additional padding bytes the compiler might want. /* BEGIN new.c */ #include <stdio.h> #include <string.h> #define STRING "\nhello world" union csa { double pi; char array[sizeof STRING]; int ret; }; void func(union csa *onion); int main(void) { union csa onion; printf("sizeof onion is %u\n", (unsigned)sizeof onion); printf("sizeof onion.pi is %u\n", (unsigned)sizeof onion.pi); printf("sizeof onion.array is %u\n", (unsigned)sizeof onion.array); printf("sizeof onion.ret is %u\n", (unsigned)sizeof onion.ret); strcpy(onion.array, STRING); func(&onion); printf("\nonion.ret is %d\n", onion.ret); return onion.ret; } void func(union csa *onion) { puts(onion -> array); onion -> ret = 0; } /* END new.c */ -- pete |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
"Horacius ReX" <horacius.rex@gmail.com> wrote in message news:7404ab66-e556-41ba-a539-09c3cf244d92@e39g2000hsf.googlegroups.com... > my data comes at the beginning from another function: > > function do_smt(char thing[50000]){ > > .. do things here with "thing" and afterwards eliminate it > > } > > and this data was not created with malloc Ok so you're already in the function that uses the 50K array? As written, I think that 'thing' will be a pointer to an array. So you can now use it for any purpose you like: int *p; p=thing; /* p now points to maybe 12500 or 25000 ints */ Assuming that 'thing' points to normal read/write memory. So whatever allocations you /would/ have used malloc for, you can manually allocate and manage from 'thing'. Remembering that on exit from this function, they may no longer exist. What you can't do is add the 'thing' memory to the memory used by malloc. Or free it, since you say it didn't come from malloc. -- Bartc |
|
|
|
#10 |
|
Messages: n/a
Hébergeur: |
Horacius ReX wrote:
> Hi, > > in some C program I need to port to some architecture, I send to a > function the parameter char[50000] with predefined values. How is this array allocated? How and when it can be deallocated would depend on this. Is it an auto object? Is it a file scope or static object? Is it got from malloc? > Inside the > function, this data is read and something is calculated. But for some > reasons that I can not explain here (too long) the memory is really > small and I would need to use the space used by this char[50000]. Then > I wonder if it can be deleted or destroyed in some way. Afterwards I > need to use malloc and free and I think I would have more memory if I > could "delete" this. Any hint ? If it has been allocated via malloc then you could obviously deallocate it via free. If it is an auto object it will automatically be deallocated when it's scope is exited, which is the enclosing block. You cannot free it yourself though you could simply re-use it. If it is a file scope or static qualified object then it will persist till the program terminates and you have no portable way of deallocating the associated memory, though again, you can just re-use it if you want. If this does not clear up your problem then please provide more details on the exact nature of the array. |
|
|
|
#11 |
|
Messages: n/a
Hébergeur: |
santosh <santosh.k83@gmail.com> writes:
> Horacius ReX wrote: >> in some C program I need to port to some architecture, I send to a >> function the parameter char[50000] with predefined values. > > How is this array allocated? How and when it can be deallocated would > depend on this. Is it an auto object? Is it a file scope or static > object? Is it got from malloc? > >> Inside the >> function, this data is read and something is calculated. But for some >> reasons that I can not explain here (too long) the memory is really >> small and I would need to use the space used by this char[50000]. Then >> I wonder if it can be deleted or destroyed in some way. Afterwards I >> need to use malloc and free and I think I would have more memory if I >> could "delete" this. Any hint ? > > If it has been allocated via malloc then you could obviously deallocate > it via free. If it is an auto object it will automatically be > deallocated when it's scope is exited, which is the enclosing block. > You cannot free it yourself though you could simply re-use it. If it is > a file scope or static qualified object then it will persist till the > program terminates and you have no portable way of deallocating the > associated memory, though again, you can just re-use it if you want. Except that, in many implementations, block-scope auto objects are not physically deallocated until the enclosing function terminates. -- Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst> Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" |
|
![]() |
| Outils de la discussion | |
|
|