0

I don't know what is the correct way to allocate memory dynamically:
I have a .csv file which has like 50 lines and I need to allocate enough space in memory to save each line of the file in one space of the struct vector.

Also, I know that the return of malloc is the first position of the allocated memory space.

Example:

typedef struct{
   int a;
   float b;
   char name[10]; //This will be set dynamically too, later...
}my_struct;

int main(){
   int *p_array;
   size_t vector_size = 2;  //Same as doing:  my_struct struc[2]   ?

   p_array = (int *) malloc(vector_size * (int));
   my_struct struc[p_array];

   return 0;
}  

Is it right? If not which is the right way of doing it. I got no errors, but I don't know why it doesn't seems right.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
PlayHardGoPro
  • 2,791
  • 10
  • 51
  • 90
  • 3
    What is this `my_strict struc[p]` for? and whre is `p` defined? Your code does not compile, you must post a compilable example. The first answer I can give is that it's wrong because it doesn't compile. – Iharob Al Asimi Mar 14 '15 at 14:23
  • @iharob I edited my question. Take a look at this and let me know if it still confused. I need to create a vector of my struct, but it's size has to be set dynamically according to the number of lines in my *.csv* file. – PlayHardGoPro Mar 14 '15 at 14:25
  • 1
    @PlayHardGoPro It's still not clear what `p` is (it's never defined in your program).. Also, your `//Same as doing: my_struct struc[2]` comment isn't very clear.. What do you mean by it? – Daniel Kleinstein Mar 14 '15 at 14:26
  • 3
    "*I got no errors*" I doubt this. – alk Mar 14 '15 at 14:28
  • Do each line in the file have a constant number of characters? – Spikatrix Mar 14 '15 at 14:31
  • @CoolGuy No, it vary. It's content is like: *name,AccNumber,cash...* – PlayHardGoPro Mar 14 '15 at 14:33

1 Answers1

3

It's completely wrong, starting here

  1. This is wrong

    p_array = (int *) malloc(vector_size * (int));
    /*                                       ^ what is this? */
    

    if you want an array of integers of vector_size size you need

     p_array = malloc(vector_size * sizeof(int));
     /*                                ^ the sizeof operator */
    
  2. This doesn't make sense at all

    my_struct struc[p_array];
    

    maybe you mean?

    my_struct struc[vector_size];
    

    above you are passing a pointer where an integer should go, if this compiles then what is happening is that the address stored in the pointer is evaluated as an integer and hence your struc array has a size very different than what you think

    If you use the corrected version of this, the malloc() makes absolutely no sense, so you don't really need it.

If you want to allocate the struc array dynamically then

my_struct *array;

array = malloc(elementCount * sizeof(my_struct)):
if (array == NULL)
    pleaseDoNot_Use_array_AndProbably_AbortHere();
/* you can use it here, and when you finish */
free(array);

If you enable compiler warnings there will be a few warnings that will let you know some of the things I mentioned above.

Also, in c There is no need to cast malloc()

Community
  • 1
  • 1
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97