-2

What kind of issues could allocations for the above will arrise? If we allocated size of the actual data type rather than the size of a pointer of that type?

Would it be an issue for chars as sizeof (char*) > sizeof (char)? What about other data types and user defined structs?

Thanks

user3259045
  • 89
  • 1
  • 5
  • [Related question](http://stackoverflow.com/questions/8762278/are-mallocsizeofstruct-a-and-mallocsizeofstruct-a-the-same) – kbshimmyo Feb 13 '14 at 19:35
  • http://4.bp.blogspot.com/-EaB-P8Jbp_Y/T-pVx1aJ3VI/AAAAAAAAAtI/pnathflam9Q/s1600/keep-calm-and-do-your-homework.jpg – michaelmeyer Feb 13 '14 at 20:05

3 Answers3

0

By first malloc function,you are trying to allocate memory to store pointers to char, But by 2nd statement, you are trying to allocate the 'char' data types in array, which compiler would actually accept,but if later on if you try to access memory which you think you had allocated will have undefined behavior.

Hence, you may want to use actual data types on the right side, as your left side.

And, for 2nd part of your question->You can allocate any type of memory on the heap by using malloc() function. e.g you can allocate array of struct as

struct emp **array=malloc(sizeof(struct emp *)*len)

I hope this helped.

  • 1
    Why do you think that won't compile? `malloc` isn't picky about what you're using the memory for. – Paul Feb 13 '14 at 19:16
  • Something like `char * a = malloc(sizeof(long)*600/(4*sizeof(float)));` will also compile just fine. It's just less likely to have any use cases. – Paul Feb 13 '14 at 19:18
  • Are you aware of any wrong combinations that will compile? – user3259045 Feb 13 '14 at 19:18
  • I accept malloc() won't be picky about size, but if lateron you try to access the memory which is not there, then there should be run time error. – ganeshwani Feb 13 '14 at 19:19
  • @ganeshwani Yes, it will invoke undefined behaviour if you use unallocated memory, but there are some use cases where you actually want a `char *` to point to the memory location where you have longs stored. It's not incorrect code, it's just not useful as often. – Paul Feb 13 '14 at 19:22
  • @Paulpro:Ok.Let me modify an answer to make it useful and more precise. – ganeshwani Feb 13 '14 at 19:24
0

If you want enough memory for len char pointers then use sizeof (char*)*len.

It really just depends on how much memory you want, but generally you want a char ** to point to one or more char *, so it is much more likely that you want enough memory for len char pointers, which is almost always more memory than len chars.

Paul
  • 139,544
  • 27
  • 275
  • 264
0

Use type *variable = malloc(sizeof(*variable) * len) and avoid the issue.

Example: char ** array = malloc(sizeof(*array) * len);


char ** array = malloc (sizeof (char*)*len) allocates memory correctly.

char ** array = malloc (sizeof (char)*len) only allocate enough memory correctly if sizeof(char) == sizeof(char*) which is certainly false except in an exceptional platform.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256