0

I have some misunderstanding about VS C++. In version 2010 the code below works fine: I can get a string and I can free memory afterwords.

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

 #define MAX 14

 void GetString(char **str);

 int main(int argc, char *argv[])
 {
    char *str = NULL;
    GetString(&str);
    printf("%s\n", str);

    getchar();

    free(str);

    return 0;
 }

 void GetString(char **str)
 {
    char *s = (char *) malloc(sizeof(char) * MAX);
    strcpy(s, "HELLO, WORLD!");
    *str = s;
 }

But in VS 2008 the code above will end up with memory corruption error. I guess, there is a small difference in standards used. Am I right? If no, could you, please, tell me, why the same code doesn't work in different versions of the Visual Studio?

Thank you beforehand for your answers.

p.s. I'm curious of what happens, but couldn't yet find any information on the topic.

p.p.s. Language used - C

user1415536
  • 236
  • 1
  • 14
  • 26
  • 1
    when giving code examples, make them compilable by including all the necessary headers (in your case `` `` and ``) – TemplateRex Feb 05 '13 at 13:16
  • I'm sorry. I'll make changes right now! – user1415536 Feb 05 '13 at 13:21
  • 3
    Well... I copy paste your code in VS2008, built it and run it... and guess what? It works. I think thiere is something you did you are not telling us (like having GetString function declared in a Dll ?) – Kek Feb 05 '13 at 13:28
  • @Kek No, I didn't. I told you as it was in that morning. I don't doubt it has to work. – user1415536 Feb 05 '13 at 13:47

1 Answers1

1

You're not including the required headers, which means your code could be interpreted differently by different compilers. You should add:

#include <stdlib.h>

to make malloc() a well-defined function.

You're also calling strcpy(), so you need:

#include <string.h>

and doing I/O, for which you need:

#include <stdio.h>

Also, in C this:

char *s = (char *) malloc(sizeof(char) * MAX);

is better written as:

char *s = malloc(MAX);

since

  1. It's a bad idea to cast the return value of malloc()
  2. sizeof (char) is always 1, so it just adds clutter.

Finally, you should of course check that malloc() succeeds before using the returned pointer.

Community
  • 1
  • 1
unwind
  • 391,730
  • 64
  • 469
  • 606
  • I'm sorry, I didn't copy all code from the file. And I can't mark this as an answer either. Because it's a simplest example I could think off. Also if you will not cast malloc in VS it will show you an error! 1 IntelliSense: a value of type "void *" cannot be used to initialize an entity of type "char *" – user1415536 Feb 05 '13 at 13:23
  • Moreover, if malloc will fail, it will return NULL anyway. And the s -variable in the method will be NULL. http://stackoverflow.com/questions/11788803/what-if-malloc-fails So, yes, I have a potentional risk everything will blow up. But there was a bit different question. – user1415536 Feb 05 '13 at 13:35
  • 1
    @user1415536: You're trying to compile it as C++ aren't you? Try compiling it as C instead, since you know, it is C. – netcoder Feb 05 '13 at 13:36
  • @netcoder it will work in Linux(gcc will not show any exeptions or warnings). I've already tried it. – user1415536 Feb 05 '13 at 13:38
  • @netcoder: this seems to be a [known issue in Visual Studio](http://stackoverflow.com/a/3143815/69809), even in VS2015; intellisense treats C files as C++, so even if the source actually compiles without warnings, it will sometimes show a bunch of errors. – vgru Mar 22 '17 at 02:17