LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-15-2013, 09:02 PM   #1
chaoxifer
LQ Newbie
 
Registered: Nov 2010
Location: Seoul, South Korea
Distribution: Arch, Gentoo
Posts: 15

Rep: Reputation: 0
What difference between these initializing array?


I'm studying linux kernel source and I found the definition like this:

static pidmap_t pidmap_array[PIDMAP_ENTRIES] =
{ [ 0 ... PIDMAP_ENTRIES-1 ] = { ATOMIC_INIT(BITS_PER_PAGE), NULL } };

Why maintainer coded the source like this?
I think they could make the code like this:
static pidmap_t pidmap_array[PIDMAP_ENTRIES] = { ATOMIC_INIT(BITS_PER_PAGE), NULL };

I want to know the difference between them.
 
Old 10-18-2013, 02:31 PM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
The first style is a C extension to initialize all elements of the array. Are you saying that if one is initializing all array elements that the second notation works? I don't think the second one works, I think you initialize only two array elements with that second variation.
 
2 members found this post helpful.
Old 10-21-2013, 02:42 AM   #3
chaoxifer
LQ Newbie
 
Registered: Nov 2010
Location: Seoul, South Korea
Distribution: Arch, Gentoo
Posts: 15

Original Poster
Rep: Reputation: 0
Thanks for reply..

Thanks for reply.

I thought that
Code:
static pidmap_t pidmap_array[PIDMAP_ENTRIES] = { ATOMIC_INIT(BITS_PER_PAGE), NULL };
is possible because all entry has same value as ATOMIC_INIT(BITS_PER_PAGE),NULL -> That's because pidmap_t is a structure defined as

Code:
typedef struct pidmap {
	atomic_t nr_free;
	void *page;
} pidmap_t;
As we can initialize some array with the way like
Code:
int arr[arrSize]={0};
I thought that the code can be simplified using that way... In my thought, the developer use a C extension you mentioned for explicitly initializiing the value.... Am I wrong??
 
Old 10-21-2013, 09:10 AM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by chaoxifer View Post
Am I wrong??
Unfortunately yes, but my initial answer didn't settle the issue completely either. So in the future we both can remember that one can code simple stuff like this and validate or invalidate an area of question.

Test source code. Note the differences in the initializations of test_array_1 versus test_array_2:

Code:
#include <stdio.h>
#include <stdlib.h>

typedef struct {
    int a;
    char bee[8];
}sTest_t;

void main(void)
{
    sTest_t test_array_1[5] = { [ 0 ... 4 ] = { 1, "one" } };
    sTest_t test_array_2[10] = { 2, "two" };
    int i;

    for(i = 0; i < 5; i++) {
        printf("Element: %d Values: %d %s\n", i, test_array_1[i].a, test_array_1[i].bee);
    }
    for(i = 0; i < 10; i++) {
        printf("Element: %d Values: %d %s\n", i, test_array_2[i].a, test_array_2[i].bee);
    }
}
The results:

Code:
~/testcode$ ./extension 
Element: 0 Values: 1 one
Element: 1 Values: 1 one
Element: 2 Values: 1 one
Element: 3 Values: 1 one
Element: 4 Values: 1 one
Element: 0 Values: 2 two
Element: 1 Values: 0 
Element: 2 Values: 0 
Element: 3 Values: 0 
Element: 4 Values: 0 
Element: 5 Values: 0 
Element: 6 Values: 0 
Element: 7 Values: 0 
Element: 8 Values: 0 
Element: 9 Values: 0 
~/testcode$
 
1 members found this post helpful.
Old 10-22-2013, 01:45 AM   #5
chaoxifer
LQ Newbie
 
Registered: Nov 2010
Location: Seoul, South Korea
Distribution: Arch, Gentoo
Posts: 15

Original Poster
Rep: Reputation: 0
Smile Thank you ~!!

Thanks for reply.

Now I can understand!!..

Although it is basical grammer(?), I didn't know about that...shamefully.. oTL

I should learn C language, including GCC compiler extention.

Thank you rtmistler!!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with allocation and initializing in dynamic 2dimentional array. alireza976 Programming 3 07-03-2011 03:25 AM
C++; Problems initializing static member array l_long_island Programming 5 06-07-2011 05:32 PM
Initializing drive array command for class lab on fedora 12 stefnthebox Linux - Newbie 4 02-12-2010 06:36 AM
Initializing new array tbone63078 Red Hat 1 07-25-2007 02:00 PM
Initializing Array of Strings jrtayloriv Programming 10 02-03-2005 12:22 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration