473,396 Members | 1,995 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Segmentation Fault during memory allocation

I am trying to program a code with memory allocation. I feel like the code is right and it compiles properly, however im hit with a segmentation fault when I try and run it. The following is my code and any help will be appreciated.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <malloc.h>
  5.  
  6. void pound(int argc, char*str)
  7.   int i;
  8.   i = 0;
  9.     do {
  10.       printf("Input Parameter= ##%s##\n",str);
  11.       i = i + 1;
  12.     } while (i < argc);
  13. }
  14.  
  15. int 
  16. main(int argc, char**argv)
  17. {
  18.   char *str;
  19.   unsigned num;
  20.  
  21.   printf("Size of array = ");
  22.   scanf("%d", num);
  23.   str = (char *)malloc((size_t)num + 1);
  24.   if (str == NULL)
  25.     {
  26.     printf("Can't allocate memory!");
  27.     exit(0);
  28.     }
  29.   printf("Allocated array!");
  30.   free(str);
  31.  
  32.   if (argc < 3) {
  33.       printf("Error: You need to pass in atleast one argument.\n");
  34.       exit(-1);
  35.   }
  36.   strncpy(str, argv[1], 99);
  37.   printf("Input parameter = %s\n", str);
  38.  
  39.   int count = atol(argv[2]);
  40.  
  41.   pound(count, str);
  42. }
May 21 '10 #1

✓ answered by Markus

On line 23, you're passing the value of num, not it's address.

2 1928
Markus
6,050 Expert 4TB
On line 23, you're passing the value of num, not it's address.
May 21 '10 #2
Thanks a lot Markus. That fixed it perfectly.
May 21 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: fudmore | last post by:
Hello Everybody. I have a Segmentation fault problem. The code section at the bottom keeps throwing a Segmentation fault when it enters the IF block for the second time. const int...
6
by: I_have_nothing | last post by:
Hi! I am new in C. I try to use dynamical allocation fuction malloc( ) and realloc( ). I found something strange. After several calling realloc( ), the malloc( ) will give me a Segmentation...
59
by: Christian Christmann | last post by:
Hi, I'm wondering why this program does not crash with a segmentation fault: #include <malloc.h> #include <string.h> #include <stdio.h> int main()
23
by: sam_cit | last post by:
Hi Everyone, I have the following program unit, #include <stdlib.h> int main() { char *p = (char*)malloc(100); if(p==NULL)
9
by: weidongtom | last post by:
Hi, I've written the code that follows, and I use the function add_word(), it seems to work fine *before* increase_arrays() is called that uses realloc() to allocate more memory to words. But...
8
by: Andrea | last post by:
I wrote this code: void * xmalloc (size_t size){ register void *value = OPENSSL_malloc(size); if (value == 0) printf("virtual memory exhausted"); return value; } int _chooseTSK(char*...
5
by: paragon.john | last post by:
Hello all, I am trying to read a file into some allocated memory as part of a program and I am running into a problem. The program gets a segmentation fault during fread. I have previously...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.