1

I was trying to save the binary equivalent of a 32 bit number in an array A. For testing my showbits() function , I choosed 8,9 when I came across this thing:

I am facing an unreasonable thing in my code when I am placing memset in the function showbits(),I am geting absurd integers while I expect an output something as

00000000000000000000000000001000

that is the binary equivalent of 8 . While when I place memset in the main() method, it works properly and gives me the right output.Am I going out of bounds(I cannot see it !) .

My code : SHOWBITS:

void showbits(int A[32],int num)
{
    int k=0;
    memset(A,0,sizeof(A));
    while(num>0)
    {
        A[k] = num&1;
        k++;
        num>>=1;
    }
    return ;
}

Note: I have placed memset in showbits ,and I am getting incorrect answers! MAIN:

int main()
{
    int A[32],i;
    showbits(A,8);
    for(i=31;i>=0;i--)
        printf("%d",A[i]);
    return 0;
}

Whole program for testing:

#include<stdio.h>
#include<algorithm>
#include<cstring>
using namespace std;
void showbits(int A[32],int num)
{
    int k=0;
    memset(A,0,sizeof(A));
    while(num>0)
    {
        A[k] = num&1;
        k++;
        num>>=1;
    }
    return ;
}
int main()
{
    int A[32],i;
    showbits(A,8);
    for(i=31;i>=0;i--)
        printf("%d",A[i]);
    return 0;
}

When I place that memset statement in Main method before showbits() , I am getting correct output!

EDIT If someone is interested in what I am getting

398420075242008462686872420075219611920941961187434-2205336646196127610926869242 68672826866724200752000202903316219611874341961187478819611565142686716196182637 61961141748268665201000

Shubham Sharma
  • 799
  • 7
  • 31

3 Answers3

5

The A[32] in the method is actually just a pointer to A. Therefore, sizeof is the size of *int. Take the following test code:

void szof(int A[32])
{
    std::cout << "From method: " << sizeof(A) << "\n";
}
int main(int argc, char *argv[])
{
    int B[32];
    std::cout << "From main: " << sizeof(B) << "\n";
    szof(B);
    return 0;
}

which give the following output:

From main: 128
From method: 8

Thus, the memset sets fewer bits than you think.

Avi Ginsburg
  • 10,323
  • 3
  • 29
  • 56
4

You must pass A by reference

void showbits(int (&A)[32],int num)

See here for more details: C++ pass an array by reference

Community
  • 1
  • 1
ssemilla
  • 3,900
  • 12
  • 28
  • Oh sorry, you are right. I'll try to edit my answer. – ssemilla Jun 22 '15 at 04:06
  • 1
    @Avi forgive me, I think `sizeof(A)` in this case would return 128 which is what we expect. Pointers are different from references. If A was declared as *int, then it `sizeof(A)` returns the word size of the machine. – ssemilla Jun 22 '15 at 04:10
  • I do not know ,when I am trying to do what he said I am getting correct output! When I am trying to print the sizeof A in showbits function, it is indeed giving the correct size of real A i.e. 128 bytes? @user4581301 – Shubham Sharma Jun 22 '15 at 04:11
  • 1
    @ShubhamSharma Yes, that is correct. I also tried this on my machine and it gives the expected result. – ssemilla Jun 22 '15 at 04:15
  • My mistake on tagging Avi, my comment should be directed to @user4581301 – ssemilla Jun 22 '15 at 04:16
  • I found out(this might be helpful) : http://stackoverflow.com/questions/57483/what-are-the-differences-between-a-pointer-variable-and-a-reference-variable-in – Shubham Sharma Jun 22 '15 at 04:17
4

Avi explained the problem in your code already. Another possible solution is to use C++-style arrays, instead of C-style arrays and memset. Then there is no possibility of a memset length error. Also there is no loss of performance.

#include <array>

void showbits(std::array<int, 32> &A, int num)
{
    A = {};     // set all entries to 0
    // ...
}

int main()
{
    std::array<int, 32> A;
    // ...
}
M.M
  • 138,810
  • 21
  • 208
  • 365