473,609 Members | 2,241 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

variable length array (was: question about dynamic memory allocation)

Hi,

I decided to start a new topic about my memory allocation question. One
of the answers was that variable length arrays are possible using C99. I
found this topic that also mentions it.

http://groups.google.com/group/comp....335d22d6e6fb34

I did some test, but I get strange results:

int size_array = sizeof(struct ip) + cnf->msg_length;
unsigned char packet[size_array];

And then, when asking the size of the 'packet' variable I get 4. How is
that? It should be another number (31).. but I guess i'm getting the
size of type unsigned char. Right?
Thanks,

FBM
Nov 15 '05 #1
8 1866
Fernando Barsoba wrote:
I decided to start a new topic about my memory allocation question. One
of the answers was that variable length arrays are possible using C99. I
found this topic that also mentions it.

http://groups.google.com/group/comp....335d22d6e6fb34
I did some test, but I get strange results:

int size_array = sizeof(struct ip) + cnf->msg_length;
unsigned char packet[size_array];

And then, when asking the size of the 'packet' variable I get 4. How is
that? It should be another number (31).. but I guess i'm getting the
size of type unsigned char. Right?

Technically there's no way to tell, because we don't know what system
you're compiling this on.

That said, you're probably getting the size of an unsigned char*, to
which `packet' decays.

If sizeof packet != size_array after the declarations above, your
compiler does not conform to the C99 standard. gcc in particular does
not support VLAs; it supports an earlier gcc extension that is similar
but not identical to C99 VLAs.

S.
Nov 15 '05 #2
Fernando Barsoba wrote:
Hi,

I decided to start a new topic about my memory allocation question. One
of the answers was that variable length arrays are possible using C99. I
found this topic that also mentions it.

http://groups.google.com/group/comp....335d22d6e6fb34
I did some test, but I get strange results:

int size_array = sizeof(struct ip) + cnf->msg_length;
unsigned char packet[size_array];

And then, when asking the size of the 'packet' variable I get 4. How is
that? It should be another number (31).. but I guess i'm getting the
size of type unsigned char. Right?


Show us the code you used to test.
[You passed `packet' to a function, didn't you...]

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com (new post 8/5)
http://www.cafepress.com/goldsays
"If you have nothing to hide, you're not trying!"
Nov 15 '05 #3
Artie Gold wrote:
Fernando Barsoba wrote:
Hi,

I decided to start a new topic about my memory allocation question. One
of the answers was that variable length arrays are possible using C99. I
found this topic that also mentions it.

http://groups.google.com/group/comp....335d22d6e6fb34
I did some test, but I get strange results:

int size_array = sizeof(struct ip) + cnf->msg_length;
unsigned char packet[size_array];

And then, when asking the size of the 'packet' variable I get 4. How is
that? It should be another number (31).. but I guess i'm getting the
size of type unsigned char. Right?


Show us the code you used to test.
[You passed `packet' to a function, didn't you...]


I pass 'packet' to a function later on.. but not for this test. I
checked the size of 'packet' after
unsigned char packet[size_array];

and there's where I get the value 4.

Regarding to the other question, I'm using Eclipse + CDT (and the
compiler is the one that comes with Cygwin which I am using too).

FBM
Nov 15 '05 #4
Fernando Barsoba <fb******@veriz on.net> writes:
[snip]
I did some test, but I get strange results:

int size_array = sizeof(struct ip) + cnf->msg_length;
unsigned char packet[size_array];

And then, when asking the size of the 'packet' variable I get 4. How is
that? It should be another number (31).. but I guess i'm getting the
size of type unsigned char. Right?


No, sizeof(unsigned char) is 1 by definition. It looks like it could
be sizeof(unsigned char*), but that's not

How exactly did you ask the size of the 'packet' variable? Show us
some code.

Here's an example that works properly with gcc 2.95.2, 3.4.4, and
4.0.2:

#include <stdio.h>
int main(void)
{
int len = 42;
char vla[len];
printf("sizeof vla = %d\n", (int)sizeof vla);
return 0;
}

The output is:

sizeof vla = 42

If you passed it as an argument to a function and applied sizeof to
the parameter, you'd get the size of the pointer, but that's because
array expressions decay to pointers in most contexts; if you apply
sizeof directly to the array object, it doesn't decay and you get the
size of the array.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #5
On Thu, 10 Nov 2005 00:31:56 GMT, Fernando Barsoba
<fb******@veriz on.net> wrote:
Hi,

I decided to start a new topic about my memory allocation question. One
of the answers was that variable length arrays are possible using C99. I
found this topic that also mentions it.

http://groups.google.com/group/comp....335d22d6e6fb34

I did some test, but I get strange results:

int size_array = sizeof(struct ip) + cnf->msg_length;
unsigned char packet[size_array];

And then, when asking the size of the 'packet' variable I get 4. How is
that? It should be another number (31).. but I guess i'm getting the
size of type unsigned char. Right?


Not likely. An unsigned char is guaranteed to have a size of one.
<<Remove the del for email>>
Nov 15 '05 #6
Keith Thompson wrote:
Fernando Barsoba <fb******@veriz on.net> writes:
[snip]
I did some test, but I get strange results:

int size_array = sizeof(struct ip) + cnf->msg_length;
unsigned char packet[size_array];

And then, when asking the size of the 'packet' variable I get 4. How is
that? It should be another number (31).. but I guess i'm getting the
size of type unsigned char. Right?

No, sizeof(unsigned char) is 1 by definition. It looks like it could
be sizeof(unsigned char*), but that's not

How exactly did you ask the size of the 'packet' variable? Show us
some code.

Here's an example that works properly with gcc 2.95.2, 3.4.4, and
4.0.2:

<snip>
And there's me badmouthing gcc about not supporting VLAs yet. At least
this part works. :-)

S.
Nov 15 '05 #7
Fernando Barsoba <fb******@veriz on.net> writes:
Artie Gold wrote:
Fernando Barsoba wrote: [snip]
I did some test, but I get strange results:

int size_array = sizeof(struct ip) + cnf->msg_length;
unsigned char packet[size_array];

And then, when asking the size of the 'packet' variable I get 4. How is
that? It should be another number (31).. but I guess i'm getting
the size of type unsigned char. Right?
Show us the code you used to test.
[You passed `packet' to a function, didn't you...]


