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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
12-01-2009, 05:38 PM
|
#1
|
Member
Registered: Nov 2004
Location: UK
Distribution: Fedora 12
Posts: 129
Rep:
|
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.
|
|
|
12-01-2009, 05:42 PM
|
#2
|
Member
Registered: Nov 2009
Location: Lisbon, Portugal
Distribution: Gentoo, CentOs, Ubuntu, Debian
Posts: 182
Rep:
|
Quote:
Originally Posted by Cyhaxor
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.
|
|
|
12-01-2009, 05:58 PM
|
#3
|
Member
Registered: Nov 2004
Location: UK
Distribution: Fedora 12
Posts: 129
Original Poster
Rep:
|
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.
|
|
|
12-01-2009, 06:06 PM
|
#4
|
Member
Registered: Nov 2009
Location: Lisbon, Portugal
Distribution: Gentoo, CentOs, Ubuntu, Debian
Posts: 182
Rep:
|
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.
|
|
|
12-01-2009, 06:06 PM
|
#5
|
LQ Guru
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 11,377
|
It contains garbage. It's not initialized to any particular value...
Can you spell, "segfault?"
|
|
|
12-01-2009, 06:17 PM
|
#6
|
Member
Registered: Nov 2004
Location: UK
Distribution: Fedora 12
Posts: 129
Original Poster
Rep:
|
Ok I understand! Thank you very much 
|
|
|
12-01-2009, 06:30 PM
|
#7
|
Member
Registered: Dec 2007
Location: Slightly left of center
Distribution: slackware
Posts: 276
Rep:
|
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.
|
|
|
12-01-2009, 06:36 PM
|
#8
|
Member
Registered: Nov 2009
Location: Lisbon, Portugal
Distribution: Gentoo, CentOs, Ubuntu, Debian
Posts: 182
Rep:
|
Quote:
Originally Posted by raconteur
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?
|
|
|
|
12-01-2009, 07:25 PM
|
#9
|
Member
Registered: Sep 2006
Location: Munich, Germany
Distribution: Debian / Ubuntu
Posts: 298
Rep:
|
Quote:
Originally Posted by raconteur
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.
|
12-02-2009, 09:04 AM
|
#11
|
LQ Guru
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 11,377
|
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.
|
|
|
12-02-2009, 01:34 PM
|
#12
|
Member
Registered: Dec 2007
Location: Slightly left of center
Distribution: slackware
Posts: 276
Rep:
|
Quote:
Originally Posted by rupertwh
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.
|
|
|
12-02-2009, 01:38 PM
|
#13
|
LQ Guru
Registered: Dec 2007
Distribution: Centos
Posts: 5,286
|
Quote:
Originally Posted by raconteur
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.
|
|
|
12-02-2009, 02:05 PM
|
#14
|
Member
Registered: Dec 2007
Location: Slightly left of center
Distribution: slackware
Posts: 276
Rep:
|
Quote:
Originally Posted by johnsfine
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.
|
|
|
12-02-2009, 02:36 PM
|
#15
|
Member
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811
Rep: 
|
Quote:
Originally Posted by johnsfine
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.
|
|
|
All times are GMT -5. The time now is 09:31 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|