Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8770361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:32:50+00:00 2026-06-13T17:32:50+00:00

All I am writing a very simple C code of dynamic 2D array declaration

  • 0

All

I am writing a very simple C code of dynamic 2D array declaration and initializing it with memset and then printing out the value. My code is something as follows:

float **env;
int i,j,num;

printf("Enter a number : \n");
scanf("%d",&num);

env = (float **)malloc(num*sizeof(float *));

for(i=0;i<num;i++)
{env[i] = (float *)malloc(num*sizeof(float));}

memset(env, 0, sizeof(float)*num*num);

for(i=0;i<num;i++)
{  for (j=0;j<num;j++)
   {
      printf("%f\t",env[i][j]);
      if (j == num -1)
       { printf("\n\n");}
    }
}

for(i=0;i<num;i++)
{free(env[i]);
}

free(env);

When I compile the program, there is no compile error or warning, but when I try to print out the values I am not able to print them. Then I debugged the program, and after the memset statement the env 2D variable is showing something like
CXX0030: Error: expression cannot be evaluated, and when I print the values a window appears showing

Unhandled exception at 0x008b1e27 in ***.exe: 0xC0000005: Access violation reading location 0x00000000.

I have tried explicitly initializing the 2D array env to 0 using 2 for loops and it works perfectly and I am also able to print the values, but it doesn’t work when I use memset. It would be very helpful if somebody could help me out. Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T17:32:51+00:00Added an answer on June 13, 2026 at 5:32 pm

    Firstly, the usual advice: stop using type names under sizeof and stop casting the result of malloc. This is what it should’ve looked like

    env = malloc(num * sizeof *env);
    
    for (i = 0; i < num; i++)
      env[i] = malloc(num * sizeof *env[i]);
    

    In general, prefer to write you memory allocations by following this pattern

    some_ptr = malloc(N * sizeof *some_ptr);
    

    Secondly, the “array” you create by using this technique is not a classic C-style contiguous 2D array. Instead, you get a “simulated” 2D array assembled from a bunch of completely unrelated 1D arrays. The latter can potentially be scattered randomly in memory. This immediately means that you won’t be able to process your array as a continuous object. Meanwhile your memset call attempts to do just that. You can’t do it this way. In your case you have to zero each 1D sub-array independently, using a cycle.

    However, instead of allocating your 1D sub-arrays independently, you can allocate them all as a single memory block

    assert(num > 0);
    env = malloc(num * sizeof *env);
    env[0] = malloc(num * num * sizeof *env[0]);
    
    for (i = 1; i < num; ++i)
      env[i] = env[i - 1] + num;
    

    If allocated as shown above, you array’s data will be stored in a single continuous memory block, although you still have to understand what you are doing when you are working with that array. For example, memseting the array data would look as follows

    memset(*env, 0, num * num * sizeof **env);
    

    or

    memset(&env[0][0], 0, num * num * sizeof env[0][0]);
    

    (which is the same thing).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a code that I am sure is very simple, but for
I am writing a simple python code for a very task which some of
I was writing a very simple piece of code given in the cuda by
I am writing a program for a class that is very simple. All it
I'm writing a very simple CRUD app that takes user stories and stores them
Writing a small HTML web page with some very simple Javascript in it, I
I'm writing a very simple Java Game. Let me describe it briefly: There are
Here is the code in question (a very simple crawler), the file is a
I'm writing a very basic WPF dialog and want to apply a simple style
I have written an Interface for writing a very very simple Plugin. In fact

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.