Skip to main content

Array parsing in C++

Array parsing in  C++ is always done by reference, and in std there's no concept of parsing by value. From a stakoverflow question I summarize this below. The array size information is lost here.

void by_pointer(int *p, int size);
void by_pointer(int p[], int size);
void by_pointer(int p[7], int size);   // the 7 is ignored in this context!

void by_reference(int (&a)[7]); // only arrays of size 7 can be passed here!

template void by_reference(int (&a)[size]);

void TestMethod(char cArray[], int size) {
    

    const char* pTest = "test";
    strncpy(cArray, pTest,
size);
    cArray[
size-1] = '\0';
   
}

 

Usage:
char cMyArray[7] =  {a};
TestMethod(cMyAray, 7} 

However this can be quite tricky to understand. Take the problem of dynamic resizing of an array.

unsigned int iArrSize = 10;
int *iArr = new int[iArrSize];

for (int i = 0; i < 100; i++) {
        if (i >= iArrSize) {
            int *arr3 = new int[iArrSize*2];
            memcpy(arr3, iArr, iArrSize * sizeof (int));
            delete[] iArr;
            iArr = arr3;
            iArrSize = iArrSize*2;
        }

        iArr[i] = i;
    }
  
    for (int i = 0; i < 100; i++) {
        std::cout << iArr[i] << std::endl;
    }
  
 delete[] iArr;


This works just fine. Now try to replace the array resizing part with a function.......

 unsigned int iArrSize = 10;
 int *iArr = new int[iArrSize];

    for (int i = 0; i < 100; i++) {
        if (i >= iArrSize) {
            EnlargeArray(iArr, iArrSize);
            iArrSize = iArrSize*2;
        }

        iArr[i] = i;
    }
  
    for (int i = 0; i < 100; i++) {
        std::cout << iArr[i] << std::endl;
    }
  
 delete[] iArr;


 void EnlargeArray(int* arr, unsigned int arr_size) {

    int *arr3 = new int[arr_size*2];
    memcpy(arr3, arr, arr_size * sizeof (int));
    delete[] arr;
    arr = arr3;

}


This will lead to a segmentation fault and using valgrind would say that the line iArr[i] = i; is trying to use already deleted memory at delete[] arr in side the EnlargeArray(). Why?

The parameter arr will point to the start of the real array iArr, this is TRUE
So deleting this will delete the real array.
However we we say arr = arr3; I expected the real array to be set the start of the newly allocated array arr3, which is FALSE
This is because the parameter name is not passed by reference, but what gets passed  is actually a copy of the pointer, so when you assign something to it, the copy gets updated but not the original !!!

So you'd have to go with,
void EnlargeArray(int* & arr, unsigned int arr_size) or  void EnlargeArray(int** arr, unsigned int arr_size)

Comments

Popular posts from this blog

Detaching a process from terminal - exec(), system(), setsid() and nohup

Linux processes are created by fork() and exec(). The very first process of POSIX systems is init and subsequent processes are derived from the init as parent. These subsequent processes are child processes. During forking the parent process copies itself - with all I/O, address space, stack, everything. The only thing that is different is the process ID. The parent and child will have 2 different process IDs. The system() library function uses fork(2) to create a child process that executes the shell command specified in command using execl(3) as follows: execl("/bin/sh", "sh", "-c", command, (char *) 0); system() returns after the command has been completed. system() executes a command specified in command by calling /bin/sh -c command , and returns after the command has been completed. During execution of the command, SIGCHLD will be blocked, and SIGINT and SIGQUIT will be ignored.  system() calls are often made within programs to execut...

sprintf, snprintf, strcpy, strncpy and sizeof operator

"C library functions such as strcpy (), strcat (), sprintf () and vsprintf () operate on null terminated strings and perform no bounds checking." "snprintf is safer than sprintf" What do these statements really mean? int sprintf ( char * str , const char * format , ... )  int snprintf ( char * s, size_t n, const char * format, ... );  char * strcpy ( char * destination, const char * source ); char * strncpy ( char * destination, const char * source, size_t num );   The usage is something like; char* msg1 = new char[10]; strcpy(msg1, "test"); // 1 char buffer[128]; sprintf(buffer, "%s", msg); //2 strcpy : Copies bytes until it finds a 0-byte in the source code. The string literal "test" has 4 characters and a terminating null character at end, therefore needs 5 characters at least on msg1.  Is this dangerous? Yes, because if the source message is not null terminated it will read until a null character ...

Vaadin vs GWT

From Chapter 1 of book of Vaadin I quote the following. Vaadin Framework is a Java web application development framework that is designed to make creation and maintenance of high quality web-based user interfaces easy. Vaadin supports two different programming models: server-side and client-side . The server-driven programming model is the more powerful one . It lets you forget the web and program user interfaces much like you would program a desktop application with conventional Java toolkits such as AWT, Swing, or SWT. But easier. While traditional web programming is a fun way to spend your time learning new web technologies, you probably want to be productive and concentrate on the application logic. The server-side Vaadin framework takes care of managing the user interface in the browser and the AJAX communications between the browser and the server . With the Vaadin approach, you do not need to learn and deal directly with browser technologies, such as HTML or JavaScript. ...