0

Table declaration in a function

 3 years ago
source link: https://www.codesd.com/item/table-declaration-in-a-function.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Table declaration in a function

advertisements

Is it safe to write code like below?

void func(int v[], int size) {
 int array_local[size];

 for(int i = 0; i < size; i++) array_local[i] = v[i];

 /*
 Other stuff...
 */

}

Or could I stumble upon some kind of error?


Yeah this is perfectly fine and valid on and above C99. It is called VLA.

To be on safer side, you should put a check on the value of size before using it as the number of elements of the array. As you've got the size defined as int, you should prohibit passing a value of say -5 to size.

That said, for the remaining part of the code (as a general suggestion)

  1. v[i] should not cause in memory overrun.
  2. The array is local to the function. You should never try returning the address of the array (Either through a pointer or through return statement).

Related Articles

Using a table declared in a function to another function

I am currently stuck on a part of programming. I've made a function to get the amount of rows and columns for a 2D array and then filled that array up with information from an input file. I now need to use that array in another function to display th

C ++ - Declaration of internal functions of tables

If I declare an array inside a function, is the memory deallocated upon leaving the function? I wouldn't have thought so, however, when I declare an array inside a function, write the pointer to the array to a global variable and then attempt (outsid

How do I include a Temp table in a SQL function

I have a SQL Server 2014 server linked to an Oracle server. I want to use a temp table in a function to return a dataset from the Oracle database and then use the my function to return results using regular T-SQL. Since I am rather new to this I am c

Does the symbol table for C ++ contain function names and class names?

I have been searching through various posts regarding whether symbol table for a C++ code contains functions' name along with the class name. Something which i could find on a post is that it depends on the type of compiler, if it compiles code in on

Drop a temporary table when leaving a function

I use a temp table in a function with the 'on commit drop' option. My problem is, in certain cases, a more global function can call the first one twice, so the "create temp table" is called twice before the commit - so I have the normal error &q

How to delete everything from a database (tables, views, stored procedures, functions)?

I want to drop everything in the database, tables, views, stored procedures, and functions. I found some link but they don't work on complex database. It failed to drop tables. This is my own version of the script that I found on this website and twe

Update the table in a complex function using exceptions

I'm little lost trying to solve a problem. At first I've this 5 tables: CREATE TABLE DOCTOR ( Doc_Number INTEGER, Name VARCHAR(50) NOT NULL, Specialty VARCHAR(50) NOT NULL, Address VARCHAR(50) NOT NULL, City VARCHAR(30) NOT NULL, Phone VARCHAR(10) NO

PostgreSQL - equivalent version of a table declared in MySQL?

I have the following table declared in MySQL: CREATE TABLE IF NOT EXISTS `ci_sessions` ( `session_id` varchar(40) COLLATE utf8_bin NOT NULL DEFAULT '0', `ip_address` varchar(16) COLLATE utf8_bin NOT NULL DEFAULT '0', `user_agent` varchar(150) COLLATE

Why is a function declaration in another function compiled and what does it do?

I intended to call a private class member function, but by a copy&paste mistake pasted the line as this function is declared in the header file: void DebugView::on_cbYAxisEnabled_stateChanged(int) { void updateAxisEnabled(); } instead of void DebugVi

How to pass parameters to a function declared as left = function ()

How can I pass parameters to a function declared like something = function(){}; window.prototype.initInterface = function(){ this.mainPane = document.createElement('div'); this.mainPane.style.border="5px solid grey"; this.mainPane.style.margin=&

In php, how can I make sure that the static variables declared in a function will not be restarted each time the function is called?

It seems that a static variable declared in a function is re-initiated whenever the function is called, how can I use the function in a way that re-calling the function will re-use the static parameter? I defined the function 'testStatic' in static.p

Why can not models be declared in a function?

Reading C++ Templates: The Complete Guide and it says Note that templates cannot be declared in a function It does not give explanation and/or cross reference to any other chapter in the book or external resource. Could someone help in explaining thi

How to declare a model function taking a model template parameter with a type of known model argument?

I have the following two functions: class Leaf {...}; void SpitLeaves(std::string & sdata, std::vector<Leaf> const & leaves); void SpitLeaves(std::string & sdata, std::set<Leaf> const & leaves); The definition of these function

using nhibernate t-sql table-valued user-defined functions

Is it possible to use table t-sql table-valued user-defined function as part of HQL query ? here Nhibernate filtering by user defined function output is a example how to create custom dialect extention but in this example scalar UDF is used. Let's as

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK