1

This is an C assignment for school but I'm running into to something strange that I don't know if it is normal or not.

I have to take command line arguments and an example of one is

-ia.b

so within my program I dynamically allocate memory with malloc

char *fileName = NULL;
fileName = malloc(strlen(argv[i]) * sizeof(char));
//error testing etc
strcpy(fileName, argv[i]);

Works fine, but I look into memory via visual studio debugger this is what become allocated at the memory location which to me is more room then needed:

0x01608b98 "ÍÍÍÍÍýýýýB`\x1˜?`\x1\xf1¼O{º"

if I cast malloc such as so fileName = (char*)malloc(strlen(argv[i]) * sizeof(char)); I get this allocated in memory:

0x009d8d38 "ÍÍÍÍÍýýýýB"

Considering that my argument is 5 bytes, is malloc allocating more memory then it should or am I just doing this incorrectly?

jww
  • 97,681
  • 90
  • 411
  • 885
Robolisk
  • 1,682
  • 5
  • 22
  • 49
  • 2
    It's entirely possible it will allocate a bit more than you ask for. But I'm not clear on how your determining how much is being allocated. – Fred Larson Feb 16 '17 at 19:51
  • Yes, malloc implementations often allocate more memory than you request for internal use. – Marco Feb 16 '17 at 19:51
  • 3
    Quite possibly. But that won't **protect you from the shortfall** where the memory allocated is exact, because `strlen(argv[i])` should be `(strlen(argv[i])+1)` to allow for string terminator for the subsequent `strcpy`. – Weather Vane Feb 16 '17 at 19:52
  • 2
    Also note that `sizeof(char)` is 1 by definition. `* sizeof(char)` really isn't necessary. – Fred Larson Feb 16 '17 at 19:53
  • 2
    Possible duplicate of [Why does malloc allocate a different number of bytes than requested?](http://stackoverflow.com/questions/430163/why-does-malloc-allocate-a-different-number-of-bytes-than-requested) – msc Feb 16 '17 at 19:54
  • Yes, it most probably is. No, you shouldn't worry about this at all. Yes, writing beyond the size that you requested is always a Bad Thing (TM). – Daniel Kamil Kozar Feb 16 '17 at 19:54
  • 2
    Also, please see [Do I cast the result of malloc?](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – Daniel Kamil Kozar Feb 16 '17 at 19:56
  • This is all very interesting. – Robolisk Feb 16 '17 at 20:04
  • 1
    The debugger doesn't know how much was allocated, it just shows a certain amount of memory regardless – M.M Feb 16 '17 at 20:38
  • 1
    To toss in another comment, I believe you should malloc `strlen(argv[i])+1` to make room for the terminating NULL. The subsequent `strcpy` will write the NULL even if you don't allocate for it. – jww Feb 16 '17 at 22:49
  • w.r.t the unnecessary multiply, in general a cleaner alternative to multiplying explicitly, i.e. `n * sizeof(T)` is to instead fold the buffer length into the size, and ask `malloc` to allocate `sizeof(T[n])`. This is visually more elegant and it reflects how you most likely intend to use the space. – Alex Celeste Feb 16 '17 at 22:50
  • 1
    Also see [In Visual Studio C++, what are the memory allocation representations?](http://stackoverflow.com/q/127386/608639) to understand when the bit patterns are in effect and what the bit patterns mean. – jww Feb 16 '17 at 22:52

1 Answers1

3

Usually malloc allocates memory in chunks exactly divisible by the paragraph size equal to 16 bytes (that is malloc allocates chunks with a fundamental alignment requirement). However you should not rely on this internal behavior of malloc. Otherwise the behavior is undefined.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335