dynamic array - newbie help 
Author Message
 dynamic array - newbie help

Hi,

1st question :

Is there any way to create a dynamic array ?
(I need this because I do not know the array[size] before calculation.)

2nd question:
Is there any way to store different values resulting out of a
calculation in a (extern?) file and later call these values
into an array ?

Help and useful alternatives are appreciated ...* my brain is cooking *

Thanx & Regards Alexander Liedtke



Fri, 01 Jun 2001 03:00:00 GMT  
 dynamic array - newbie help

| Hi,
|
| 1st question :
|
| Is there any way to create a dynamic array ?
| (I need this because I do not know the array[size] before calculation.)

int *dynArray;

int
main()
{
  int size;

  scanf( "%d", &size );

  dynArray = (int*) malloc( size * sizeof( int ) );

  /* you've got your dynamic array */

  return 0;

Quote:
}

igorR
--
"Ma belle demoiselle, repondit Candide, quand on est amoureux, jaloux
et fouette par l'Inquisition, on ne se connait plus"
                                Voltaire, "Candide"


Fri, 01 Jun 2001 03:00:00 GMT  
 dynamic array - newbie help

   1st question :

   Is there any way to create a dynamic array ?
   (I need this because I do not know the array[size] before calculation.)

Use malloc() and realloc().

   2nd question:
   Is there any way to store different values resulting out of a
   calculation in a (extern?) file and later call these values
   into an array ?

tmpfile()/fwrite()/fread().
--
(supporter of the campaign for grumpiness where grumpiness is due in c.l.c)

Please: do not email me copies of your posts to comp.lang.c
        do not ask me C questions via email; post them instead



Fri, 01 Jun 2001 03:00:00 GMT  
 dynamic array - newbie help

Quote:


> | Hi,
> |
> | 1st question :
> |
> | Is there any way to create a dynamic array ?
> | (I need this because I do not know the array[size] before calculation.)

> int *dynArray;

> int
> main()
> {
>   int size;

>   scanf( "%d", &size );

okay - it's just an example. But beware - scanf() does not always work
the way you want it to!!!

Quote:
>   dynArray = (int*) malloc( size * sizeof( int ) );

Better style might be to use

        dynarray = malloc(size * (sizeof *dynarray));

The cast to (int *) is unnecessary. Also, what if someday you decide
that dynarray is to become an array of longs?

You need to check that dynarray is not NULL before attempting to sue the
memory. It's also best to free() any dynamically allocated memory once
you no longer need it.

Quote:

>   /* you've got your dynamic array */

>   return 0;
> }

--
 { Sunil Rao }
"There is no scorn more profound, or on the whole more justifiable,
 than that of the men who make for the men who explain."
                                          -- HARDY, Godfrey Harold.


Fri, 01 Jun 2001 03:00:00 GMT  
 dynamic array - newbie help

| >

| >
| > | Hi,
| > |
| > | 1st question :
| > |
| > | Is there any way to create a dynamic array ?
| > | (I need this because I do not know the array[size] before calculation.)
| >
| > int *dynArray;
| >
| > int
| > main()
| > {
| >   int size;
| >
| >   scanf( "%d", &size );
|
| okay - it's just an example. But beware - scanf() does not always work
| the way you want it to!!!

Could you please point out the pitfalls?

| >   dynArray = (int*) malloc( size * sizeof( int ) );
|
| Better style might be to use
|
|       dynarray = malloc(size * (sizeof *dynarray));
|
| The cast to (int *) is unnecessary. Also, what if someday you decide
| that dynarray is to become an array of longs?

Aha?

[snip]
malloc(3C)             C Library Functions             malloc(3C)

NAME
     malloc, calloc, free, memalign, realloc,  valloc,  alloca  -
     memory allocator

SYNOPSIS
     #include <stdlib.h>

     void *malloc(size_t size);

malloc() gives me back a void*, shouldn't I cast it to the proper
type?

| You need to check that dynarray is not NULL before attempting to sue the
| memory. It's also best to free() any dynamically allocated memory once
| you no longer need it.

Jepp, I should do it. I got little bit too used to new[] and a decent
OS nowadays.

Thanx for the comments.

igorR
--
"Ma belle demoiselle, repondit Candide, quand on est amoureux, jaloux
et fouette par l'Inquisition, on ne se connait plus"
                                Voltaire, "Candide"



Fri, 01 Jun 2001 03:00:00 GMT  
 dynamic array - newbie help

Quote:



> | >

> | >
> | > | Hi,
> | > |
> | > | 1st question :
> | > |
> | > | Is there any way to create a dynamic array ?
> | > | (I need this because I do not know the array[size] before calculation.)
> | >
> | > int *dynArray;
> | >
> | > int
> | > main()
> | > {
> | >   int size;
> | >
> | >   scanf( "%d", &size );
> |
> | okay - it's just an example. But beware - scanf() does not always work
> | the way you want it to!!!

> Could you please point out the pitfalls?

You could look in the C FAQ, but just think about what happens when
somebody enters in a non-numeric character.

- Show quoted text -

Quote:
> | >   dynArray = (int*) malloc( size * sizeof( int ) );
> |
> | Better style might be to use
> |
> |       dynarray = malloc(size * (sizeof *dynarray));
> |
> | The cast to (int *) is unnecessary. Also, what if someday you decide
> | that dynarray is to become an array of longs?

> Aha?

> [snip]
> malloc(3C)             C Library Functions             malloc(3C)

> NAME
>      malloc, calloc, free, memalign, realloc,  valloc,  alloca  -
>      memory allocator

> SYNOPSIS
>      #include <stdlib.h>

>      void *malloc(size_t size);

> malloc() gives me back a void*, shouldn't I cast it to the proper
> type?

You don't need to. The C standard guarantees that the conversion will be
done automatically. In fact, leaving the cast in place can mask errors -
in particular the code will compile, possibly with disastrous
consequences, if you forget to #include <stdlib.h> (as, in fact, you
did). The compiler, without a declaration in place, will assume that
malloc() returns an int.

This is off-topic, but without the cast your code won't compile umnder a
C++ compiler. But then, in C++ you'd use the new operator anyway.

Quote:
> | You need to check that dynarray is not NULL before attempting to sue the
> | memory. It's also best to free() any dynamically allocated memory once
> | you no longer need it.

> Jepp, I should do it. I got little bit too used to new[] and a decent
> OS nowadays.

You need to use the delete operator in C++ to destroy objects. (Unless -
going even further off-topic here - you were talking about Java.)

--
 { Sunil Rao }
"There is no scorn more profound, or on the whole more justifiable,
 than that of the men who make for the men who explain."
                                          -- HARDY, Godfrey Harold.



Fri, 01 Jun 2001 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Newbie Question: using delete with dynamic 2-d arrays

2. Newbie - Dynamic Arrays??

3. Newbie Q: Dynamic Array Sizing

4. Dynamic arrays not really dynamic in managec C++?

5. dynamic array help

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

7. Help - Dynamic Array

8. Dynamic Array Help

9. req: dynamic array help

10. 2d array dynamic allocation help

11. Dynamic Array/Vector Data Structures HELP

12. help in c(dynamic allocation for mulitidimensional arrays)

 

 
Powered by phpBB® Forum Software