Dynamic Alloc - Arrays 
Author Message
 Dynamic Alloc - Arrays

Quote:

> realloc(fieldsize,sizeof(unsigned short int)*no_fields);

That's not even close to correct.  You should use realloc
only with a pointer you already got from a previous call
to malloc (or realloc), and it is essential to capture
the pointer returned as the value of the malloc or realloc
function call, since that is the only handle you will have
for locating where the allocator placed the new object.
Look at several existing, working applications that use
dynamic allocation to see how the malloc/realloc/free
subsystem is used.
--



Mon, 03 Oct 2005 14:52:06 GMT  
 Dynamic Alloc - Arrays

Quote:

>I am new to C and trying to write a little utility programme to
>manipulate records in a file.   I have written some code to get input
>on the number of fields in a record and their size.   I want to
>dynamically declare an array to hold the field sizes and have used
>realloc() in the code below but have a strong feeling that I am doing
>something wrong (i.e. I could be writing over other data).  Could do

It is worse than that.  You need to find a text book and learn what
the parameters and return value from realloc signify.

Quote:
>with an opinion.  (I have removed the indents to avoid too many split
>lines hope this does not cause a problem.

>/*********************************************/

>#include <stdio.h>
>#include <stdlib.h>

>int main(int argc,char *argv[]) {

>unsigned short int i, no_fields, fieldsize[1], x = 0; // x is for
>debuging only

>/*if(argc != 3){
>printf("\nIncorrect usage:\n\nCorrect usage = exe filename
>[InputFilename] [OutputFilename]\n\nTry again please\n");

>exit(0);
>}*/

>printf("\nHow many fields are there in a record?");

>scanf("%d",&no_fields); printf("\n");

>realloc(fieldsize,sizeof(unsigned short int)*no_fields);

fieldsize is an array.  It is never legal to pass an array name to
realloc.  The first parameter must be a pointer which contains the
value returned from a previous allocation or NULL.

You never use the value returned by realloc.  You seem to think that
somehow fieldsize will be changed to point to the newly allocated
area.  Remember that c passes all function arguments by value so it is
not possible for a function to change an argument.

Why are you using realloc instead of malloc?

- Show quoted text -

Quote:

>for (i = 0; i < no_fields; i++) {        // Get all the field sizes

>printf("Please enter the size of field %d ",i+1);
>scanf("%d",&fieldsize[i]);

>}

>/*************** Debug code ******************/

>printf("\nThe input file is %s and output is to
>%s\n",argv[1],argv[2]);

>for (i = 0; i < no_fields; i++) printf("\nField %d has %d
>charcters\n\n",i+1,fieldsize[i]);

>scanf("%d",x);

>/************************************************/
>return 0;
>}
>.

<<Remove the del for email>>
--



Mon, 03 Oct 2005 14:52:42 GMT  
 Dynamic Alloc - Arrays
On 16 Apr 2003 00:23:54 GMT

Quote:

> I am new to C and trying to write a little utility programme to
> manipulate records in a file.   I have written some code to get input
> on the number of fields in a record and their size.   I want to
> dynamically declare an array to hold the field sizes and have used
> realloc() in the code below but have a strong feeling that I am doing
> something wrong (i.e. I could be writing over other data).  Could do
> with an opinion.  (I have removed the indents to avoid too many split
> lines hope this does not cause a problem.

Please don't remove all indenting, it makes it MUCH harder to read. In
lines being split is a problem then reformat the code manually splitting
the lines as required, make sure it still compilers, then submit it.

Quote:
> /*********************************************/

> #include <stdio.h>
> #include <stdlib.h>

> int main(int argc,char *argv[]) {

> unsigned short int i, no_fields, fieldsize[1], x = 0; // x is for

                                   ^^^^^^^^^
unsigned short *temp, *fieldsize=NULL;
/* You can't realloc an array, only space that was allocated by realloc
or malloc */

Quote:
> debuging only

// style comments are a pain when posting to Usenet.

Quote:
> /*if(argc != 3){
> printf("\nIncorrect usage:\n\nCorrect usage = exe filename
> [InputFilename] [OutputFilename]\n\nTry again please\n");

> exit(0);
> }*/

> printf("\nHow many fields are there in a record?");

> scanf("%d",&no_fields); printf("\n");

> realloc(fieldsize,sizeof(unsigned short int)*no_fields);

/* Realloc returns a pointer to the memory on success or NULL on
failure, it does not EVER modify its parameters. */
temp=realloc(fieldsize, fields * sizeof *fieldsize);
if (temp==NULL) {
  /* handless failed realloc */
Quote:
}

fieldsize=temp;

/* I've not checked the rest. However, I suggest you read up further
on memory allocation. */

// <snip rest of code>
--
Mark Gordon
Paid to be a Geek & a Senior Software Developer
Currently looking for a new job commutable from Slough, Berks, U.K.
Although my email address says spamtrap, it is real and I read it.
--



Mon, 03 Oct 2005 14:53:36 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Dynamic alloc. in functions

2. non-local pointers to c(m)alloc'ed arrays

3. HELP: ways to alloc multidimensional arrays

4. global alloc and numeric arrays

5. How access alloc'ed string as array?

6. HELP: Dynamically Alloc'd array

7. (*)alloc for large arrays?

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

9. How to inspect dynamic array ( my_struct* array) ?

10. Control Arrays/Dynamic Arrays

11. Lists VS. Dynamic Arrays VS. Re-defining Arrays

12. HELP -> dynamic array of char** in dynamic array of mystruct**

 

 
Powered by phpBB® Forum Software