0

I'm a beginner in C and want to ask the difference in memory allocation procedure in the 2 different codes mentioned below:

#define MAX_LEN 10000
int main()
{
char str_new[MAX_LEN];
...
}


int main()
{
char str_new[10000];
...
}

Are these two not the same essentially?? Will memory not be allocated in the same way in these two? This was not answered in the questions I searched. Although both different methods were used frequently.

3
  • 1
    You need to understand the different phases of compilation. Try to read a few stuff on the preprocessor. Commented Aug 10, 2013 at 10:57
  • Yep, they're the same -- a direct character substitution is performed before the actual compilation, so you end up compiling the same characters. The thing to beware of is coding #define MAX_LEN = 10000 or #define MAX_LEN 10000;, as those include characters you don't want to substitute. Commented Aug 10, 2013 at 10:58
  • 1
    Also, if you ever include an expression, surround it with (). Eg, if you code #define MY_CONST 5 + 10 and then int x = MY_CONST * 3;, you'll get int x = 5 + 10 * 3; and a result of 35 rather than 45. So code #define MY_CONST (5 + 10). Commented Aug 10, 2013 at 11:20

5 Answers 5

3

They are both equivalent. In the first program macro MAX_LEN is replaced with 10000 by simple textual substitution.

Sign up to request clarification or add additional context in comments.

2 Comments

so how can we improve the memory allocation while taking input, a string with characters< 10000 where the user can even put 10 characters?
@user2669913 1. You ask the user for the length of the input, then you allocate a VLA. Or, 2. you use a sufficiently small (yet large enough), constant-sized buffer, get the user input using fgets(), then append it to the end of a dynamically-expansible buffer.
1

In you code , the are the same. And in computer view, they always the same.

However, thinking if you have

char str_new_o1[10000];
char str_new_02[10000];
char str_new_03[10000];
char str_new_04[10000];
char str_new_05[10000];
 ...

char str_new_100[10000];

Even maybe they are in different files in differnt floders.

And all arrays should have the same size(because they have the same meaning ). well they are now.

But ,other day , you find you need change the size of arrays, you need 2000 now, so you need change all of those arrays , one forget ,lots errors. Now , we call the 1000 a Magic Number .

While , if you define 1000 as a marco , you only need change once ,nothing need worry.

So , they are different in our view, if you only have one array, use the number directly , because a not necessary marco is evil , while if you have lots of same size arrays , use a marco to avod magic number .

Comments

0

Yes, both the above mentioned methods will allocate the same amount of memory for str_new.

This is because static memory allocation is done at compile time where as preprocessor directives are resolved at the time of preprocessing.

Comments

0

During the compile process, the MAX_LEN in your main function will be substituted by your #define MAX_LEN value, namely, 10000. also, ALL the MAX_LEN should be substituted. and I think #define MAX_LEN will no longer exist in your symbol list after compile

Comments

0

They are both the same.

From the standard :

6.10.3/9 Macro replacement

A preprocessing directive of the form

#define identifier replacement-list new-line

defines an object-like macro that causes each subsequent instance of the macro name to be replaced by the replacement list of preprocessing tokens that constitute the remainder of the directive.

It means that during the preprocessing step you compiler will replace all the occurence of MAX_LEN by its value. So :

#define MAX_LEN 10000

char str_new[MAX_LEN];

is simply translated in :

char str_new[10000];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.