LinuxQuestions.org
Review your favorite Linux distribution.
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 12-01-2009, 05:38 PM   #1
Cyhaxor
Member
 
Registered: Nov 2004
Location: UK
Distribution: Fedora 12
Posts: 129

Rep: Reputation: 15
Array unallocated space in c


Hello there,

I am trying to determine whether a particular element in an array is empty in C without using malloc() standard library function. Here is an example code:

Code:
#include <stdio.h>

int main(void)
{
  int array[5];
  array[0] = 10;
  array[1] = 20;

  /* a code that checks each element of the array and determine if it contains a value or its empty */
}
I've tried if(array[2] == NULL) but it doesn't seem to work.

Notice: that not all the elements are initializes therefore [2-4] must be empty(NULL(?)).

Thanks in advance.
 
Old 12-01-2009, 05:42 PM   #2
ammorais
Member
 
Registered: Nov 2009
Location: Lisbon, Portugal
Distribution: Gentoo, CentOs, Ubuntu, Debian
Posts: 182

Rep: Reputation: 49
Quote:
Originally Posted by Cyhaxor View Post
Hello there,

I am trying to determine whether a particular element in an array is empty in C without using malloc() standard library function. Here is an example code:

Code:
#include <stdio.h>

int main(void)
{
  int array[5];
  array[0] = 10;
  array[1] = 20;

  /* a code that checks each element of the array and determine if it contains a value or its empty */
}
I've tried if(array[2] == NULL) but it doesn't seem to work.

Notice: that not all the elements are initializes therefore [2-4] must be empty(NULL(?)).

Thanks in advance.
A C array allways points to an address on memory(and it reserves this space). The value of array is undetermined before any value is assigned.
The array space is allocated at the beginning of the program, on the contrary with malloc that dynamically allocates space.

[EDIT]
There's no such thing as NULL values. There are NULL pointers(pointers that point to 0). For example a byte(char) variable has allwas a value between 0-255. There can't be "char var = NULL". A pointer can be NULL: "char* var == NULL)

Last edited by ammorais; 12-01-2009 at 05:45 PM.
 
Old 12-01-2009, 05:58 PM   #3
Cyhaxor
Member
 
Registered: Nov 2004
Location: UK
Distribution: Fedora 12
Posts: 129

Original Poster
Rep: Reputation: 15
Ok I can see what you're trying to say but other than that, is there any way to determine that a particular element of an array has not been initialized to any value i.e the programmer has not manually specified the value of an element at a particular index?

[EDIT]
Actually what I am trying to do is to set a pointer pointing on the address of the first "empty" element in an array.

Last edited by Cyhaxor; 12-01-2009 at 06:03 PM.
 
Old 12-01-2009, 06:06 PM   #4
ammorais
Member
 
Registered: Nov 2009
Location: Lisbon, Portugal
Distribution: Gentoo, CentOs, Ubuntu, Debian
Posts: 182

Rep: Reputation: 49
No. Take look at this example

unsigned char var[10];

