Questions tagged [alloca]

Alloca is a function to allocate dynamically memory on the Stack in C. Such memory will be automatically deallocated when leaving the function.

87 questions
1
vote
3 answers

Please give me the clarity on this

num1=0 Stored_num=23 def input_num(): num1=int(input('Enter the number again: ')) while num1!=stored_num: input_num() else: print('Congrats ! The entered number mathched the stored number') The code above is not accepting the else…
1
vote
1 answer

Trying to understand the Assembly implementation of the alloca() function on x86

I am very new to assembly and I am currently reading a book called Reverse Engineering for Beginners and I got to the part that talks about memory allocation on the stack. I understand (I think) the concept of stack allocation but in the example…
Ronen
  • 40
  • 6
1
vote
2 answers

strdupa() implementation for Visual C

I am trying to port a C (not C++) program from GCC to Visual Studio. The GCC specific function strdupa() is widely used in this program. Is there any way to implement this function for Visual C. PS. I understand that it uses alloca() and it is…
Sandro
  • 2,707
  • 2
  • 20
  • 30
1
vote
2 answers

How to replace alloca in an implementation of execvp()?

Take a look at the NetBSD implementation of execvp here: http://cvsweb.netbsd.se/cgi-bin/bsdweb.cgi/src/lib/libc/gen/execvp.c?rev=1.30.16.2;content-type=text%2Fplain Note the comment at line 130, in the special case for handling ENOEXEC: /* * we…
amoe
  • 4,473
  • 4
  • 31
  • 51
1
vote
2 answers

Declaring memory on stack overwrites previously declared memory

How can I allocate memory on the stack and have it point to different memory addresses so I can use it later? For example. this code: for (int i = 0; i < 5; i++) { int nums[5]; nums[0] = 1; printf("%p\n", &nums[0]); } Will print out the…
Aamir
  • 443
  • 3
  • 16
1
vote
1 answer

How can I emulate a stack frame in C++?

I am writing a container that uses alloca internally to allocate data on the stack. Risks of using alloca aside, assume that I must use it for the domain I am in (it's partly a learning exercise around alloca and partly to investigate possible…
OMGtechy
  • 7,935
  • 8
  • 48
  • 83
1
vote
4 answers

Allocate aligned memory on the stack like _alloca

The documentation for _alloca() says here: The _alloca routine returns a void pointer to the allocated space, which is guaranteed to be suitably aligned for storage of any type of object. However, here it says: _alloca is required to be…
Serge Rogatch
  • 13,865
  • 7
  • 86
  • 158
1
vote
1 answer

symbol lookup error: undefined symbol: _alloca

I'm building a shared object (.so) that internally makes use of a function _alloca() (defined in malloc.h). This .so compiles smoothly. But as soon as I call a function that makes use of _alloca(), my application crashes whith a console…
Elmi
  • 5,899
  • 15
  • 72
  • 143
1
vote
1 answer

alloca inside compound statement

is it possible to use alloca inside compound statement? Example: typedef struct { size_t len; char* data; } string_t; #define str_to_cstr(str) \ ({ \ char* v = alloca(str.len + 1); \ v[len] = 0; \ memcpy(v, str.data, str.len);…
user939407
  • 43
  • 1
  • 5
1
vote
4 answers

C usage of malloc inside a function

I have the following function: char * decrypt(const char *p, int key) { char *tmp = malloc(strlen(p) + 1); for (int i = 0; p[i] != '\0'; i++) { if (p[i] >= 'A' && p[i] <= 'Z') { if (key <= p[i] - 'A') tmp[i] = p[i] - key; …
user3340372
1
vote
2 answers

Wrap alloca function in C

Is it possible to wrap the C function alloca into "another"? (only macros of course) Something like: #define my_alloca(size) \ ({ \ …
Marco
  • 7,007
  • 2
  • 19
  • 49
1
vote
1 answer

Intended usage of alloca/memset in LLVM

When allocating space on the stack using alloca(), is it necessary to clear the memory or is it guaranteed to contain only zeros? I came up with the following LLVM code. Although it compiles, it leads to a core dump. In the code I was trying to…
1
vote
1 answer

alloca and ObjectiveC Garbage Collector

In an objective C project with GC enabled, I am allocating an array of variable size on the stack like this: MaValue *myStack = alloca((sizeof(id) * someLength)); (The reason why I want to do this is not important:) Then, within a loop, I push and…
Christoph
  • 687
  • 1
  • 6
  • 12
1
vote
1 answer

alloca() of a templated array of types: how to do this?

I have a smart pointer type, and would like to construct an object that takes a pointer of that type and a count (dynamically calculated at runtime) and allocates enough memory from the stack to hold that many instances of the object the smart…
moonshadow
  • 86,889
  • 7
  • 82
  • 122
0
votes
8 answers

Resizing dynamic stack allocations in C++

I'm writing a small ray tracer using bounding volume hierarchies to accelerate ray tracing. Long story short, I have a binary tree and I might need to visit multiple leafs. Current I have a node with two children left and right, then during travel()…
jonasfj
  • 2,349
  • 2
  • 24
  • 22