Dynamic alloc. in functions 
Author Message
 Dynamic alloc. in functions

Hi, I'm a beginner in C, not in programming.
I built a function that receives an array of int and the array's size.
For each distinct int in the array, I calculate how many occurences of that
int is in the array.
Since I don't know the number of distinct int in the array, I use a chained
list with dynamic allocation to compile the results.
I thought it would be a nice function that I could later overload once I
convert to C++.
The problem is, doing dynamic allocation in a function, I loose everything
when I go back to main(), do I?
Is there a way of using dynamic alloc. with functions, or everything
dynamically alloc. will always be in main()?

I hope this is understandable. English is not my first language. Thanks.
Jack.



Fri, 11 Oct 2002 03:00:00 GMT  
 Dynamic alloc. in functions
Jack a crit dans le message ...

Quote:
>Hi, I'm a beginner in C, not in programming.
>I built a function that receives an array of int and the array's size.

Sounds clear.

Quote:
>For each distinct int in the array, I calculate how many occurences of that
>int is in the array.

Ok,

Quote:
>Since I don't know the number of distinct int in the array, I use a chained
>list with dynamic allocation to compile the results.

It is named a "linked list" in English.

Quote:
>I thought it would be a nice function that I could later overload once I
>convert to C++.

What is C++ ? (I was joking)

Quote:
>The problem is, doing dynamic allocation in a function, I loose everything
>when I go back to main(), do I?

Fortunanely no. But you have to return the head of the linked list to the
caller.
(and of course design a "destroy" function -- as you say in C++ -- to
properly free the linked list)

Quote:
>Is there a way of using dynamic alloc. with functions, or everything
>dynamically alloc. will always be in main()?

Of course, you can use malloc() free() whereever you want. Just *don't loose
the key" (I mean the pointer to the allocated memory)

Quote:
>I hope this is understandable.

It is.

Quote:
>English is not my first language.

Neither is mine.

--
-hs- "Stove"
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
ISO-C Library: http://www.dinkum.com/htm_cl
"Really?  When run on my machine, a printed copy of the C FAQ leaps
from the monitor and whacks me over the head.." -- Chris Mears CLC



Sat, 12 Oct 2002 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Dynamic Alloc - Arrays

2. Dynamic Signature Function Call in ANSI C

3. Dynamic memory malloc'ed in a function.

4. COBOL making dynamic call to c function under OS/390

5. Help: caught error when a dynamic library excute functions that in static

6. Functions returning char *, dynamic memory

7. Dynamic Function Calls

8. help with function to create dynamic two-dimensional char array

9. Dynamic function (in C)

10. Anwser: Problems passing a dynamic array of structures to a function

11. Problems passing a dynamic array of structures to a function

12. dynamic parameter list for external function

 

 
Powered by phpBB® Forum Software