Each position of the array will always have an undetermined value between 0-255 before any value is assigned.
for example:
var[0] can have any value from 0-255. Even if the value was 0(there is the possibility of some compilers initialize the array to 0, but I don't know any), it's impossible to know if it was a value assigned or not.
An alternative method is a dynamic array of pointers initialized with NULLS.
 
Old 12-01-2009, 06:06 PM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,707
Blog Entries: 4

Rep: Reputation: 3949Reputation: 3949Reputation: 3949Reputation: 3949Reputation: 3949Reputation: 3949Reputation: 3949Reputation: 3949Reputation: 3949Reputation: 3949Reputation: 3949
It contains garbage. It's not initialized to any particular value...

Can you spell, "segfault?"
 
Old 12-01-2009, 06:17 PM   #6
Cyhaxor
Member
 
Registered: Nov 2004
Location: UK
Distribution: Fedora 12
Posts: 129

Original Poster
Rep: Reputation: 15
Ok I understand! Thank you very much
 
Old 12-01-2009, 06:30 PM   #7
raconteur
Member
 
Registered: Dec 2007
Location: Slightly left of center
Distribution: slackware
Posts: 276
Blog Entries: 2

Rep: Reputation: 44
Why not initialize each element of the array to an invalid value ( 0 or -1 or whatever you wish)?

Code:
  int array[5];
  memset(array, -1, 5);

  array[0] = 10;
  array[1] = 20;
Daring to venture into the realm of style, I recommend always initializing variables of any type, arrays in particular.

Last edited by raconteur; 12-01-2009 at 06:31 PM.
 
Old 12-01-2009, 06:36 PM   #8
ammorais
Member
 
Registered: Nov 2009
Location: Lisbon, Portugal
Distribution: Gentoo, CentOs, Ubuntu, Debian
Posts: 182

Rep: Reputation: 49
Quote:
Originally Posted by raconteur View Post
Why not initialize each element of the array to an invalid value ( 0 or -1 or whatever you wish)?

Code:
  int array[5];
  memset(array, -1, 5);

  array[0] = 10;
  array[1] = 20;
Daring to venture into the realm of style, I recommend always initializing variables of any type, arrays in particular.
I was going to also suggest that but OP specified specifically that it wanted to know before the array is actually initialized.

Quote:
i.e the programmer has not manually specified the value of an element at a particular index?
 
Old 12-01-2009, 07:25 PM   #9
rupertwh
Member
 
Registered: Sep 2006
Location: Munich, Germany
Distribution: Debian / Ubuntu
Posts: 297

Rep: Reputation: 49
Quote:
Originally Posted by raconteur View Post
Code:
  int array[5];
  memset(array, -1, 5);
That memset() doesn't do what you meant it to do...
 
1 members found this post helpful.
Old 12-01-2009, 07:29 PM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,375

Rep: Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755Reputation: 2755
There's a good discussion of array initialisation here http://stackoverflow.com/questions/1...-default-value
 
Old 12-02-2009, 09:04 AM   #11
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,707
Blog Entries: 4

Rep: Reputation: 3949Reputation: 3949Reputation: 3949Reputation: 3949Reputation: 3949Reputation: 3949Reputation: 3949Reputation: 3949Reputation: 3949Reputation: 3949Reputation: 3949
When a standard "C" array is allocated, its contents are unpredictable. It is literally impossible, therefore, to determine if the value is garbage or not.

This is why languages like C++ and Delphi make various promises to you about what newly-allocated memory blocks shall contain. And, it's why they make so much use of dynamic storage allocation, and dynamic data structures.

After all, there are really two reasons for using "an array." One is that you actually want to reserve and refer-to a contiguous block of bytes. The other, more common purpose is that you want to refer to a collection of items by means of a numeric index. For the latter purpose, an array has the disadvantage of being of a fixed size, and also wasteful if the actual distribution of values is sparse.

There's a reason why "C" is used most-often as "a tool to build other tools in." I am not speaking against it as a tool; merely pointing out that it is a comparatively low-level tool.
 
Old 12-02-2009, 01:34 PM   #12
raconteur
Member
 
Registered: Dec 2007
Location: Slightly left of center
Distribution: slackware
Posts: 276
Blog Entries: 2

Rep: Reputation: 44
Quote:
Originally Posted by rupertwh View Post
That memset() doesn't do what you meant it to do...
I know, I was in a hurry.

Code:
  memset(array, -1, sizeof(int) * 5);
Fixed.
 
Old 12-02-2009, 01:38 PM   #13
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by raconteur View Post
I know, I was in a hurry.

Code:
  memset(array, -1, sizeof(int) * 5);
Looks like you were still in a hurry. Your fixed version is correct, but not good. An arbitrary constant (the 5) in one place is semi OK. But in two places that need to match each other is bad code. Even needing int in both places is questionable. Simpler and better:

Code:
  memset(array, -1, sizeof(array));

Last edited by johnsfine; 12-02-2009 at 01:40 PM.
 
Old 12-02-2009, 02:05 PM   #14
raconteur
Member
 
Registered: Dec 2007
Location: Slightly left of center
Distribution: slackware
Posts: 276
Blog Entries: 2

Rep: Reputation: 44
Quote:
Originally Posted by johnsfine View Post
Looks like you were still in a hurry. Your fixed version is correct, but not good. An arbitrary constant (the 5) in one place is semi OK. But in two places that need to match each other is bad code. Even needing int in both places is questionable. Simpler and better:

Code:
  memset(array, -1, sizeof(array));
Agreed.
 
Old 12-02-2009, 02:36 PM   #15
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by johnsfine View Post
Simpler and better:

Code:
  memset(array, -1, sizeof(array));
In this precise context, yes. But it's not a good overall formula. Consider the following code snippet:
Code:
#define ELEMENT_COUNT 5
int *array;
array=malloc(ELEMENT_COUNT*sizeof(int));
if(array==NULL)
{
  /* error recovery code goes here */
}
else
{
  /* wipes out not the array, but the pointer: */

  memset(array,-1,sizeof(array));
}
I know many people here will know this already, but I want to make it explicit for anyone else.

Edit:

I blew it. This code does wipe out part of the array, but only the first n bytes of the array, where n is the size of a pointer in your system.

Last edited by wje_lq; 12-02-2009 at 02:47 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Assign unallocated space to existing partition. EricTRA Debian 5 09-16-2009 01:49 AM
Giving Unallocated space to /home mickeyboa Fedora 3 06-30-2009 02:36 PM
Creating and formatting a partion from unallocated space. Lassenc Linux - Newbie 4 03-20-2008 05:09 PM
partioning unallocated space Ledft Linux - Newbie 4 05-30-2006 11:23 PM
Format ext3 ONLY on unallocated space X-Crawler Linux - General 3 03-16-2004 11:27 AM

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

All times are GMT -5. The time now is 01:27 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