Thread: 2D array allocation

  1. #1
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404

    2D array allocation

    Here is what I am doing. I am writing my own ini file reader.
    Pretend this is the file:
    Code:
    #Comment 1
    [Main]
    string=none
    numeric=6
    
    #Comment 2
    [Other]
    string=hello
    numeric=19
    Here is how I want it to be layed out in a variable;

    Code:
    data[0] = "[Main]"
    data[0][0] = "string=none"
    data[0][1] = "numeric=6"
    data[1] = "[Other]"
    data[1][0] = "string=hello"
    data[1][1] = "numeric=19"
    Hopefully you can see my pattern. I am declaring my variable to be used like this:
    Code:
    char **data = NULL;
    I already have my functions to hand me my data, all I need to do now is to allocate the correct space and assign the data to it.

    I am having a lot of trouble here. Right now I just want to at least get the "sections" of the ini written (they go into the 1d part of the array). Here is what I have been currently trying to work with:
    Code:
    						data[l] = (char *)realloc(data[l], (sizeof(char) * ((j - i) + 1)));
    						l++;
    						strncpy(data[l-1], &buffer[i], j - i);
    j - i return the length of the "section". I also am not getting very good error details, just a "yourprog has stopped working..."

    By the way, this is on windows.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >char **data = NULL;
    Did you declare the first dimension of data? For example if there are two sections (num_sections=2):
    Code:
    data = malloc(num_sections * sizeof *data);
    And if you don't know how many sections there will be, you'd have to realloc() each time you have a new section. Also strncpy may not copy the string terminator to the destination, so you may need to add:
    Code:
    data[l-1][j-i] = '\0';
    I would prefer to avoid strncpy() for this reason, and use strncat() instead:
    Code:
    						data[l] = (char *)realloc(data[l], (sizeof(char) * ((j - i) + 1)));
    						l++;
    						data[l-1][0] = '\0';	/* Equivalent to strcpy(data[l-1], ""); */
    						strncat(data[l-1], &buffer[i], j - i);
    Last edited by swoopy; 06-24-2008 at 11:00 PM.

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Just curious but is there any reason you are using C for this? C++ offers some very nice stream I/O classes that would make your life a lot easier.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Whoa back up a sec:
    I am writing my own ini file reader.
    I must instinctively ask the obvious question:

    WHY?!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by carrotcake1029 View Post
    Code:
    data[0] = "[Main]"
    data[0][0] = "string=none"
    data[0][1] = "numeric=6"
    data[1] = "[Other]"
    data[1][0] = "string=hello"
    data[1][1] = "numeric=19"
    And I can spot a problem right away.
    There is no such thing as data[0]. The closest thing that DOES exist is data[0][0], but it's occupied with "string=none".
    The best approach to this problem, I think, is a linked list, and not a 2D array.

    Quote Originally Posted by Bubba View Post
    Just curious but is there any reason you are using C for this? C++ offers some very nice stream I/O classes that would make your life a lot easier.
    Isn't it always easier in C++ than C when you can do it?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    Quote Originally Posted by iMalc View Post
    Whoa back up a sec:I must instinctively ask the obvious question:

    WHY?!
    Learning experience. I probably have to say that 90% of what I write in C is to just grasp a better understanding of the language. If I hopped right into C++, I wouldn't be understanding the code I was using. I don't tend to enjoy using what someone else has written until after I can thoroughly understand it.

    The best approach to this problem, I think, is a linked list, and not a 2D array.
    Good point. I guess this would be a good application for a linked list.

  7. #7
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    Ok, I am making some good progress using a singly linked list. My problem is getting the char * ready to insert into the linked list
    Code:
    						temp = realloc(temp, sizeof(char) * (j-i)+1);
    						strncpy(temp, &buffer[i], (j-i)+1);
    						list_add(&n, temp);
    temp is declared as char *temp.
    buffer is declared as char *buffer.

    Is this a proper way to be inserting character strings?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I would urge you to create a string and a couple of functions for string handling.
    A struct for strings, with a char* and a size_t for size.
    When copying or appending stuff, you would have to check if there's enough storage and reallocate if necessary.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  3. Allocating a 2D Array Differently
    By pianorain in forum C++ Programming
    Replies: 13
    Last Post: 12-15-2005, 02:01 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. 2D arrays:dynamic allocation and freeing
    By bravetanveer in forum C Programming
    Replies: 5
    Last Post: 02-05-2005, 07:24 AM