Zero out two dimensional array

This is a discussion on Zero out two dimensional array within the C Programming forums, part of the General Programming Boards category; Hello, I have an array Code: char host_fname[80][250]; whenever i write to it, it works fine. the problem arises when ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    61

    Zero out two dimensional array

    Hello,

    I have an array

    Code:
    char host_fname[80][250];
    whenever i write to it, it works fine.

    the problem arises when i write to it again.

    if a shorter string of characters is written than the last [to the same array element], the existing characters are displayed after the null terminator of the latest string written.

    I know i can combat this by memset()ing the array to zero before writing to it again.

    The problem is i do not know weather i should start from host_fname or host fname[80] and how long to set memory to zero (without, obviously affecting memory not allocated to this array) - given that i would like to reset the whole array.

    thankyou for any advice regarding this matter, you help is much appreciated

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Write in your shorter string of char's into the array, at the end of each string, be sure you have an end of string marker char: '\0'.

    Then when you look at your array, like this:

    array[0] //only the first dimension given

    in a watch window, or when you print out the array, you'll see only the shorter string's char's.

    I use this all the time, and have no problems with it. Never had to use memset for this.

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    I don't really get what you are doing. Give us a sort example

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by davo666 View Post
    I know i can combat this by memset()ing the array to zero before writing to it again.
    That would be the solution right there. So how do you think the memset() would look like?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    61
    thing is, im reading UTF8 characters and i made sure i copied a Null character (0x00) into the last element of the array... i will recheck and see.

    ... Reading the Cprogramming lanuguage (kernighan and ritchie ) it states that if you setup a mutildimensional array it sets aside row times col of your datatypes... i just dont know if they are all consecutive memory cells or not...

    I had a though, the fact that i make NSString pointers from UTF8 character pointer array, may be why i am experiencing the caching effect.

    please shout me down if i am not allowed to post here because i have objective-c outputting my pure C function values

    thankyou for you help

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Quote Originally Posted by davo666 View Post
    i just dont know if they are all consecutive memory cells or not...
    But they are...
    An array is a number of elements laid of next to each other in memory.
    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.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    61
    but are mulitdimensional arrays consecutive as well?

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Yes! You can think of them an array of arrays.
    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.

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    61
    you were right about not having the problem with null terminated strings.

    when i call the function recursively from obj-C the malfunction happens there.

    simply calling a memset(host_fname[iteration],0,250) after converting the UTF string to an NSString* solves this bi-lingual problem

    Sorry for posting off topic, and thanks for your time

  10. #10
    Registered User
    Join Date
    Jan 2009
    Posts
    61
    ahh, thankyou,
    so is this correct
    Code:
    int arr[2][2];
    
    //would theoretically look like this in memory:
    
    arr[0][0],arr[0][1]arr[0][2]arr[1][0]arr[1][1],arr[1][2],arr[2][0],arr[2][1],arr[2][2]
    thanks clearing that up!

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,632
    No, that array would only have 4 integers indexed from [0][0], [0][1], [1][0] and [1][1].
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Quote Originally Posted by davo666 View Post
    you were right about not having the problem with null terminated strings.

    when i call the function recursively from obj-C the malfunction happens there.

    simply calling a memset(host_fname[iteration],0,250) after converting the UTF string to an NSString* solves this bi-lingual problem

    Sorry for posting off topic, and thanks for your time
    But that seems more like a hack rather than a solution.
    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.

  13. #13
    Registered User
    Join Date
    Jan 2009
    Posts
    61
    the memset after calling recursively adds extra problems however : (

    I think the problem stems because I read character by character from another array.

    here is the code behind the function
    Code:
    int q;
    for(q=1; q<=248; ++q){
    
    if(resp[q+9] == 0x00)
    	break;
    if(q == 248){
    host_fname[y][q-1] = 0x00;
    }
    host_fname[y][q-1] = resp[q+9];
    }
    this does add a null character though (i think the utf converter function in objc reads past the null byte)

    I have seen the null byte printed to the screen, in your more experienced eyes, is there a null byte printed (btw i am very sure there is a null byte from the array it takes the chars from)

    tjanks for your help

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Don't use "magic numbers" such as 248, because noone knows what character that is. Use proper characters, ie '\n' instead.
    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.

  15. #15
    Registered User
    Join Date
    Jan 2009
    Posts
    61
    lol sorry! running a memset(host_fname[y],0,250); before the for loop actually fixes this problem (im assuming it is becuase i am reading byte for byte?) or am i just bad at C

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. two dimensional array
    By leisiminger in forum C Programming
    Replies: 12
    Last Post: 03-09-2008, 11:53 PM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 12:01 AM
  4. Replies: 5
    Last Post: 11-20-2001, 11:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21