I pass 'packet' to a function later on.. but not for this test. I
checked the size of 'packet' after
>>unsigned char packet[size_array];

and there's where I get the value 4.


That shouldn't be happening.

Show us the code you used to test. That means posting a complete
self-contained program that we can compile and run ourselves, along
with the exact output that it gave you. Without that, we really can't
help.
Regarding to the other question, I'm using Eclipse + CDT (and the
compiler is the one that comes with Cygwin which I am using too).


Cygwin comes with gcc (version 3.4.4 if you've updated it recently).

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #8
Keith Thompson wrote:
Fernando Barsoba <fb******@veriz on.net> writes:
Artie Gold wrote:
Fernando Barsoba wrote: [snip]

Show us the code you used to test.
[You passed `packet' to a function, didn't you...]

I pass 'packet' to a function later on.. but not for this test. I
checked the size of 'packet' after
>>unsigned char packet[size_array];

and there's where I get the value 4.


That shouldn't be happening.

Show us the code you used to test. That means posting a complete
self-contained program that we can compile and run ourselves, along
with the exact output that it gave you. Without that, we really can't
help.


Ok, I tested the following code:

#include <stdio.h>
#include <stdlib.h>
#include <netinet/ip.h>
int main(int argc, char **argv) {

int size_array = sizeof(struct ip) + 11;
unsigned char packet[size_array];
// unsigned char ipheader_cpy[sizeof(struct ip)];

printf("Result for sizeof(packet) is: %d", sizeof(packet)) ;
exit(0);

}

And I got as a result:
$ ./Test.exe
Result for sizeof(packet) is: 31

So, I think I have to assume that it's working correctly. The problem is
that I'm using Eclipse/CDT and the debugger seems to have problems with
this kind of code (with VL Arrays). It showed me the '4' that I was
referring to, in the debugging perspective/window from Eclipse... I
thought the problem was with the code, but all you made me focus on the
test.. I guess I relied too much on the debugger...sigh ..

Thanks,

FBM
Regarding to the other question, I'm using Eclipse + CDT (and the
compiler is the one that comes with Cygwin which I am using too).


Cygwin comes with gcc (version 3.4.4 if you've updated it recently).

Nov 15 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
2385
by: Tan Thuan Seah | last post by:
Hi all, I was told this in one of the university course I was doing. In C we may expect good performance for: double a, c, d; for (i=0; i<N; i++) for(j=0; j<N; j++) a = a + c *d;
5
14101
by: dam_fool_2003 | last post by:
Hai, I studied that the array size is fixed. But I come across a word called "variable length array". Is it possible to change the array size? So I tried the following: #include<stdio.h> #include<stdlib.h> int main(void) { int y = { 7, 9,10},i; for (;i<20;i++)
19
2124
by: Skybuck Flying | last post by:
Hi, I think I might have just invented the variable bit cpu :) It works simply like this: Each "data bit" has a "meta data bit". The meta data bit describes if the bit is the ending bit of a possibly large structure/field.
7
6422
by: heddy | last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int Newsize); and the newsize is smaller then what the array was previously, are the resources allocated to the objects that are now thown out of the array released properly by the CLI?
16
6708
by: John | last post by:
Does the length of my C variable names have any affect, performance-wise, on my final executable program? I mean, once compiled, etc., is there any difference between these two: number = 3; n = 3; I know its setting aside storage for the variable itself; does it also use up more storage if the variable name is longer? I realize it would probably take quite a lot of long variable names to make any impact if so, but I was just curious...
53
26358
by: fdmfdmfdm | last post by:
This is an interview question and I gave out my answer here, could you please check for me? Q. What are the memory allocation for static variable in a function, an automatic variable and global variable? My answer: static variable in function and global variable are allocated in head, and automatic variable is allocated in stack. Right?
1
7959
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was compiled and run without any erros but the second program has a run time error when the function return from allocate and the ptr become NULL. How to fixed this? Second Program: /* Best Method to allocate memory for 2D Array because it's ...
27
3283
by: aravind | last post by:
Hi, I need to develop a menu system for a project of mine. The menu will be displayed on a character LCD display driven by ARM7 Microcontroller. For this purpose i wish to construct a structure in C which will contain a the following - struct menu { int n (no. of elements in menu); char menu_items; (This will contains the strings to be displayed on the LCD, 20 characters and n lines funcptr fptr; (Pointer to the corresponding menu...
8
3240
by: Andrew Smallshaw | last post by:
I'm working on a data structure that began life as a skip list derivative, but has evolved to the point that it now only has a passing resemblance to them. Each node of this structure has a few constant size variables (ints and the like), an array holding the actual data (chars in this case) and a random number of pointers to other nodes in the structure. At the moment my code uses a fixed size array for the contents (which may or may...
0
8113
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8203
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8378
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6981
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6047
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5504
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4066
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2517
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1373
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.